-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from quangdangfit/protobuf
Protobuf
- Loading branch information
Showing
16 changed files
with
1,454 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package grpc | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
|
||
"github.com/quangdangfit/gocommon/logger" | ||
"github.com/quangdangfit/gocommon/redis" | ||
"github.com/quangdangfit/gocommon/validation" | ||
"google.golang.org/grpc" | ||
"gorm.io/gorm" | ||
|
||
userGRPC "goshop/internal/user/port/grpc" | ||
"goshop/pkg/config" | ||
"goshop/pkg/middleware" | ||
) | ||
|
||
type Server struct { | ||
engine *grpc.Server | ||
cfg *config.Schema | ||
validator validation.Validation | ||
db *gorm.DB | ||
cache redis.IRedis | ||
} | ||
|
||
func NewServer(validator validation.Validation, db *gorm.DB, cache redis.IRedis) *Server { | ||
interceptor := middleware.NewAuthInterceptor(config.AuthIgnoreMethods) | ||
|
||
grpcServer := grpc.NewServer( | ||
grpc.ChainUnaryInterceptor( | ||
interceptor.Unary(), | ||
), | ||
) | ||
|
||
return &Server{ | ||
engine: grpcServer, | ||
cfg: config.GetConfig(), | ||
validator: validator, | ||
db: db, | ||
cache: cache, | ||
} | ||
} | ||
|
||
func (s Server) Run() error { | ||
userGRPC.RegisterHandlers(s.engine, s.db, s.validator) | ||
|
||
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", s.cfg.GrpcPort)) | ||
logger.Info("GRPC server is listening on PORT: ", s.cfg.GrpcPort) | ||
if err != nil { | ||
logger.Error("Failed to listen: ", err) | ||
return err | ||
} | ||
|
||
// Start grpc server | ||
err = s.engine.Serve(lis) | ||
if err != nil { | ||
logger.Fatal("Failed to serve grpc: ", err) | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package grpc | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/quangdangfit/gocommon/logger" | ||
|
||
"goshop/internal/user/dto" | ||
"goshop/internal/user/service" | ||
"goshop/pkg/utils" | ||
pb "goshop/proto/gen/go/user" | ||
) | ||
|
||
type UserHandler struct { | ||
pb.UnimplementedUserServiceServer | ||
|
||
service service.IUserService | ||
} | ||
|
||
func NewUserHandler(service service.IUserService) *UserHandler { | ||
return &UserHandler{ | ||
service: service, | ||
} | ||
} | ||
|
||
func (h *UserHandler) Login(ctx context.Context, req *pb.LoginReq) (*pb.LoginRes, error) { | ||
user, accessToken, refreshToken, err := h.service.Login(ctx, &dto.LoginReq{ | ||
Email: req.Email, | ||
Password: req.Password, | ||
}) | ||
if err != nil { | ||
logger.Error("Failed to register ", err) | ||
return nil, err | ||
} | ||
|
||
var res pb.LoginRes | ||
utils.Copy(&res.User, &user) | ||
res.AccessToken = accessToken | ||
res.FreshToken = refreshToken | ||
return &res, nil | ||
} | ||
|
||
func (h *UserHandler) Register(ctx context.Context, req *pb.RegisterReq) (*pb.RegisterRes, error) { | ||
user, err := h.service.Register(ctx, &dto.RegisterReq{ | ||
Email: req.Email, | ||
Password: req.Password, | ||
}) | ||
if err != nil { | ||
logger.Error("Failed to register ", err) | ||
return nil, err | ||
} | ||
|
||
var res pb.RegisterRes | ||
utils.Copy(&res.User, &user) | ||
return &res, nil | ||
} | ||
|
||
func (h *UserHandler) GetMe(ctx context.Context, _ *pb.GetMeReq) (*pb.GetMeRes, error) { | ||
userID, _ := ctx.Value("userId").(string) | ||
user, err := h.service.GetUserByID(ctx, userID) | ||
if err != nil { | ||
logger.Error("Failed to register ", err) | ||
return nil, err | ||
} | ||
|
||
var res pb.GetMeRes | ||
utils.Copy(&res.User, &user) | ||
return &res, nil | ||
} | ||
|
||
func (h *UserHandler) RefreshToken(ctx context.Context, req *pb.RefreshTokenReq) (*pb.RefreshTokenRes, error) { | ||
userID, _ := ctx.Value("userId").(string) | ||
accessToken, err := h.service.RefreshToken(ctx, userID) | ||
if err != nil { | ||
logger.Error("Failed to register ", err) | ||
return nil, err | ||
} | ||
|
||
res := pb.RefreshTokenRes{ | ||
AccessToken: accessToken, | ||
} | ||
return &res, nil | ||
} | ||
|
||
func (h *UserHandler) ChangePassword(ctx context.Context, req *pb.ChangePasswordReq) (*pb.ChangePasswordRes, error) { | ||
userID, _ := ctx.Value("userId").(string) | ||
err := h.service.ChangePassword(ctx, userID, &dto.ChangePasswordReq{ | ||
Password: req.Password, | ||
NewPassword: req.NewPassword, | ||
}) | ||
if err != nil { | ||
logger.Error("Failed to register ", err) | ||
return nil, err | ||
} | ||
|
||
return &pb.ChangePasswordRes{}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package grpc | ||
|
||
import ( | ||
"github.com/quangdangfit/gocommon/validation" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/reflection" | ||
"gorm.io/gorm" | ||
|
||
"goshop/internal/user/repository" | ||
"goshop/internal/user/service" | ||
pb "goshop/proto/gen/go/user" | ||
) | ||
|
||
func RegisterHandlers(svr *grpc.Server, db *gorm.DB, validator validation.Validation) { | ||
userRepo := repository.NewUserRepository(db) | ||
userSvc := service.NewUserService(validator, userRepo) | ||
userHandler := NewUserHandler(userSvc) | ||
|
||
pb.RegisterUserServiceServer(svr, userHandler) | ||
reflection.Register(svr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.