Skip to content

Commit

Permalink
Merge pull request #4 from hafizxd/ft/grpc
Browse files Browse the repository at this point in the history
add grpc protobuf
  • Loading branch information
hafizxd committed Feb 19, 2024
2 parents d248577 + 5831732 commit d1b1ff0
Show file tree
Hide file tree
Showing 38 changed files with 1,294 additions and 64 deletions.
17 changes: 17 additions & 0 deletions .idea/protoeditor.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,22 @@ server:
go run main.go

mock:
mockgen -package mockdb -destination db/mock/store.go github.com/hafizxd/simple_bank/db/sqlc Store
mockgen -package mockdb -destination db/mock/store.go github.com/hafizxd/micro-bank/db/sqlc Store

db_docs:
dbdocs build doc/db.dbml

db_schema:
dbml2sql --postgres -o doc/schema.sql doc/db.dbml

.PHONY:
postgres postgresstart createdb dropdb migrateup migratedown sqlc test server mock db_docs db_schema
proto:
del /s pb
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: postgres postgresstart createdb dropdb migrateup migratedown sqlc test server mock db_docs db_schema proto evans
4 changes: 2 additions & 2 deletions api/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"database/sql"
"errors"
"github.com/gin-gonic/gin"
db "github.com/hafizxd/simple_bank/db/sqlc"
"github.com/hafizxd/simple_bank/token"
db "github.com/hafizxd/micro-bank/db/sqlc"
"github.com/hafizxd/micro-bank/token"
"github.com/lib/pq"
"net/http"
)
Expand Down
8 changes: 4 additions & 4 deletions api/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"encoding/json"
"fmt"
"github.com/golang/mock/gomock"
mockdb "github.com/hafizxd/simple_bank/db/mock"
db "github.com/hafizxd/simple_bank/db/sqlc"
"github.com/hafizxd/simple_bank/token"
"github.com/hafizxd/simple_bank/util"
mockdb "github.com/hafizxd/micro-bank/db/mock"
db "github.com/hafizxd/micro-bank/db/sqlc"
"github.com/hafizxd/micro-bank/token"
"github.com/hafizxd/micro-bank/util"
"github.com/stretchr/testify/require"
"io"
"net/http"
Expand Down
4 changes: 2 additions & 2 deletions api/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package api

import (
"github.com/gin-gonic/gin"
db "github.com/hafizxd/simple_bank/db/sqlc"
"github.com/hafizxd/simple_bank/util"
db "github.com/hafizxd/micro-bank/db/sqlc"
"github.com/hafizxd/micro-bank/util"
"github.com/stretchr/testify/require"
"os"
"testing"
Expand Down
2 changes: 1 addition & 1 deletion api/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/hafizxd/simple_bank/token"
"github.com/hafizxd/micro-bank/token"
"net/http"
"strings"
)
Expand Down
2 changes: 1 addition & 1 deletion api/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package api
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/hafizxd/simple_bank/token"
"github.com/hafizxd/micro-bank/token"
"github.com/stretchr/testify/require"
"net/http"
"net/http/httptest"
Expand Down
6 changes: 3 additions & 3 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
db "github.com/hafizxd/simple_bank/db/sqlc"
"github.com/hafizxd/simple_bank/token"
"github.com/hafizxd/simple_bank/util"
db "github.com/hafizxd/micro-bank/db/sqlc"
"github.com/hafizxd/micro-bank/token"
"github.com/hafizxd/micro-bank/util"
)

type Server struct {
Expand Down
4 changes: 2 additions & 2 deletions api/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
db "github.com/hafizxd/simple_bank/db/sqlc"
"github.com/hafizxd/simple_bank/token"
db "github.com/hafizxd/micro-bank/db/sqlc"
"github.com/hafizxd/micro-bank/token"
"net/http"
)

Expand Down
4 changes: 2 additions & 2 deletions api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"database/sql"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
db "github.com/hafizxd/simple_bank/db/sqlc"
"github.com/hafizxd/simple_bank/util"
db "github.com/hafizxd/micro-bank/db/sqlc"
"github.com/hafizxd/micro-bank/util"
"github.com/lib/pq"
"net/http"
"time"
Expand Down
6 changes: 3 additions & 3 deletions api/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/golang/mock/gomock"
mockdb "github.com/hafizxd/simple_bank/db/mock"
db "github.com/hafizxd/simple_bank/db/sqlc"
"github.com/hafizxd/simple_bank/util"
mockdb "github.com/hafizxd/micro-bank/db/mock"
db "github.com/hafizxd/micro-bank/db/sqlc"
"github.com/hafizxd/micro-bank/util"
"github.com/stretchr/testify/require"
"io"
"net/http"
Expand Down
2 changes: 1 addition & 1 deletion api/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package api

import (
"github.com/go-playground/validator/v10"
"github.com/hafizxd/simple_bank/util"
"github.com/hafizxd/micro-bank/util"
)

var validCurrency validator.Func = func(fieldLevel validator.FieldLevel) bool {
Expand Down
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
TOKEN_DURATION=15m
REFRESH_TOKEN_DURATION=24h
4 changes: 2 additions & 2 deletions db/mock/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion db/sqlc/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package db
import (
"context"
"database/sql"
"github.com/hafizxd/simple_bank/util"
"github.com/hafizxd/micro-bank/util"
"testing"
"time"

Expand Down
2 changes: 1 addition & 1 deletion db/sqlc/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package db
import (
"context"
"database/sql"
"github.com/hafizxd/simple_bank/util"
"github.com/hafizxd/micro-bank/util"
"testing"
"time"

Expand Down
2 changes: 1 addition & 1 deletion db/sqlc/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package db

import (
"database/sql"
"github.com/hafizxd/simple_bank/util"
"github.com/hafizxd/micro-bank/util"
_ "github.com/lib/pq"
"log"
"os"
Expand Down
2 changes: 1 addition & 1 deletion db/sqlc/transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package db
import (
"context"
"database/sql"
"github.com/hafizxd/simple_bank/util"
"github.com/hafizxd/micro-bank/util"
"testing"
"time"

Expand Down
2 changes: 1 addition & 1 deletion db/sqlc/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package db

import (
"context"
"github.com/hafizxd/simple_bank/util"
"github.com/hafizxd/micro-bank/util"
"github.com/stretchr/testify/require"
"testing"
"time"
Expand Down
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/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),
}
}
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"
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
}
65 changes: 65 additions & 0 deletions gapi/rpc_login_user.go
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
}
31 changes: 31 additions & 0 deletions gapi/server.go
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
}
Loading

0 comments on commit d1b1ff0

Please sign in to comment.