typical-rest-server

module
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2020 License: MIT

README

Go-Workflow Go Report Card codebeat badge codecov

typical-rest-server

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

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

How to Use

  • References for project layout and code best practice.
  • Shared library (pkg) is utility/helper to general use.
  • For setup new project, please use rest-project-starter
    curl -s https://raw.githubusercontent.com/typical-go/rest-project-starter/master/setup.sh | bash -s [project_name] [package_name]
    

Builds

The project using typical-go as its build-tool. The build descriptor can be found in tools/typical-build/typical-build.go

Run application:

./typicalw docker up   # equivalent with `docker-compose up -d`
./typicalw reset       # reset infra: drop, create and migrate postgres database 
./typicalw run         # run the application

Test application:

./typicalw mock        # generate mock (if needed)
./typicalw test        # run test 

Project Layout

Typical-Rest encourage standard go project layout

Source codes:

Others directory:

  • tools Supporting tool for the project e.g. Build Tool
  • 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

Layered Architecture

Typical-Rest encourage layered architecture (as most adoptable architectural pattern) with SOLID Principle and Table-Driven Test

  • Presentation Layer at internal/app/server/controller
    • Handling HTTP routes
    • Parsing the request
    • Sending response (both success & error)
  • Logic Layer at internal/app/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/app/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 (@envconfig).

type (
  // AppCfg application configuration
  // @envconfig (prefix:"APP")
  AppCfg struct {
    Address string `envconfig:"ADDRESS" default:":8089" required:"true"`
    Debug   bool   `envconfig:"DEBUG" default:"true"`
  }
)

Generate usage documentation (USAGE.md) and .env file

// in typical-build

&typcfg.EnvconfigAnnotation{
  DotEnv:   ".env",     // generate .env file
  UsageDoc: "USAGE.md", // generate USAGE.md
}

Mocking

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

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

Mock class will be generated in *_mock package

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/echotest Utility for table-driven test echo handler

3rd-Party Library

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

Golang References

License

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

Directories

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

Jump to

Keyboard shortcuts

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