A template for a go JSON api with Google App Engine ready deployment.
As of Go version 1.11, GAE offers a standard environment that lifts all previous restrictions (See https://blog.golang.org/appengine-go111 and https://cloud.google.com/blog/products/application-development/go-1-11-is-now-available-on-app-engine).
- Dependency injection with
google/wire
- Integrates with postgres on Cloud SQL, as well as local dockerised postgres for integration testing.
- Integration tests are totally decoupled for app code. By using a different language (JS), it prevents reuse of struct definitions and enforces a black box approach.
- System side effects needed only for tests (e.g. migrate DB down and up), are decoupled into a separate binary.
- Fork this repository
- Rewrite all the imports and other file to point to your fork with:
$ perl -i -p -e 's/github.com\/pazams\/go-create-api/github.com\/<your-gh-handle>\/<your-repo-name>/g' **/*.*
- Install wire:
$ GO111MODULE=on go get github.com/google/wire/cmd/wire
- Generate
wire_gen.go
:$ GO111MODULE=on wire ./pkg/api/
- When adding code, if you get "pkg/api/wire_gen.go: not enough arguments in call to ..." it means we need to regenerate wiring.
$ make build
$ make serve
$ make test
Note: while iterating on the tests, after running the test command, if the app code didn't change, we can just build and run the tests with while everything else is still running: $ docker-compose up --build --no-deps test
have these env vars set:
APP_ENV= # LOCAL / PROD / GAE
GCP_PROJECT= # only if APP_ENV=GAE
PORT= # in GAE set by the environment
API_TOKEN= # string
POSTGRES_GCP_CONNECTION_NAME= # only if APP_ENV=GAE <project_id:zone:instance>
POSTGRES_ADDR= #either or POSTGRES_GCP_CONNECTION_NAME
POSTGRES_DATABASE=
POSTGRES_USER=
POSTGRES_PASSWORD=
Then, deploy with:
$ make deploy
Alternatively, to use GCP secrets for configs
have GCP_PROJECT=
and IS_GCP_CONFIG=true
and then
$ make deploy-sec-conf
Note:
app-subst.yaml
is committed to source and does NOT have secrets expendedapp.yaml
is NOT committed to source control and does have secrets expended
- micro services?