Skip to content

A template for a go JSON api with Google App Engine ready deployment.

License

Notifications You must be signed in to change notification settings

pazams/go-create-api

Repository files navigation

Build Status

Create Go API

What

A template for a go JSON api with Google App Engine ready deployment.

Why

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).

Features

  • 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.

How to

Make it your own (fork)

  • 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' **/*.*

Add dependencies to DI(wire)

  • 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.

Build (updates go.mod)

$ make build

Run

$ make serve

Test

$ 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

Deploy

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 expended
  • app.yaml is NOT committed to source control and does have secrets expended

TODO

  • micro services?

About

A template for a go JSON api with Google App Engine ready deployment.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published