typical-rest-server

module
v0.8.37 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2020 License: MIT

README

Go-Workflow Go Report Card codebeat badge codecov

typical-rest-server

Opinionated, simple and straight forward Restful server implementation for Golang.

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) with SOLID Principle and Table-Driven Test

  • 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 uber-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 (
  // AppCfg application configuration
  // @app-cfg (prefix:"APP")
  AppCfg struct {
    Address string `envconfig:"ADDRESS" default:":8089" required:"true"`
    Debug   bool   `envconfig:"DEBUG" default:"true"`
  }
)

Mocking

Typical-Rest encourage mocking using gomock and annotation(@mock).

type(
  // Reader responsible to read
  // @mock
  Reader interface{
    Read() error
  }
)

ORM Hate

Typical-Rest do not encourage Objection-Relation-Mapping/ORM (ORM Hate)

Shared Library

The pkg contain useful library for general needs

  • pkg/typrest Utility for rest application e.g. health check, error, 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

Tools

3rd-Party Library

The project use go modules to manage package dependency. The compelete library list can be found in go.mod.

Golang References

Project Status

The project status is WIP (Work in progress) which means the author continously evaluate and improve the project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Directories

Path Synopsis
cmd
internal
app
generated
Package generated contain generated code from annotate
Package generated contain generated code from annotate
server/repository_mock
Package repository_mock is a generated GoMock package.
Package repository_mock is a generated GoMock package.
server/service_mock
Package service_mock is a generated GoMock package.
Package service_mock is a generated GoMock package.
pkg
tools
pg-tool command
typical-build command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL