This repository contains all best practices for writing a go API that I learned over the years. It is meant to be cloned and renamed to get a headstart for building out your api. Or just to be pulled up while working on another project to find inspiration.
This repo uitlizes
- Command line flags using pflag
- Structured logging using zap
- HTTP framework using chi
- Database integration using gorm
- Database integration tests using dockertest
- API integration tests using gomock
- Documentation as code using http-swagger
And follows the following best practices:
- Log version on startup
- Graceful exit
- Staged Dockerfile
- Custom middlewares
- Swagger docs
To run this api you need a postgres instance. You can set the connection string via the DB_CONNECTION
environment
variable. The following command starts an empty database and calls runs the binary:
make run
Clone this repo with
git clone https://github.com/jonnylangefeld/go-api.git
And rename the module with
go mod edit -module <your module name>