GO Example (Web Service)
Trying to implement follow The Twelve Factor App
Dependencies
- Command-line interface: github.com/spf13/cobra
- Configuration: github.com/spf13/viper
- Testing: github.com/stretchr/testify
- Mocking DB: github.com/DATA-DOG/go-sqlmock
- ORM: gorm.io/gorm
- Logging: github.com/op/go-logging
- HTTP Server: github.com/gin-gonic/gin
- API Document: github.com/swaggo/swag/cmd/swag
Project structure
.
โโโ Dockerfile
โโโ LICENSE
โโโ Makefile
โโโ README.md
โโโ cmd
โ โโโ othercmd # example other command line app
โ โโโ server # start reading code from here
โโโ internal
โ โโโ api/v1
โ โโโ config
| |-- dto
โ โโโ entities
| |-- errors
| |-- log
โ โโโ services
โ โโโ utils
โโโ config
โ โโโ default.yaml
โโโ docs
โโโ dist
โ โโโ drawin
โ โโโ linux
โ โโโ windows
โโโ docker-compose.yml
โโโ go.mod
โโโ go.sum
Get started
Cross platform build environment setup
macOS
# install dep to build binary for linux and windows
brew install FiloSottile/musl-cross/musl-cross
brew install mingw-w64
Command Line
Run project with docker compose
docker compose -f dev.yml up --build
Run project without build
go run ./cmd/server [command] --[flag-name]=[flag-value]
Generate API Document
make doc
# open url http://localhost:5000/doc/index.html
Build using make
command
# Build single binary with specify os
make build[-mac|win|linux]
# Build all os
make all
# Running test
make test
# Start server without build binary file
make run
Build with docker
docker compose build # build docker image
docker compose up # run on docker
# or
docker compose up --build # build and run
docker push [image-name] # public docker image to registry
Configuration
Viper uses the following precedence order. Each item takes precedence over the item below it:
- explicit call to Set
- flag
- env
- config
- key/value store
- default
Example List
- Simple in main branch
- Port/Adapter in hexagonal branch