Skip to content

Commit

Permalink
Merge pull request #4 from cukhoaimon/feat/refactor
Browse files Browse the repository at this point in the history
feat!: refactor all code base on clean architecture by Uncle Bob
  • Loading branch information
cukhoaimon authored Feb 12, 2024
2 parents cc717f4 + 554e83d commit 8736077
Show file tree
Hide file tree
Showing 112 changed files with 806 additions and 668 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DB_DRIVER=postgres
DB_SOURCE=postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable
MIGRATION_URL=file://db/migration
MIGRATION_URL=file://migration
HTTP_SERVER_ADDRESS=0.0.0.0:8080
GRPC_SERVER_ADDRESS=0.0.0.0:50051
TOKEN_SYMMETRIC_KEY=12345678901234567890123456789012
Expand Down
10 changes: 5 additions & 5 deletions .idea/sqldialects.xml

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

2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ WORKDIR /app
COPY --from=builder /app/main .
COPY .env .
COPY start.sh .
COPY db/migration ./db/migration
COPY migration ./db/migration

EXPOSE 8080
ENTRYPOINT [ "/app/start.sh" ]
28 changes: 14 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ dropdb:
docker exec -it postgres16 dropdb simple_bank

migrate-up:
migrate -path db/migration -database "postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable" -verbose up
migrate -path migration -database "postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable" -verbose up

migrate-down:
migrate -path db/migration -database "postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable" -verbose down
migrate -path migration -database "postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable" -verbose down

migrate-up1:
migrate -path db/migration -database "postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable" -verbose up 1
migrate -path migration -database "postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable" -verbose up 1

migrate-down1:
migrate -path db/migration -database "postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable" -verbose down 1
migrate -path migration -database "postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable" -verbose down 1

sqlc:
sqlc generate
Expand All @@ -26,22 +26,22 @@ test:
go test -v -cover ./...

server:
go run main.go
go run cmd/main.go

dbdocs:
dbdocs build doc/database.dbml
dbdocs build docs/database.dbml

mock:
mockgen -package mockdb -destination db/mock/store.go github.com/cukhoaimon/SimpleBank/db/sqlc Store
mockgen -package mockdb -destination internal/delivery/http/mock/store.go github.com/cukhoaimon/SimpleBank/internal/usecase/sqlc Store

