Template x go microservices
The microservice will be packaged in a docker image, to deploy use the provided configs in the deployments folder
- install go
Make sure to install the version indicated in the go.mod file
- Docker for building
- Edit
go.modwith the name (likegit.cdlan.net/<group>/<reponame>) of your project (and update all import statements) - Edit
serviceNamein server.go with the name of the service - Edit
app.imagein docker-compose.yaml - (optional) Uncomment all rows from .gitlab-ci.yml to enable pipelines
- Register project in sonarqube
- Edit .proto files in api folder
- Run this command to generate go code from the protos
make grpc- In grpc create a file for each service that you defined and implement the service servers and add a NewXYZServer() that return a pointer to the server
- In server.go register the newly created servers
We use mkdocs for documentation
- Update the content of mkdocs.yml for title and url
- Place md files inside the docs folder eventually in subfolders
- Run this command to build the container locally and serve it
make docsWe use testify for tests. Each package should have this two test suite defined:
type IntegrationTestSuite struct {
suite.Suite
}
type UnitTestSuite struct {
suite.Suite
}
func TestIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(IntegrationTestSuite))
}
func TestUnitTestSuite(t *testing.T) {
suite.Run(t, new(UnitTestSuite))
}- UnitTestSuite should contains all tests that can be run on code alone (no db, no ext services connections) these can always be run inside the CI pipeline
- IntegrationTestSuite contains all tests that should be run with some dependencies (db/ext-services/...)
To add a new test inside a suite:
func (suite *IntegrationTestSuite) TestCustomerGroupDAO_RmNonExistentResFromCustomerGroup() {
res := model.CustomerGroupResource{
CustomerGroupId: 99,
Resource: "test",
AccessType: "read",
}
dao := CustomerGroupDAO{}
ctx := context.Background()
// rm res
err := dao.RemoveResourceAccess(ctx, res)
assert.NotNil(suite.T(), err)
}