Boilerplate
Code structure
-
Project layout
-
Storage client:
- gorm => ORM library for Golang
- gen => Type-safe DAO API without interface{}
- rueidis => fast redis client
-
Development tools:
-
Tech:
-
Utils:
How to develop
$ go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
$ go install github.com/cosmtrek/air@latest
$ go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
$ go install github.com/swaggo/swag/cmd/swag@latest
Basic
# make sure you have dependencies up
$ docker compose up -d
# manual run
$ go run cmd/app/main.go
# hot reloading
$ air
Swagger
Please note that it's only for local environment
Served at: http://localhost:8080/swagger/index.html
$ swag init -d api,internal/request,internal/response,internal/database/entities -o ./api/docs -g ./http.go
Authentication
In local development, we send email in 'authorization' header
In development, uat and production environment, we use jwt auth
# local
$ curl localhost:8080/... -H 'authorization: admin@example.com'
# development, uat, production
$ curl localhost:8080/... -H 'authorization: Bearer ...'
Linter
❗️Please do this before raise the PRs ❗️
$ make lint
DAO Interface generation
❗️Please do this when you have updated the entities
❗️
$ go run cmd/gen/main.go
Migration
New migration, find out more on best practices here
$ migrate create -digits 6 -dir migrations -ext sql -seq 'MIGRATION_NAME'
For testing only, in the code we have auto-migration in place.
$ migrate -source file://migrations -database 'postgres://service:password@localhost:5432/book?sslmode=disable' up
$ migrate -source file://migrations -database 'postgres://service:password@localhost:5432/book?sslmode=disable' down -all
$ migrate -source file://migrations -database 'postgres://service:password@localhost:5432/book?sslmode=disable' drop -f
Test
# make sure you have dependencies up
$ docker compose up -d
$ make test