
typical-rest-server
Simple, straight-forward and opinionated Rest-Server implementation example in Go.
How to Use
- Use the project as reference for project layout and code best practice.
- The pkg package is shared library that can help in various needs.
Prerequisite
Quick Start
The project using typical-go as its build-tool.
./typicalw docker up # equivalent with `docker-compose up -d` (if infrastructure not up)
./typicalw pg reset # drop, create and migrate postgres database (if database not ready)
./typicalw mock # generate mock (if require mock)
./typicalw test # run test
./typicalw run # run the application
Project Layout
Typical-Rest encourage standard go project layout
internal Exclusive go source files for the project
pkg Shareable go source files e.g. helper/utitily Library
api Any related scripts for API e.g. api-model script (swagger, raml, etc) or client script
databases Any related scripts for Databases e.g. migration scripts and seed data
tools Supporting tool for the project
Layered Architecture
Typical-Rest encourage layered architecture as most adoptable architectural pattern.
- Presentation Layer at
internal/server/controller
- Handling HTTP routes
- Parsing the request
- Sending response (both success & error)
- Logic Layer at
internal/server/service
- Intermediary between controller (end-point) and repository (data)
- Logic of controller
- Data Validation
- DTO (Data Transfer Object) Model
- Data Access Layer at
internal/server/repository
- No logic except operation to database
- Repository pattern
- DAO (Data Access Object) Model
- Database Entity or Business Entity
Dependency Injection
Typical-Rest encourage dependency-injection using dig and annotations (@ctor for constructor and @dtor for destructor).
// OpenConn open new database connection
// @ctor
func OpenConn() *sql.DB{
}
// CloseConn close the database connection
// @dtor
func CloseConn(db *sql.DB){
}
Application Config
Typical-Rest encourage application config with environment variables using envconfig and annotation (@app-cfg).
type (
// ServerCfg configuration
// @cfg (prefix:"SERVER")
ServerCfg struct {
Address string `envconfig:"ADDRESS" default:":8080" required:"true"`
}
)
Mocking
Typical-Rest encourage mocking using gomock and annotation(@mock).
type(
// Reader responsible to read
// @mock
Reader interface{
Read() error
}
)
Shared Library
The pkg contain useful library for general needs
pkg/typrest Utility for rest application e.g. health check, etc
pkg/dbkit Utility for database operation
pkg/dbtxn Utility for database transaction
pkg/dockerrx Docker Recipe Collection to be generated by typical-go
pkg/echotest Utility for table-driven test echo handler
3rd-Party Library
Go idiomatically, the project use go modules to manage package dependency.
Concepts
Golang References
Project Status
The project status is WIP (Work in progress) which means the author plan to continously evaluate and improve the project.
License
This project is licensed under the MIT License - see the LICENSE.md file for details