A slightly opinionated HTTP API boilerplate with the Go programming language, following (some) Clean Architecture principles.
- HTTP server with net/http;
- Testing suites, assertions and mocks with testify;
- Live reload with air;
- Logging with zerolog;
- Configuration with viper;
- PostgreSQL database connection with pgx and sqlx
- Pre-configured CI job with Github Actions;
- JWT authentication (under the
with-auth
branch)
.
├── cmd
│ └── api <== Application entrypoint
├── config <== Configurations setup
├── docs <== Auto-generated OpenAPI documents
├── internal
│ ├── domain <== Domain objects with their own contracts and business logic
│ ├── infra <== Everything related to the infrastructure layer, such as databases, web handlers, queues etc
│ │ ├── database
│ │ │ ├── models <== Objects representing database entities
│ │ │ └── repositories <== Database operations and queries
│ │ ├── gateways <== Objects that encapsulates access to infra resources, following an interface defined by each use-case
│ │ └── web <== Everything related to the HTTP REST API
│ │ ├── handlers <== "Controllers"
│ │ │ └── dto <== Input and output objects
│ │ └── middlewares <== Request middlewares
│ ├── pkg <== Shared code that doesn't contain business logic, but are needed to support other packages
│ │ ├── apierror <== Error returned to the API client
│ │ ├── apperror <== Internal error object
│ │ ├── di <== Dependency injection resolver, where everything is glued together
│ │ └── logger <== Logging utilities
│ ├── tests <== Testing utilities
│ │ ├── fixtures <== Pre-built objects for test cases
│ │ └── mocks <== Methods mocks for necessary packages
│ └── usecases <== Main business rules
│ └── counter
├── migrations <== Database migrations generated with `migrate`
├── scripts <== Utilitarian shell scripts
- Create .env file
cp .env.example .env
- Rename packages to your project's name
make rename-pkgs
- Run setup script
make setup
To start a local server with live-reload and all necessary containers, execute:
make run
To execute all test suites and get a coverage report at the end, just run:
make test
There's a Dockerfile.prod
included with the project to build an optimized image based on distroless, so you just need to adapt it for your needs and publish to your desired registry.
# This should be set by your CI/CD system
export BUILD_ID="$(uuidgen)"
# Building the image
docker build . \
-t mathcale/go-api-boilerplate \
-f Dockerfile.prod \
--build-arg BUILD_ID
# Clean intermediate images
docker image prune \
--filter label=stage=builder \
--filter label=build=$BUILD_ID
By running the following command, the application will be compiled and outputted to the bin
directory.
make build
- Add database connection
- Add logging middleware
- Add Github Actions CI workflow
- Add database
- Add authentication branch
- Add OpenAPI specs on web handlers
- Improve project structure documentation