Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1abd67d

Browse files
committed
updated
1 parentac35588 commit1abd67d

24 files changed

+819
-336
lines changed

‎.air.toml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
root ="."
2+
testdata_dir ="testdata"
3+
tmp_dir ="tmp"
4+
5+
[build]
6+
bin ="tmp\\main.exe"
7+
cmd ="go build -o ./tmp/main.exe ./cmd/server/main.go"
8+
delay =1000
9+
exclude_dir = ["assets","tmp","vendor","testdata"]
10+
exclude_file = []
11+
exclude_regex = ["_test.go"]
12+
exclude_unchanged =false
13+
follow_symlink =false
14+
full_bin =""
15+
include_dir = []
16+
include_ext = ["go","tpl","tmpl","html"]
17+
kill_delay ="0s"
18+
log ="build-errors.log"
19+
send_interrupt =false
20+
stop_on_error =true
21+
22+
[color]
23+
app =""
24+
build ="yellow"
25+
main ="magenta"
26+
runner ="green"
27+
watcher ="cyan"
28+
29+
[log]
30+
time =false
31+
32+
[misc]
33+
clean_on_exit =false
34+
35+
[screen]
36+
clear_on_rebuild =false

‎Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
.PHONY: dev dev-down goproto
1+
.PHONY: dev dev-downproto server-goclient-go
22

33
dev:
44
docker-compose up -d
55

66
dev-down:
77
docker-compose down
88

9-
go:
10-
air
9+
server-go:
10+
air cmd/server/main.go
11+
12+
client-go:
13+
go run cmd/client/main.go
1114

1215
proto:
1316
protoc --proto_path=proto --go_out=pb --go_opt=paths=source_relative\

‎client/geMe_client.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"time"
8+
9+
"github.com/wpcodevo/golang-mongodb/pb"
10+
"google.golang.org/grpc"
11+
)
12+
13+
typeGetMeClientstruct {
14+
service pb.UserServiceClient
15+
}
16+
17+
funcNewGetMeClient(conn*grpc.ClientConn)*GetMeClient {
18+
service:=pb.NewUserServiceClient(conn)
19+
20+
return&GetMeClient{service}
21+
}
22+
23+
func (getMeClient*GetMeClient)GetMeUser(credentials*pb.GetMeRequest) {
24+
25+
ctx,cancel:=context.WithTimeout(context.Background(),time.Duration(time.Millisecond*5000))
26+
defercancel()
27+
28+
res,err:=getMeClient.service.GetMe(ctx,credentials)
29+
30+
iferr!=nil {
31+
log.Fatalf("GeMe: %v",err)
32+
}
33+
34+
fmt.Println(res)
35+
}

‎client/main.go

Lines changed: 0 additions & 46 deletions
This file was deleted.

‎client/signup_client.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"time"
8+
9+
"github.com/wpcodevo/golang-mongodb/pb"
10+
"google.golang.org/grpc"
11+
)
12+
13+
typeSignUpUserClientstruct {
14+
service pb.AuthServiceClient
15+
}
16+
17+
funcNewSignUpUserClient(conn*grpc.ClientConn)*SignUpUserClient {
18+
service:=pb.NewAuthServiceClient(conn)
19+
20+
return&SignUpUserClient{service}
21+
}
22+
23+
func (signUpUserClient*SignUpUserClient)SignUpUser(credentials*pb.SignUpUserInput) {
24+
25+
ctx,cancel:=context.WithTimeout(context.Background(),time.Duration(time.Millisecond*5000))
26+
defercancel()
27+
28+
res,err:=signUpUserClient.service.SignUpUser(ctx,credentials)
29+
30+
iferr!=nil {
31+
log.Fatalf("SignUpUser: %v",err)
32+
}
33+
34+
fmt.Println(res)
35+
}

‎cmd/client/main.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"log"
5+
6+
"github.com/wpcodevo/golang-mongodb/client"
7+
"github.com/wpcodevo/golang-mongodb/pb"
8+
"google.golang.org/grpc"
9+
"google.golang.org/grpc/credentials/insecure"
10+
)
11+
12+
const (
13+
address="0.0.0.0:8080"
14+
)
15+
16+
funcmain() {
17+
conn,err:=grpc.Dial(address,grpc.WithTransportCredentials(insecure.NewCredentials()),grpc.WithBlock())
18+
19+
iferr!=nil {
20+
log.Fatalf("failed to connect: %v",err)
21+
}
22+
23+
deferconn.Close()
24+
25+
iffalse {
26+
signUpUserClient:=client.NewSignUpUserClient(conn)
27+
newUser:=&pb.SignUpUserInput{
28+
Name:"Micheal Smith",
29+
Email:"michealmith@gmail.com",
30+
Password:"password123",
31+
PasswordConfirm:"password123",
32+
}
33+
signUpUserClient.SignUpUser(newUser)
34+
}
35+
36+
getMeClient:=client.NewGetMeClient(conn)
37+
id:=&pb.GetMeRequest{
38+
Id:"628cffb91e50302d360c1a2c",
39+
}
40+
getMeClient.GetMeUser(id)
41+
42+
}