proto:
del doc\swagger\*.swagger.json &
del pb\*.pb.go &
protoc --proto_path=proto --go_out=pb --go_opt paths=source_relative \
--go-grpc_out=pb --go-grpc_opt paths=source_relative \
--grpc-gateway_out=pb --grpc-gateway_opt paths=source_relative \
--openapiv2_out=doc/swagger --openapiv2_opt allow_merge=true,merge_file_name=simplebank \
proto/*.proto
del docs\swagger\*.swagger.json &
del internal\delivery\grpc\pb &
protoc --proto_path=internal/delivery/grpc/proto --go_out=internal/delivery/grpc/pb --go_opt paths=source_relative \
--go-grpc_out=internal/delivery/grpc/pb --go-grpc_opt paths=source_relative \
--grpc-gateway_out=internal/delivery/grpc/pb --grpc-gateway_opt paths=source_relative \
--openapiv2_out=docs/swagger --openapiv2_opt allow_merge=true,merge_file_name=simplebank \
internal/delivery/grpc/proto/*.proto

rerun_compose:
docker compose down &
Expand Down
35 changes: 0 additions & 35 deletions api/main_test.go

This file was deleted.

67 changes: 0 additions & 67 deletions api/server.go

This file was deleted.

18 changes: 18 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"github.com/cukhoaimon/SimpleBank/internal/app"
"github.com/cukhoaimon/SimpleBank/utils"
"log"
)

func main() {
// load config
config, err := utils.LoadConfig(".")
if err != nil {

log.Fatal(err.Error())
}

app.Run(config)
}
2 changes: 1 addition & 1 deletion doc/database.dbml → docs/database.dbml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Table users as U {

Table accounts as A {
id bigserial [pk]
owner varchar [r ef: > U.username, not null]
owner varchar [ref: > U.username, not null]
balance bigint [not null]
currency varchar [not null]
created_at timestamptz [not null, default: `now()`]
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tools/tools.go → framework/grpc/tools.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build tools
// +build tools

package tools
package grpc

import (
_ "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway"
Expand Down
32 changes: 0 additions & 32 deletions gapi/server.go

This file was deleted.

35 changes: 35 additions & 0 deletions internal/app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package app

import (
"database/sql"
db "github.com/cukhoaimon/SimpleBank/internal/usecase/sqlc"
"github.com/cukhoaimon/SimpleBank/pkg/grpc"
"github.com/cukhoaimon/SimpleBank/utils"
"github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/golang-migrate/migrate/v4/source/github"
_ "github.com/lib/pq"
"log"
)

// Run will run simple bank app
func Run(config utils.Config) {
conn, err := sql.Open(config.DBDriver, config.DBSource)
if err != nil {
log.Fatal("The open connection to database process was encountered an error", err)
}

driver, err := postgres.WithInstance(conn, &postgres.Config{})
if err != nil {
log.Fatal("Error when create postgres driver instance: ", err)
}

RunDBMigration(config.MigrationURL, config.PostgresDB, driver)

store := db.NewStore(conn)

go grpc.RunGatewayServer(store, config)
grpc.Run(store, config)
//http2.Run(store, config)
}
20 changes: 20 additions & 0 deletions internal/app/migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package app

import (
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database"
"log"
)

func RunDBMigration(sourceURL string, databaseName string, databaseInstance database.Driver) {
migration, err := migrate.NewWithDatabaseInstance(sourceURL, databaseName, databaseInstance)
if err != nil {
log.Fatal("fail to create migration instance: ", err)
}

if err = migration.Up(); err != nil && err.Error() != "no change" {
log.Fatal("fail to run migrate up: ", err)
}

log.Println("migration is successfully")
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package gapi

import (
db "github.com/cukhoaimon/SimpleBank/db/sqlc"
"github.com/cukhoaimon/SimpleBank/pb"
"github.com/cukhoaimon/SimpleBank/internal/delivery/grpc/pb"
db "github.com/cukhoaimon/SimpleBank/internal/usecase/sqlc"
"google.golang.org/protobuf/types/known/timestamppb"
)

Expand Down
File renamed without changes.
13 changes: 13 additions & 0 deletions internal/delivery/grpc/gapi/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package gapi

import (
db "github.com/cukhoaimon/SimpleBank/internal/usecase/sqlc"
"github.com/cukhoaimon/SimpleBank/pkg/token"
"github.com/cukhoaimon/SimpleBank/utils"
)

type Handler struct {
Config utils.Config
TokenMaker token.Maker
Store db.Store
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
grpcGatewayClientIP = "x-forwarded-host"
)

func (server *Server) extractMetadata(ctx context.Context) *Metadata {
func (handler *Handler) extractMetadata(ctx context.Context) *Metadata {
mtdt := &Metadata{}

if md, ok := metadata.FromIncomingContext(ctx); ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package gapi
import (
"context"
"errors"
db "github.com/cukhoaimon/SimpleBank/db/sqlc"
"github.com/cukhoaimon/SimpleBank/pb"
"github.com/cukhoaimon/SimpleBank/internal/delivery/grpc/pb"
db "github.com/cukhoaimon/SimpleBank/internal/usecase/sqlc"
"github.com/cukhoaimon/SimpleBank/internal/usecase/val"
"github.com/cukhoaimon/SimpleBank/utils"
"github.com/cukhoaimon/SimpleBank/val"
"github.com/lib/pq"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (server *Server) CreateUser(ctx context.Context, req *pb.CreateUserRequest) (*pb.CreateUserResponse, error) {
func (handler *Handler) CreateUser(ctx context.Context, req *pb.CreateUserRequest) (*pb.CreateUserResponse, error) {
violations := validateCreateUserRequest(req)
if violations != nil {
return nil, invalidArgumentError(violations)
Expand All @@ -31,7 +31,7 @@ func (server *Server) CreateUser(ctx context.Context, req *pb.CreateUserRequest)
Email: req.GetEmail(),
}

user, err := server.store.CreateUser(ctx, arg)
user, err := handler.Store.CreateUser(ctx, arg)
if err != nil {
var pqErr *pq.Error
if errors.As(err, &pqErr) {
Expand Down
Loading

0 comments on commit 8736077

Please sign in to comment.