Basic golang boilerplate for backend projects.
Key features:
- cobra - CLI interactions
- viper - Configuration management
- http.ServeMux is used as router (pluggable)
- uber dig is used as DI framework
- for small projects it may make sense to setup dependencies manually
slog
is used for logs- slog-http is used to produce access logs
- testify and mockery are used for tests
- gow is used to watch and restart tests or server
To be added:
- Docker
- Examples of APIs
-
Clone the repo with a new name
-
Replace module name with desired one. Example:
find . -name "*.go" -o -name "go.mod" | xargs sed -i 's|github.com/gemyago/golang-backend-boilerplate|<YOUR-MODULE-PATH>|g';
Note: on osx you may have to install and use gnu sed. In such case you may need to replace
sed
withgsed
above.
- cmd/server is a main entrypoint to start API server
- internal/api/http - includes http routes related stuff
- internal/api/http/routes - add new routes here and register in handler.go
internal/app
- is assumed to include application layer code (e.g business logic). Examples to be added.internal/services
- lower level components are supposed to be here (e.g database access layer e.t.c). Examples to be added.
Please have the following tools installed:
Install/Update dependencies:
# Install
go mod download
make tools
# Update:
go get -u ./... && go mod tidy
Run all lint and tests:
make lint
make test
Run specific tests:
# Run once
go test -v ./internal/api/http/routes/ --run TestHealthCheckRoutes
# Run same test multiple times
# This is useful for tests that are flaky
go test -v -count=5 ./internal/api/http/routes/ --run TestHealthCheckRoutes
# Run and watch
gow test -v ./internal/api/http/routes/ --run TestHealthCheckRoutes
# Regular mode
go run ./cmd/server/
# Watch mode (double ^C to stop)
gow run ./cmd/server/