Skip to content

moukoublen/goboilerplate

Repository files navigation

Go service boilerplate

The boilerplate consists of:

  • Go code with chi http router, zerolog logging, knadh/koanf config library and testify. Basic code infra with http setup and request and response logger (configurable), logging setup, config setup and graceful shutdown.
  • Makefile targets for local installation of external tools (staticcheck, golangci-lint, goimports, gofumpt, gojq, air).
  • Makefile targets for linting (installing all necessary tools locally), building, testing, and local run (docker compose or native).
  • GitHub Actions for linter checks and tests on each pr.
  • Dependabot setup for updating dependencies.
  • Visual Studio Code settings, tasks and launch configuration for building, debugging and testing the code.
  • Docker compose and development Dockerfile with hot-rebuild (using air) and dlv debug server.
  • Production Dockerfile

How to use

  1. Click Use this template from this repo github page, and choose your destination repo/name (e.g. github.com/glendale/service)
  2. Clone your repo locally and run ./scripts/rename giving your new package name and main cmd name. E.g. ./scripts/rename github.com/glendale/service service.
  3. Commit the renaming and you are ready to start.

Go package and file structure

Every local package that collides with a golang std package is named with x postfix. For example .../internal/httpx or .../internal/logx. By doing this cheat you can avoid putting aliases when you need both of those and you can take advantage of package autocomplete/autoimport features more easily than having colliding names.

Package Description
cmd/*/main.go main function that is intended to glue together the most core level components like: configuration, http server and router, logs initialization, db connections (if any), the App and finally the Main struct (/internal/exec.go) that handles the long running / signal handling / graceful shutdown of the service.
internal/exec.go the Main struct that wraps the long running / signal handling / graceful shutdown of the service
internal/config this package contain the initialization of koanf config.
internal/httpx this package contains: the setup of the chi router, some helpers functions for parsing/writing http request and http response and some middlewares like for logging each request/response.
internal/logx this package contains the setup/init function for zerolog logger.
Folder/File Description
deployments this folder is intended to hold everything regarding deployment (e.g. helm/kubernetes etc). Inside compose directory a docker-compose.yml file is included that is intended for local development.
build/docker this folder contains production-like docker file as well as a local development one.

Make file targets

Build, Debug and Local Deploy Targets

build

Builds all binaries under ./cmd (one for each directory) and outputs the binaries into ./output folder using this patter ./cmd/<directory name> -> ./output/<directory name>.


cmd.<directory name>

Builds a specific cmd directory <directory name> and outputs the binary into ./output folder using this pattern ./cmd/<directory name> -> ./output/<directory name>.


clean

Deletes ./output folder.


compose-up

Deploys (rebuilds and recreates) the docker compose file deployments/compose/docker-compose.yml in attached mode. This docker compose file is intended for local development.
It uses the docker file build/docker/debug.Dockerfile and runs air target (see below), including hot-reload (rebuild on changes) and debug server.


compose-down

Stops (if started) the containers specified by the docker compose file deployments/compose/docker-compose.yml removes containers, cleans up the volumes and delete local docker images.


air

Installs (if needed) air under TOOLS_BIN folder (default is ./.tools/bin) and runs air -c .air.toml.
Air watches for code file changes and rebuilds the binary according to the configuration .air.toml.
Current air configuration executes cmd.goboilerplate target (on each file change) and then runs the ./build/dlv that starts the debug server (dlv exec) with the produced binary.


image

Builds the docker image using the docker file ./build/docker/Dockerfile.
This docker file is intended to be used as a production image.
Image name and tag are specified by IMAGE_NAME and IMAGE_TAG. Default values can be overwritten during execution (eg make IMAGE_NAME=myimage IMAGE_TAG=1.0.0 image).


Util Targets

mod

Runs go mod tidy and verify.


vendor

Runs go mod vendor that downloads all dependencies into ./vendor folder.


env

Prints information regarding the local environment. More specifically: go env, all go packages and folders, and the specified tools bin directory.


git-reset

Full hard reset to HEAD. Cleans up all untracked files and restore all staged and un-staged changes.


Tests and Linters Targets

test

Runs go test with race conditions and prints cover report.


tools

Installs (if needed) all tools (goimports, staticcheck, gofumpt, etc) under TOOLS_BIN folder (default is ./.tools/bin).


checks

Runs all default checks (vet, staticcheck, gofumpt, goimports, golangci-lint).


vet

Runs go vet.


gofumpt

Installs (if needed) gofumpt under TOOLS_BIN folder (default is ./.tools/bin) and runs gofumpt for each folder.
If gofumpt finds files that need fix/format it displays those files as list and the target fails (exit 1).


gofumpt.display

In case of gofumpt fails this target can be used to display the necessary changes. It runs gofumpt -d to display the needed changes that can be applied with make gofumpt.fix.


gofumpt.fix

Runs gofumpt -w to fix/format files (that need fix).


goimports

Installs (if needed) goimports under TOOLS_BIN folder (default is ./.tools/bin) and runs goimports for each folder.
If goimports finds files that need fix/format it displays those files as list and the target fails (exit 1).


goimports.display

In case of goimports fails this target can be used to display the necessary changes. It runs goimports -d to display the needed changes that can be applied with make goimports.fix.


goimports.fix

Runs goimports -w to fix files (that need fix).


staticcheck

Installs (if needed) staticcheck under TOOLS_BIN folder (default is ./.tools/bin) and runs staticcheck using all checks excluding ST1000.
If issues are found the target fails (due to staticcheck exit status).


golangci-lint

Installs (if needed) golangci-lint under TOOLS_BIN folder (default is ./.tools/bin) and runs golangci-lint.
If issues are found the target fails (due to golangci-lint exit status).


About

Template for Golang service boilerplate

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published