golang-boilerplate
Golang Boilerplate Template for RESTful API and Worker
Usage
You can run the whole project with this command:
make docker-compose-up
The API server will be availableready at http://localhost:8080. And the swagger will be at http://localhost:8080/docs.
If you want to develop with this project, you can run the prject with dev mode:
make DEV=1 docker-compose-up
And run the API server or worker by:
go run ./cmd/apiserver
go run ./cmd/worker
For more usage, please check Makefile and hack.
Configuration
You can override the configuration files under configs/ or provide .env
file when you are developing.
Global configs
Key |
Value |
Example |
MODE |
debug | release |
debug |
LOG_LEVEL |
panic | fatal | error | warn | info | debug | trace |
debug |
DATABASE_URI |
SCHEMA://USERNAME:PASSWORD@HOSTNAME:PORT/DATABASE_NAME |
sqlite://file::memory:?cache=shared |
Worker configs
To config the worker and worker client, please check the usage of machinery:
https://github.com/RichardKnop/machinery#configuration
Worker Tasks
There are some example builit-in tasks that can be used to create new job:
- sleep: The example
args
is {"seconds": 10}
- remoteHTTPCall: The example
args
is {"url": "https://postman-echo.com/post", "body": {"message": "Hello World"}}
- fibonacci: The example
args
is {"target": 5}
- error: The example
args
is {"message": "Error"}
Project Layout
This project trying to follow the project layout from this reference:
https://github.com/golang-standards/project-layout
The main implementation are located under pkg/ folder and the entrypoint of each application ar under cmd/.
Dependencies
- gorm: The ORM framework
- gin: The web framework
- machinery: The worker freamwwork
- zerolog: The log replacement
- viper: The configuration management
TODO