/go-microservice-template

Template for Go REST API microservices, includes minimal dependencies, ready to use Dockerfile, graceful shutdown and more!

Primary LanguageGoMIT LicenseMIT

Go Reference Go Report Card

Description

go-microservice-template main purpose is to be a starting point for a REST API in Go. Reducing boilerplate writing and speeding up development of new microservices.

How to use

  1. Clone repository.
  2. Delete LICENSE and .git.
  3. Rename module name on go.mod.
  4. Rename the module reference of local packages inside server/ and main.go with the new module name. Example:
import ( 
    // rename with you module name
    "github.com/bruno-chavez/go-microservice-template/handlers"
    "github.com/julienschmidt/httprouter"
    "net/http"
    "os"
    "time"
)
  1. Rename the .env.example file to .env and customize the parameters accordingly.

Features

  • Fast and easy to use router with julienschmidt/httprouter.
  • Ready to use Dockerfile for building a secure and small Docker Image.
  • Graceful shutdown out of the box.
  • Environment variables loading from an .env file with the help of godotenv.
  • Dependency management with Go Modules.
  • Optional CORS handling with rs/cors, see server/server.go for how to enable it.

Continuous Integration

go-microservice-template currently uses GitHub Actions for running a CI pipeline containing linting, testing and building the binary and Docker image. Current workflow:

  • Lints with golangci-lint.
  • Run unit tests and prints the results with tparse.
  • Builds binary.
  • Builds Docker Image.

See ci.yml for more info.

Notes

  • Previously the module was called go-web-template, it lacked any kind of meaningful updates and was too opinionated for its purpose. I decided to complete refactor it and came up with the current iteration of go-microservice-template.

Contribute

Found a bug or an error? Post it in the issue tracker.

Want to add an awesome new feature? Fork this repository, add the feature on a new branch, send a pull request!

License

The MIT License (MIT) Copyright (c) 2021 Bruno Chavez