A template for a go microservice with a PubSub connection together with protobuf.
For a service configured with an REST-API look at https://github.com/johan-lejdung/go-microservice-api-template
Features:
- Dependency injection
- Database connection
- Database migrations
- Fluentd logging
- PubSub setup with Protobuf
- Kubernetes compatible logging/liveness endpoints
- CricleCI + CodeCov configuration files
Start by forking this repository or just download the code.
Replace all occourances of johan-lejdung
with whatever your github username/organization is.
Replace all occourances of go-microservice-pubsub-template
with whatever you are calling your project.
Open up .env
and change the settings necessary.
Simple register an account at CircleCI and CodeCov, copy the CodeCov token to the environment variables on CircleCI like so:
CODECOV_TOKEN
= <TOKEN>
Add the following lines to ~/.bach_profile or ~/.zshrc (if using zsh)
export GOPATH=/Users/username/go
export PATH=$GOPATH/bin:$PATH
Where username is the username of your profile.
Then install dep:
brew install dep
Run this command in the correct folder:
dep ensure
Install mockery
go get github.com/vektra/mockery/.../
Generate files
go generate ./....
Run tests
go test ./...
The pubsub connection is found in the ps folder
and has the service it uses injected to it's structure.
Take a look in the consume
and produce
folders to check out how to subscribe and connect to topics.
Run a local pubsub instance in docker with docker-compose up
.
A generated protobuf file is in the goservice
packages. The raw proto file is round in root dir.
To generate a proto file install protoc
and execute protoc --go_out=./go/ ./<PROTO_FILE>
The database will automatically apply migrations if the variable in .env
called ENV
is either dev
or test
.
I am using https://github.com/mattes/migrate for database migrations.
Run a local database in docker with docker-compose up
.
I am using https://github.com/facebookgo/inject for dependency injection.
By injecting implementations of interfaces in the bootstrapApp
you can easily inject them in structs such as:
type TestStruct struct {
Variable pkg.InterfaceType `inject:""`
}
I have the vendor folder checked into the repo, for reproducibility.