GolangでクリーンアーキテクチャのAPIを実装してみる
$ git clone git@github.com:qvtec/go-app.git
$ cd go-app
$ cp .env.example .env
$ make up
$ make migrate-up
# Login
curl -X POST -H "Content-Type: application/json" -d '{"email": "test@test.com", "password": "password"}' http://localhost:8080/api/v1/auth/login
# ALL
$ curl -X GET -H "Authorization: Bearer $TOKEN" http://localhost:8080/api/v1/users
# CREATE
$ curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" -d '{"name": "John Doe", "email": "john@example.com", "password": "password"}' http://localhost:8080/api/v1/users
# GetByID
$ curl -X GET -H "Authorization: Bearer $TOKEN" http://localhost:8080/api/v1/users/{user_id}
# UPDATE
$ curl -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" -d '{"name": "John Smith"}' http://localhost:8080/api/v1/users/{user_id}
# DELETE
$ curl -X DELETE -H "Authorization: Bearer $TOKEN" http://localhost:8080/api/v1/users/{user_id}
$ make test
├── build
│ ├── go
│ │ └── Dockerfile
│ └── mysql
│ ├── initdb.d
│ └── my.cnf
├── cmd
│ └── main.go
├── db
│ └── migrations
├── internal
│ ├── delivery // Frameworks & Drivers
│ │ └── http
│ │ ├── handler
│ │ ├── middleware
│ │ └── router
│ ├── domain // Entities
│ ├── repository // Interface
│ └── usecase // Use cases
├── mocks
├── pkg
│ └── db
├── go.mod
├── go.sum
├── docker-compose.yml
├── Makefile
└── README.md
$ go mod tidy
-
gin-gonic/gin
-
go-sql-driver/mysql
-
hot reload: air
-
auth: jwt
-
test: testify
-
mock: mockery
# Create up/down migration files
$ make migrate-create
# -- Add SQL
# Run migrate
$ make migrate
- delivery: ルーターとコントローラ呼び出し
- usecase: ビジネスロジック
- repository: DBデータ関連
- domain: データ構成
albumRepository := repository.NewAlbumRepository(db)
albumUseCase := usecase.NewAlbumUseCase(albumRepository)
albumHandler := httpHandler.NewAlbumHandler(albumUseCase)
httpRouter.SetupAlbumRouter(router, albumHandler)