Boilerplate template for backend project using go language for optimize and efficient resources.
First before we run the application, lets fresh create model schema using ent:
- Get dependency for golang ent
go get entgo.io/ent/cmd/ent
- Create new model schema, the generated model located on
ent/schema/model_name.go
https://entgo.io/docs/schema-def for schema documentation
go run entgo.io/ent/cmd/ent init {model_name}
- Generate assets
go generate ./ent
generate mock file for all interface in domains. Install mockery first
go install github.com/vektra/mockery/[email protected]
then generate mock using below command
mockery --all --dir internal/applications --output mocks --keeptree --packageprefix mock_
- Install google wire CLI
go install github.com/google/wire/cmd/wire@latest
- Add wire on your $PATH, so we can use wire CLI on every project
- Create {domains}_injector.go in your feature directory
- Then Run wire using makefile
>> make wire
>> Enter directory:
>> A_templates_directory
Steps to generate OpenAPI Docs and use via Swagger UI:
- Install
swag
command into your local machine:go install github.com/swaggo/swag/cmd/swag@latest
- Annotate each controller with Declarative Comment Format
- Update and synchronize the
cmd/docs
module with your update:make swagger
- Restart the service and access Swagger UI at
http://localhost:8888/:app-name/swagger/index.html
generate migration, up, down and status
- Run the command to download and install the migration library.
go install github.com/pressly/goose/v3/cmd/goose@latest
- Run the command for migration creation, this will create the migration file version
make migration-create name={name_migration} type={go|sql}
- Run the command for migration up, this command will execute the generated migration files
make migration-up
- Run the command for migration down, this command will execute rollback migration based on version of your migration file
make migration-down
- Run the command for migration status, we can check status the migration using this command
make migration-status
- Default general config is located in
root-project-path/.env
- For credential the default config is located in
root-project-path/secret.env
- We can use custom path on running the executable application
./main -credentials-path="ABC" -credentials-name="XYZ"
- We can use custom path on running the executable application
using Makefile to run the project. make all
will execute schema ent generation, mockery for mock test, swagger docs, testing project and build then run project
make all
- health check example using MVC pattern - health
- System parameter for feature flag as CRUD example using MVC & repository pattern - system_parameter
- Transaction CRUD example using ent go - service
- Publish and subscribe using rabbitmq - [add link]
- Golang 1.19
- https://go.dev/doc/effective_go
- https://github.com/golovers/effective-go
- https://go.dev/play/
- Video references from PZN bahasa indo : PZN-golang-playlist
- Tutorial bahasa indonesia dasar golang noval agung
- echo framework v4
- https://echo.labstack.com/, Not the fastest, but on par with GIN with better documentation benchmark discussion
- There are also a lot of tutorial on the net using bahasa indo, and easy for beginner ! noval agung echo framework rest api
- Viper
- https://github.com/spf13/viper, commonly used and powerfull configuration libs
- Entgo
- https://entgo.io/
- New kids on the block, developed by facebook team. Not the fastest, but better than gorm and have generated query builder!
- See benchmark : ent benchmark
- https://entgo.io/
- Google wire - code gen for dependency injection
- https://github.com/google/wire, DI code generator
- Good tutorial for getting started with example tutorial google DI with google wire
- Video references from PZN - golang DI with google wire
- testify : assert test
- mockery : mock test
- Cache : {to add explanation later}
- Logging : using echo log
- Message brooker : {to add explanation later}
- API documentation : swagger using swaggo
- Containerization : {to add explanation later}
- create clean code structure
- create interface with example domains system param
- create manual DI on hello worlds example domains
- implement repository + database connection using ent in system param example domains
- implement optimistic locking https://github.com/ent/ent/blob/master/examples/version/README.md
- implement global error handling
- implement DI google wire
- implement redis cache
- implement migration files, instead of using ent / atlas we decide to use https://pkg.go.dev/github.com/golang-migrate/migrate/v4 for easiness and simplicity
- Example - implement test for CRUD example
- positive test case
- great talk by imre ! Writing Better Test in Go | Go Srilanka Meetup June 25th, 2021
- using testify for the sake concise code, no needs write many if for assertion
- using mockery
- Example - implement outbound http calls - support config timeout, retry, circuit breaker
- Example - implement transaction examples - https://entgo.io/docs/transactions
- Example - implement migration files - https://entgo.io/docs/data-migrations, https://atlasgo.io/
- implement logger
- integrate live reload
- implement message broker integration - rabbitmq
- integrate swagger or API docs - https://github.com/swaggo/echo-swagger
- dockerize project / Colima