Skip to content
I-Chiao Hsu edited this page Feb 3, 2020 · 5 revisions

Welcome to the readr-restful wiki!

Table of Content

Package layout

readr-restful
├── config
├── db_schema
├── integration_test
├── internal
├── models
├── pkg
├── routes
├── utils
├── Dockerfile
├── Makefile
├── README.md
├── go.mod
├── go.sum
└── main.go
  • config: JSON config files and config-related functions
  • db_schema: Schema migration files
  • integration_test: Integration tests and golden files
  • internal: Not public-facing package and mostly used internally by readr-restful. Most of them are helpers
  • models: DB mapping structs and database layer functions. Legacy
  • pkg: Used for public services.
  • routes: http handlers. Legacy
  • utils: Internal use functions. Supposedly legacy

Dependencies

We use Golang module, so Go version must be greater than 1.12 and enable GO111MODULE.

Belows are some packages we heavily depend on:

Test

Mocking with gomock

gomock is the core mock package maintained by Golang team. We could exploit it to mock database services interface to unit-test our http handlers.

How to generate database mock

Below will take promotion as an example.

  • Use mockgen CLI
mockgen -package=mock -destination=mock/mock.go github.com/readr-media/readr-restful/pkg/promotion DataLayer

This could generate mocked DataLayer struct directly into file mock.go

  • Write the generate instructions in *.go files In promotion.go you could find the line
//go:generate mockgen -package=mock -destination=mock/mock.go github.com/readr-media/readr-restful/pkg/promotion DataLayer

notice there is no space between // and go:generate... Then you could use go generate ./... under the root path of the service. This will find every interface denoted to be mocked and generate the mock files in the assigned path

Notes

Disabled cgo when testing

Because the natural restriction in go 1.12, it will try to recompile the net/http package when run go test. This will fail on alpine images because of the lack of gcc. I have to disable cgo when testing. This is hacked in the test part in Makefile, too.