Skip to content

Commit

Permalink
add grpc service to create and login user (techschool#32)
Browse files Browse the repository at this point in the history
* add grpc service to create and login user

* fix indentation

* update go 1.18 in github action test

Co-authored-by: phamlequang <phamlequang@gmail.com>
techschool and phamlequang authored Apr 10, 2022
1 parent 6d1d65f commit 5174eb1
Showing 21 changed files with 1,286 additions and 291 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.16
go-version: ^1.18
id: go

- name: Check out code into the Go module directory
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build stage
FROM golang:1.16-alpine3.13 AS builder
FROM golang:1.18-alpine3.13 AS builder
WORKDIR /app
COPY . .
RUN go build -o main main.go
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -45,4 +45,13 @@ server:
mock:
mockgen -package mockdb -destination db/mock/store.go github.com/techschool/simplebank/db/sqlc Store

.PHONY: network postgres createdb dropdb migrateup migratedown migrateup1 migratedown1 db_docs db_schema sqlc test server mock
proto:
rm -f pb/*.go
protoc --proto_path=proto --go_out=pb --go_opt=paths=source_relative \
--go-grpc_out=pb --go-grpc_opt=paths=source_relative \
proto/*.proto

evans:
evans --host localhost --port 9090 -r repl

.PHONY: network postgres createdb dropdb migrateup migratedown migrateup1 migratedown1 db_docs db_schema sqlc test server mock proto evans
3 changes: 2 additions & 1 deletion app.env
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
ACCESS_TOKEN_DURATION=15m
REFRESH_TOKEN_DURATION=24h
17 changes: 17 additions & 0 deletions gapi/converter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package gapi

import (
db "github.com/techschool/simplebank/db/sqlc"
"github.com/techschool/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),
}
}
42 changes: 42 additions & 0 deletions gapi/rpc_create_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package gapi

import (
"context"

"github.com/lib/pq"
db "github.com/techschool/simplebank/db/sqlc"
"github.com/techschool/simplebank/pb"
"github.com/techschool/simplebank/util"
"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
}
67 changes: 67 additions & 0 deletions gapi/rpc_login_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package gapi

import (
"context"
"database/sql"

db "github.com/techschool/simplebank/db/sqlc"
"github.com/techschool/simplebank/pb"
"github.com/techschool/simplebank/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")
}
return nil, status.Errorf(codes.Internal, "failed to find user")
}

err = util.CheckPassword(req.Password, user.HashedPassword)
if err != nil {
return nil, status.Errorf(codes.NotFound, "incorrect password")
}

accessToken, accessPayload, err := server.tokenMaker.CreateToken(
user.Username,
server.config.AccessTokenDuration,
)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to create access token")
}

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")
}

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")
}

rsp := &pb.LoginUserResponse{
User: convertUser(user),
SessionId: session.ID.String(),
AccessToken: accessToken,
RefreshToken: refreshToken,
AccessTokenExpiresAt: timestamppb.New(accessPayload.ExpiredAt),
RefreshTokenExpiresAt: timestamppb.New(refreshPayload.ExpiredAt),
}
return rsp, nil
}
34 changes: 34 additions & 0 deletions gapi/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package gapi

import (
"fmt"

db "github.com/techschool/simplebank/db/sqlc"
"github.com/techschool/simplebank/pb"
"github.com/techschool/simplebank/token"
"github.com/techschool/simplebank/util"
)

// Server serves gRPC requests for our banking service.
type Server struct {
pb.UnimplementedSimpleBankServer
config util.Config
store db.Store
tokenMaker token.Maker
}

// NewServer creates a new gRPC server.
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 maker: %w", err)
}

server := &Server{
config: config,
store: store,
tokenMaker: tokenMaker,
}

return server, nil
}
51 changes: 42 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,21 +1,54 @@
module github.com/techschool/simplebank

go 1.16
go 1.18

require (
github.com/aead/chacha20poly1305 v0.0.0-20201124145622-1a5aba2a8b29
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/gin-gonic/gin v1.7.7
github.com/go-playground/validator/v10 v10.10.0
github.com/go-playground/validator/v10 v10.10.1
github.com/golang/mock v1.6.0
github.com/google/uuid v1.3.0
github.com/lib/pq v1.10.4
github.com/lib/pq v1.10.5
github.com/o1egl/paseto v1.0.0
github.com/spf13/afero v1.8.1 // indirect
github.com/spf13/viper v1.10.1
github.com/stretchr/testify v1.7.0
github.com/ugorji/go v1.2.6 // indirect
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a // indirect
gopkg.in/ini.v1 v1.66.3 // indirect
github.com/stretchr/testify v1.7.1
golang.org/x/crypto v0.0.0-20220408190544-5352b0902921
google.golang.org/grpc v1.43.0
google.golang.org/protobuf v1.28.0
)

require (
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect
github.com/aead/poly1305 v0.0.0-20180717145839-3fee0db0b635 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
301 changes: 25 additions & 276 deletions go.sum

Large diffs are not rendered by default.

33 changes: 32 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -3,11 +3,16 @@ package main
import (
"database/sql"
"log"
"net"

_ "github.com/lib/pq"
"github.com/techschool/simplebank/api"
db "github.com/techschool/simplebank/db/sqlc"
"github.com/techschool/simplebank/gapi"
"github.com/techschool/simplebank/pb"
"github.com/techschool/simplebank/util"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)

func main() {
@@ -22,12 +27,38 @@ func main() {
}

store := db.NewStore(conn)
runGrpcServer(config, store)
}

func runGrpcServer(config util.Config, store db.Store) {
server, err := gapi.NewServer(config, store)
if err != nil {
log.Fatal("cannot create server:", err)
}

grpcServer := grpc.NewServer()
pb.RegisterSimpleBankServer(grpcServer, server)
reflection.Register(grpcServer)

listener, err := net.Listen("tcp", config.GRPCServerAddress)
if err != nil {
log.Fatal("cannot create listener")
}

log.Printf("start gRPC server at %s", listener.Addr().String())
err = grpcServer.Serve(listener)
if err != nil {
log.Fatal("cannot start gRPC server")
}
}

func runGinServer(config util.Config, store db.Store) {
server, err := api.NewServer(config, store)
if err != nil {
log.Fatal("cannot create server:", err)
}

err = server.Start(config.ServerAddress)
err = server.Start(config.HTTPServerAddress)
if err != nil {
log.Fatal("cannot start server:", err)
}
240 changes: 240 additions & 0 deletions pb/rpc_create_user.pb.go
284 changes: 284 additions & 0 deletions pb/rpc_login_user.pb.go
86 changes: 86 additions & 0 deletions pb/service_simple_bank.pb.go
141 changes: 141 additions & 0 deletions pb/service_simple_bank_grpc.pb.go
192 changes: 192 additions & 0 deletions pb/user.pb.go
18 changes: 18 additions & 0 deletions proto/rpc_create_user.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
syntax = "proto3";

package pb;

import "user.proto";

option go_package = "github.com/techschool/simplebank/pb";

message CreateUserRequest {
string username = 1;
string full_name = 2;
string email = 3;
string password = 4;
}

message CreateUserResponse {
User user = 1;
}
22 changes: 22 additions & 0 deletions proto/rpc_login_user.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
syntax = "proto3";

package pb;

import "user.proto";
import "google/protobuf/timestamp.proto";

option go_package = "github.com/techschool/simplebank/pb";

message LoginUserRequest {
string username = 1;
string password = 2;
}

message LoginUserResponse {
User user = 1;
string session_id = 2;
string access_token = 3;
string refresh_token = 4;
google.protobuf.Timestamp access_token_expires_at = 5;
google.protobuf.Timestamp refresh_token_expires_at = 6;
}
13 changes: 13 additions & 0 deletions proto/service_simple_bank.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
syntax = "proto3";

package pb;

import "rpc_create_user.proto";
import "rpc_login_user.proto";

option go_package = "github.com/techschool/simplebank/pb";

service SimpleBank {
rpc CreateUser (CreateUserRequest) returns (CreateUserResponse) {}
rpc LoginUser (LoginUserRequest) returns (LoginUserResponse) {}
}
15 changes: 15 additions & 0 deletions proto/user.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
syntax = "proto3";

package pb;

import "google/protobuf/timestamp.proto";

option go_package = "github.com/techschool/simplebank/pb";

message User {
string username = 1;
string full_name = 2;
string email = 3;
google.protobuf.Timestamp password_changed_at = 4;
google.protobuf.Timestamp created_at = 5;
}
3 changes: 2 additions & 1 deletion util/config.go
Original file line number Diff line number Diff line change
@@ -11,7 +11,8 @@ import (
type Config struct {
DBDriver string `mapstructure:"DB_DRIVER"`
DBSource string `mapstructure:"DB_SOURCE"`
ServerAddress string `mapstructure:"SERVER_ADDRESS"`
HTTPServerAddress string `mapstructure:"HTTP_SERVER_ADDRESS"`
GRPCServerAddress string `mapstructure:"GRPC_SERVER_ADDRESS"`
TokenSymmetricKey string `mapstructure:"TOKEN_SYMMETRIC_KEY"`
AccessTokenDuration time.Duration `mapstructure:"ACCESS_TOKEN_DURATION"`
RefreshTokenDuration time.Duration `mapstructure:"REFRESH_TOKEN_DURATION"`

0 comments on commit 5174eb1

Please sign in to comment.