GO REST API service
Design
- two layered micro service
- web api controller logic
- persistence layer
- Data transfer object definition for domain entity data representation
- using dependency injection for each layer for testability
- low ceremony IoC solution for dependency injection karlkfi/inject
- using echo as web api framework
- gomock with mockgen as mocking framework
- test/coverage report (and much more) integrated via build script
- use DATA-DOG/go-sqlmock for persistence tests
- code structure according to thockin/go-build-template
How to Build
set your $GOPATH (applies to unixoid OS', for windows replace all
$ENVVARNAME whith %ENVVARNAME% )
get and build project plus build & test dependencies
go get -t -v github.com/ckolumbus/golangRestApiExampleWithDependencyInjection
How to run Tests
go test -v github.com/ckolumbus/golangRestApiExampleWithDependencyInjection/...
How to Run
No special setup needed, database and schema are created automatcially in ./db.sqlite
cd $GOPATH/bin
./golangRestApiExampleWithDependencyInjection
Using MAGE
mage is a go based task runner which implements
an internal DSL define tasks and dependencies.
Install mage by running
go get -u -d github.com/magefile/mage
cd $GOPATH/src/github.com/magefile/mage
go run bootstrap.go
After this you can get a list of possbile targets with
cd $GOPATH/src/github.com/ckolumbus/golangRestApiExampleWithDependencyInjection
$GOPATH/bin/mage
$GOPATH/bin/mage -v check
$GOPATH/bin/mage -v install
Output
Targets:
check Run tests and linters
checkVendor verifies that vendored packages match git HEAD
fmt Run gofmt linter
install binary
lint Run golint linter
service Build binary
serviceNoGitInfo Build Service without git info
serviceRace Build binary with race detector enabled
test Run tests
test386 Run tests in 32-bit mode
testCoverHTML Generate test coverage report
testRace Run tests with race detector
vendor Install Go Dep and sync vendored dependencies
vet Run go vet linter
Todos
- create build script: use mage
- improve documentation
- integrate mock framework : use gmock
- integrate initial db/schema creation
- investigate possible use of an ORM
-
investigate seperation of tests (e.g. controller) into own package and/or directory
according to all best practices on the net this seperation should not be done
- add mock generation to build script (example:
$GOPATH/bin/mockgen -source pkg/employee/persistence/IPersistEmployee.go -destination controllers/PersistEmployeeMock_test.go -package controllers)
- logging concept
- handling arrays: use elments with or without "*"?
- Vendoring with dep (seems to be the recommenden tool, alternatives are listed in the Go Wiki)
- structured logging support with, possibly with logrus
References
Structure
c.f. code guidlines
Naming conventions
Handling of external Dependency
Install all dependencies, -t includes test dependencies
go get -t ./...
Build
Tests
according to the (test structure][https://golang.org/doc/code.html#Testing] definition the
test file are located next to the production code files.
TODO: search for best practices, maybe seperate test from production code
Dependency Injection / Inversion of Control
Mocks
Debug
other REST examples
JSON handling
Task runner
The goal was to have a go based task runner to stay whitin one technology, my personal
preference goes towards internal DSL, i.e. the task description is a go file itself.
There is quite a number of task runners fo go, most are yaml based,
some are go based.
YAML
GO