-
Notifications
You must be signed in to change notification settings - Fork 0
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 #4 from hafizxd/ft/grpc
add grpc protobuf
- Loading branch information
Showing
38 changed files
with
1,294 additions
and
64 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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:9090 | ||
TOKEN_SYMMETRIC_KEY=12345678901234567890123456789012 | ||
TOKEN_DURATION=15m | ||
REFRESH_TOKEN_DURATION=24h |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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/hafizxd/micro-bank/db/sqlc" | ||
"github.com/hafizxd/micro-bank/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,42 @@ | ||
package gapi | ||
|
||
import ( | ||
"context" | ||
db "github.com/hafizxd/micro-bank/db/sqlc" | ||
"github.com/hafizxd/micro-bank/pb" | ||
"github.com/hafizxd/micro-bank/util" | ||
"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 := util.HashPassword(req.GetPassword()) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "failed 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 { | ||
if pqErr, ok := err.(*pq.Error); ok { | ||
switch pqErr.Code.Name() { | ||
case "unique_violation": | ||
return nil, status.Errorf(codes.AlreadyExists, "username already exists: %s", err) | ||
} | ||
} | ||
|
||
return nil, status.Errorf(codes.Internal, "failed 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,65 @@ | ||
package gapi | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
db "github.com/hafizxd/micro-bank/db/sqlc" | ||
"github.com/hafizxd/micro-bank/pb" | ||
"github.com/hafizxd/micro-bank/util" | ||
"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 err == sql.ErrNoRows { | ||
return nil, status.Errorf(codes.NotFound, "user not found: %s", err) | ||
} | ||
|
||
return nil, status.Errorf(codes.Internal, "failed to get user: %s", err) | ||
} | ||
|
||
err = util.CheckPassword(req.GetPassword(), user.HashedPassword) | ||
if err != nil { | ||
return nil, status.Errorf(codes.NotFound, "wrong password: %s", err) | ||
} | ||
|
||
accessToken, accessPayload, err := server.tokenMaker.CreateToken(req.GetUsername(), server.config.TokenDuration) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "failed to create access token: %s", err) | ||
} | ||
|
||
refreshToken, refreshPayload, err := server.tokenMaker.CreateToken( | ||
user.Username, | ||
server.config.RefreshTokenDuration, | ||
) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "failed to create refresh token: %s", err) | ||
} | ||
|
||
session, err := server.store.CreateSession(ctx, db.CreateSessionParams{ | ||
ID: refreshPayload.ID, | ||
Username: user.Username, | ||
RefreshToken: refreshToken, | ||
UserAgent: "", | ||
ClientIp: "", | ||
IsBlocked: false, | ||
ExpiresAt: refreshPayload.ExpiredAt, | ||
}) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "failed to create session: %s", err) | ||
} | ||
|
||
rsp := &pb.LoginUserResponse{ | ||
SessionId: session.ID.String(), | ||
AccessToken: accessToken, | ||
AccessTokenExpiresAt: timestamppb.New(accessPayload.ExpiredAt), | ||
RefreshToken: refreshToken, | ||
RefreshTokenExpiresAt: timestamppb.New(refreshPayload.ExpiredAt), | ||
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,31 @@ | ||
package gapi | ||
|
||
import ( | ||
"fmt" | ||
db "github.com/hafizxd/micro-bank/db/sqlc" | ||
"github.com/hafizxd/micro-bank/pb" | ||
"github.com/hafizxd/micro-bank/token" | ||
"github.com/hafizxd/micro-bank/util" | ||
) | ||
|
||
type Server struct { | ||
pb.UnimplementedMicroBankServer | ||
config util.Config | ||
store db.Store | ||
tokenMaker token.Maker | ||
} | ||
|
||
func NewServer(config util.Config, store db.Store) (*Server, error) { | ||
tokenMaker, err := token.NewPasetoMaker(config.TokenSymmetricKey) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot create token marker: %v", err) | ||
} | ||
|
||
server := &Server{ | ||
config: config, | ||
store: store, | ||
tokenMaker: tokenMaker, | ||
} | ||
|
||
return server, nil | ||
} |
Oops, something went wrong.