Skip to content

This project includes a boilerplate structure for a golang project.

License

Notifications You must be signed in to change notification settings

gemyago/golang-backend-boilerplate

Repository files navigation

golang-backend-boilerplate

Tests Coverage

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

Starting a new project

  • 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 with gsed above.

Project structure

  • cmd/server is a main entrypoint to start API server
  • internal/api/http - includes http routes related stuff
  • 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.

Project Setup

Please have the following tools installed:

Install/Update dependencies:

# Install
go mod download
make tools

# Update:
go get -u ./... && go mod tidy

Lint and Tests

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

Run local API server:

# Regular mode
go run ./cmd/server/

# Watch mode (double ^C to stop)
gow run ./cmd/server/

About

This project includes a boilerplate structure for a golang project.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published