‎main.gorenamed to‎cmd/server/main.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,31 @@ func main() {
103103
startGrpcServer(config)
104104
}
105105

106+
funcallowedRoles()map[string][]string {
107+
constuserServicePath="/pb.UserService/"
108+
returnmap[string][]string{
109+
userServicePath+"GetMe": {"user"},
110+
}
111+
}
112+
106113
funcstartGrpcServer(config config.Config) {
107-
server,err:=gapi.NewGrpcServer(config,authService,userService,authCollection)
114+
authServer,err:=gapi.NewGrpcAuthServer(config,authService,userService,authCollection)
108115
iferr!=nil {
109-
log.Fatal("cannot create grpcserver: ",err)
116+
log.Fatal("cannot create grpcauthServer: ",err)
110117
}
111118

112-
grpcServer:=grpc.NewServer()
113-
pb.RegisterAuthServiceServer(grpcServer,server)
119+
userServer,err:=gapi.NewGrpcUserServer(config,userService,authCollection)
120+
iferr!=nil {
121+
log.Fatal("cannot create grpc userServer: ",err)
122+
}
123+
124+
interceptor:=services.NewAuthInterceptor(allowedRoles(),&config,userService)
125+
grpcServer:=grpc.NewServer(
126+
grpc.UnaryInterceptor(interceptor.Unary()),
127+
)
128+
129+
pb.RegisterAuthServiceServer(grpcServer,authServer)
130+
pb.RegisterUserServiceServer(grpcServer,userServer)
114131
reflection.Register(grpcServer)
115132

116133
listener,err:=net.Listen("tcp",config.GrpcServerAddress)

‎gapi/server.gorenamed to‎gapi/auth-server.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ import (
77
"go.mongodb.org/mongo-driver/mongo"
88
)
99

10-
typeServerstruct {
10+
typeAuthServerstruct {
1111
pb.UnimplementedAuthServiceServer
1212
config config.Config
1313
authService services.AuthService
1414
userService services.UserService
1515
userCollection*mongo.Collection
1616
}
1717

18-
funcNewGrpcServer(config config.Config,authService services.AuthService,
19-
userService services.UserService,userCollection*mongo.Collection) (*Server,error) {
18+
funcNewGrpcAuthServer(config config.Config,authService services.AuthService,
19+
userService services.UserService,userCollection*mongo.Collection) (*AuthServer,error) {
2020

21-
server:=&Server{
21+
authServer:=&AuthServer{
2222
config:config,
2323
authService:authService,
2424
userService:userService,
2525
userCollection:userCollection,
2626
}
2727

28-
returnserver,nil
28+
returnauthServer,nil
2929
}

‎gapi/get_me.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package gapi
2+
3+
import (
4+
"context"
5+
6+
"github.com/wpcodevo/golang-mongodb/pb"
7+
"google.golang.org/grpc/codes"
8+
"google.golang.org/grpc/status"
9+
"google.golang.org/protobuf/types/known/timestamppb"
10+
)
11+
12+
func (userServer*UserServer)GetMe(ctx context.Context,req*pb.GetMeRequest) (*pb.UserResponse,error) {
13+
id:=req.GetId()
14+
user,err:=userServer.userService.FindUserById(id)
15+
16+
iferr!=nil {
17+
returnnil,status.Errorf(codes.Unimplemented,err.Error())
18+
}
19+
20+
res:=&pb.UserResponse{
21+
User:&pb.User{
22+
Id:user.ID.Hex(),
23+
Name:user.Name,
24+
Email:user.Email,
25+
Role:user.Role,
26+
CreatedAt:timestamppb.New(user.CreatedAt),
27+
UpdatedAt:timestamppb.New(user.UpdatedAt),
28+
},
29+
}
30+
returnres,nil
31+
}

‎gapi/rpc_signin_user.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"google.golang.org/grpc/status"
1111
)
1212

13-
func (server*Server)SignInUser(ctx context.Context,req*pb.SignInUserInput) (*pb.SignInUserResponse,error) {
14-
user,err:=server.userService.FindUserByEmail(req.GetEmail())
13+
func (authServer*AuthServer)SignInUser(ctx context.Context,req*pb.SignInUserInput) (*pb.SignInUserResponse,error) {
14+
user,err:=authServer.userService.FindUserByEmail(req.GetEmail())
1515
iferr!=nil {
1616
iferr==mongo.ErrNoDocuments {
1717

@@ -36,14 +36,14 @@ func (server *Server) SignInUser(ctx context.Context, req *pb.SignInUserInput) (
3636
}
3737

3838
// Generate Tokens
39-
access_token,err:=utils.CreateToken(server.config.AccessTokenExpiresIn,user.ID,server.config.AccessTokenPrivateKey)
39+
access_token,err:=utils.CreateToken(authServer.config.AccessTokenExpiresIn,user.ID,authServer.config.AccessTokenPrivateKey)
4040
iferr!=nil {
4141

4242
returnnil,status.Errorf(codes.PermissionDenied,err.Error())
4343

4444
}
4545

46-
refresh_token,err:=utils.CreateToken(server.config.RefreshTokenExpiresIn,user.ID,server.config.RefreshTokenPrivateKey)
46+
refresh_token,err:=utils.CreateToken(authServer.config.RefreshTokenExpiresIn,user.ID,authServer.config.RefreshTokenPrivateKey)
4747
iferr!=nil {
4848
returnnil,status.Errorf(codes.PermissionDenied,err.Error())
4949
}

‎gapi/rpc_signup_user.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"google.golang.org/grpc/status"
1313
)
1414

15-
func (server*Server)SignUpUser(ctx context.Context,req*pb.SignUpUserInput) (*pb.GenericResponse,error) {
15+
func (authServer*AuthServer)SignUpUser(ctx context.Context,req*pb.SignUpUserInput) (*pb.GenericResponse,error) {
1616
ifreq.GetPassword()!=req.GetPasswordConfirm() {
1717
returnnil,status.Errorf(codes.InvalidArgument,"passwords do not match")
1818
}
@@ -24,7 +24,7 @@ func (server *Server) SignUpUser(ctx context.Context, req *pb.SignUpUserInput) (
2424
PasswordConfirm:req.GetPasswordConfirm(),
2525
}
2626

27-
newUser,err:=server.authService.SignUpUser(&user)
27+
newUser,err:=authServer.authService.SignUpUser(&user)
2828

2929
iferr!=nil {
3030
ifstrings.Contains(err.Error(),"email already exist") {
@@ -40,7 +40,7 @@ func (server *Server) SignUpUser(ctx context.Context, req *pb.SignUpUserInput) (
4040
verificationCode:=utils.Encode(code)
4141

4242
// Update User in Database
43-
server.userService.UpdateUserById(newUser.ID.Hex(),"verificationCode",verificationCode)
43+
authServer.userService.UpdateUserById(newUser.ID.Hex(),"verificationCode",verificationCode)
4444

4545
varfirstName=newUser.Name
4646

@@ -50,7 +50,7 @@ func (server *Server) SignUpUser(ctx context.Context, req *pb.SignUpUserInput) (
5050

5151
// 👇 Send Email
5252
emailData:= utils.EmailData{
53-
URL:server.config.Origin+"/verifyemail/"+code,
53+
URL:authServer.config.Origin+"/verifyemail/"+code,
5454
FirstName:firstName,
5555
Subject:"Your account verification code",
5656
}

‎gapi/rpc_verify_user.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import (
1111
"google.golang.org/grpc/status"
1212
)
1313

14-
func (server*Server)VerifyEmail(ctx context.Context,req*pb.VerifyEmailRequest) (*pb.GenericResponse,error) {
14+
func (authServer*AuthServer)VerifyEmail(ctx context.Context,req*pb.VerifyEmailRequest) (*pb.GenericResponse,error) {
1515
code:=req.GetVerificationCode()
1616

1717
verificationCode:=utils.Encode(code)
1818

1919
query:= bson.D{{Key:"verificationCode",Value:verificationCode}}
2020
update:= bson.D{{Key:"$set",Value: bson.D{{Key:"verified",Value:true}, {Key:"updated_at",Value:time.Now()}}}, {Key:"$unset",Value: bson.D{{Key:"verificationCode",Value:""}}}}
21-
result,err:=server.userCollection.UpdateOne(ctx,query,update)
21+
result,err:=authServer.userCollection.UpdateOne(ctx,query,update)
2222
iferr!=nil {
2323
returnnil,status.Errorf(codes.Internal,err.Error())
2424
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp