-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: add gRPC to serve gRPC api with create user and login user api
- Loading branch information
1 parent
cb7f7ca
commit 2873bf8
Showing
17 changed files
with
297 additions
and
104 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
DB_DRIVER=postgres | ||
DB_SOURCE=postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable | ||
SERVER_ADDRESS=0.0.0.0:8080 | ||
HTTP_SERVER_ADDRESS=0.0.0.0:8080 | ||
GRPC_SERVER_ADDRESS=0.0.0.0:50051 | ||
TOKEN_SYMMETRIC_KEY=12345678901234567890123456789012 | ||
TOKEN_DURATION=15m | ||
REFRESH_TOKEN_DURATION=24h |
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,17 @@ | ||
package gapi | ||
|
||
import ( | ||
db "github.com/cukhoaimon/SimpleBank/db/sqlc" | ||
"github.com/cukhoaimon/SimpleBank/pb" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
func convertUser(user db.User) *pb.User { | ||
return &pb.User{ | ||
Username: user.Username, | ||
FullName: user.FullName, | ||
Email: user.Email, | ||
PasswordChangedAt: timestamppb.New(user.PasswordChangedAt), | ||
CreatedAt: timestamppb.New(user.CreatedAt), | ||
} | ||
} |
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,44 @@ | ||
package gapi | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
db "github.com/cukhoaimon/SimpleBank/db/sqlc" | ||
"github.com/cukhoaimon/SimpleBank/pb" | ||
"github.com/cukhoaimon/SimpleBank/utils" | ||
"github.com/lib/pq" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
func (server *Server) CreateUser(ctx context.Context, req *pb.CreateUserRequest) (*pb.CreateUserResponse, error) { | ||
hashedPassword, err := utils.HashPassword(req.GetPassword()) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "fail to hash password: %s", err) | ||
} | ||
|
||
arg := db.CreateUserParams{ | ||
Username: req.GetUsername(), | ||
HashedPassword: hashedPassword, | ||
FullName: req.GetFullName(), | ||
Email: req.GetEmail(), | ||
} | ||
|
||
user, err := server.store.CreateUser(ctx, arg) | ||
if err != nil { | ||
var pqErr *pq.Error | ||
if errors.As(err, &pqErr) { | ||
switch pqErr.Code.Name() { | ||
case "unique_violation": | ||
return nil, status.Errorf(codes.AlreadyExists, "username already exists: %s", err) | ||
} | ||
} | ||
|
||
return nil, status.Errorf(codes.Internal, "fail to create user: %s", err) | ||
} | ||
|
||
rsp := &pb.CreateUserResponse{ | ||
User: convertUser(user), | ||
} | ||
return rsp, 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,66 @@ | ||
package gapi | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"errors" | ||
db "github.com/cukhoaimon/SimpleBank/db/sqlc" | ||
"github.com/cukhoaimon/SimpleBank/pb" | ||
"github.com/cukhoaimon/SimpleBank/utils" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
func (server *Server) LoginUser(ctx context.Context, req *pb.LoginUserRequest) (*pb.LoginUserResponse, error) { | ||
user, err := server.store.GetUser(ctx, req.GetUsername()) | ||
if err != nil { | ||
if errors.Is(err, sql.ErrNoRows) { | ||
return nil, status.Errorf(codes.NotFound, "user [%s] is not found in database", req.GetUsername()) | ||
} | ||
|
||
return nil, status.Errorf(codes.Internal, "fail to get user [%s] from database", req.GetUsername()) | ||
} | ||
|
||
err = utils.CheckPassword(req.Password, user.HashedPassword) | ||
if err != nil { | ||
return nil, status.Error(codes.Unauthenticated, "wrong password") | ||
|
||
} | ||
|
||
token, accessTokenPayload, err := server.tokenMaker.CreateToken(req.Username, server.config.TokenDuration) | ||
if err != nil { | ||
return nil, status.Error(codes.Internal, "fail to create access token") | ||
} | ||
|
||
refreshToken, refreshTokenPayload, err := server.tokenMaker.CreateToken(req.Username, server.config.RefreshTokenDuration) | ||
if err != nil { | ||
return nil, status.Error(codes.Internal, "fail to create refresh access token") | ||
} | ||
|
||
arg := db.CreateSessionParams{ | ||
ID: accessTokenPayload.Id, | ||
Username: accessTokenPayload.Username, | ||
RefreshToken: refreshToken, | ||
UserAgent: "", | ||
ClientIp: "", | ||
IsBlocked: false, | ||
ExpiresAt: refreshTokenPayload.ExpiredAt, | ||
} | ||
|
||
session, err := server.store.CreateSession(ctx, arg) | ||
if err != nil { | ||
return nil, status.Error(codes.Internal, "fail to create session") | ||
} | ||
|
||
res := &pb.LoginUserResponse{ | ||
SessionId: session.ID.String(), | ||
AccessToken: token, | ||
AccessTokenExpiresAt: timestamppb.New(accessTokenPayload.ExpiredAt), | ||
RefreshToken: refreshToken, | ||
RefreshTokenExpiresAt: timestamppb.New(refreshTokenPayload.ExpiredAt), | ||
User: convertUser(user), | ||
} | ||
|
||
return res, 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,32 @@ | ||
package gapi | ||
|
||
import ( | ||
db "github.com/cukhoaimon/SimpleBank/db/sqlc" | ||
"github.com/cukhoaimon/SimpleBank/pb" | ||
"github.com/cukhoaimon/SimpleBank/token" | ||
"github.com/cukhoaimon/SimpleBank/utils" | ||
) | ||
|
||
// Server serves gRPC request | ||
type Server struct { | ||
pb.UnimplementedSimpleBankServer | ||
config utils.Config | ||
tokenMaker token.Maker | ||
store db.Store | ||
} | ||
|
||
// NewServer will return new gRPC server | ||
func NewServer(store db.Store, config utils.Config) (*Server, error) { | ||
maker, err := token.NewPasetoMaker(config.TokenSymmetricKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
server := &Server{ | ||
store: store, | ||
tokenMaker: maker, | ||
config: config, | ||
} | ||
|
||
return server, 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.