All Projects → typical-go → Typical Rest Server

typical-go / Typical Rest Server

Licence: mit
Pragmatic Boilerplate for Golang RESTful Server Implementation

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Typical Rest Server

Resource Router Middleware
🚴 Express REST resources as middleware mountable anywhere
Stars: ✭ 124 (-14.48%)
Mutual labels:  rest-api, rest
Ssm
👅基于RESTful风格的前后端分离的SSM框架,集成了shiro和swagger等框架
Stars: ✭ 141 (-2.76%)
Mutual labels:  rest-api, rest
Watsonwebserver
Watson is the fastest, easiest way to build scalable RESTful web servers and services in C#.
Stars: ✭ 125 (-13.79%)
Mutual labels:  rest-api, rest
Rest Api Fuzz Testing
REST API Fuzz Testing (RAFT): Source code for self-hosted service developed for Azure, including the API, orchestration engine, and default set of security tools (including MSR's RESTler), that enables developers to embed security tooling into their CI/CD workflows
Stars: ✭ 119 (-17.93%)
Mutual labels:  rest-api, rest
Open Rest
Standard rest server, Base on restify and sequelize
Stars: ✭ 136 (-6.21%)
Mutual labels:  rest-api, rest
Rest
REST API generator with Node.js, Express and Mongoose
Stars: ✭ 1,663 (+1046.9%)
Mutual labels:  rest-api, rest
Swagger Inflector
Stars: ✭ 131 (-9.66%)
Mutual labels:  rest-api, rest
Swagger Express Ts
Generate and serve swagger.json
Stars: ✭ 102 (-29.66%)
Mutual labels:  rest-api, rest
Api Generator
Api Generator是一款可以自动解析Controller类抽取REST接口信息并自动上传YApi的IDEA插件。YApi好伴侣,从此维护文档再也不是事儿了!
Stars: ✭ 139 (-4.14%)
Mutual labels:  rest-api, rest
Sandman2
Automatically generate a RESTful API service for your legacy database. No code required!
Stars: ✭ 1,765 (+1117.24%)
Mutual labels:  rest-api, rest
Automatic Api
A list of software that turns your database into a REST/GraphQL API
Stars: ✭ 1,583 (+991.72%)
Mutual labels:  rest-api, rest
Codeigniter Jwt Sample
CodeIgniter JWT Sample
Stars: ✭ 144 (-0.69%)
Mutual labels:  rest-api, rest
Json Serverless
Transform a JSON file into a serverless REST API in AWS cloud
Stars: ✭ 108 (-25.52%)
Mutual labels:  rest-api, rest
Poloniex Api Node
Poloniex API client for REST and WebSocket API
Stars: ✭ 138 (-4.83%)
Mutual labels:  rest-api, rest
Airdcpp Webclient
Communal peer-to-peer file sharing application for file servers/NAS devices
Stars: ✭ 106 (-26.9%)
Mutual labels:  rest-api, rest
Jersey Jwt
Example of REST API with JWT authentication using Jersey, Jackson, Undertow, Weld, Hibernate and Arquillian.
Stars: ✭ 131 (-9.66%)
Mutual labels:  rest-api, rest
Http restful api
整理HTTP后台端的RESTful API方面的知识
Stars: ✭ 94 (-35.17%)
Mutual labels:  rest-api, rest
Node Typescript Mongodb
node js typescript mongodb express generator yo
Stars: ✭ 96 (-33.79%)
Mutual labels:  rest-api, rest
Go Codon
Workflow based REST framework code generator
Stars: ✭ 133 (-8.28%)
Mutual labels:  rest-api, rest
Gemini
Model Driven REST framework to automatically generate CRUD APIs
Stars: ✭ 138 (-4.83%)
Mutual labels:  rest-api, rest

Project Status: WIP – Initial development is in progress, but there has not yet been a stable, usable release suitable for the public. 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.

Pragmatic Golang RESTful Server Implementation. The project using typical-go as its build-tool.

  • Application
    • [x] Go-Standards Project Layout
    • [x] Environment Variable Configuration
    • [x] Health-Check and Debug API
    • [x] Graceful Shutdown
  • Layered architecture
    • [x] SOLID Principle
    • [x] Dependency Injection (using @ctor annotation)
    • [x] ORMHate
    • [x] Database Transaction
  • HTTP Server
    • [x] Echo framework
    • [x] Server Side Caching
      • [x] Cache but revalidate (Header Cache-Control: no-cache)
      • [x] Set Expiration Time (Header Cache-Control: max-age=120)
      • [x] Return 304 if not modified (Header If-Modified-Since: Sat, 31 Oct 2020 10:28:02 GMT)
    • [x] Request ID in logger (Header X-Request-Id: xxx)
  • RESTful
    • [x] Create Resource (POST verb)
    • [x] Update Resource (PUT verb)
    • [x] Partially Update Resource (PATCH verb)
    • [x] Find Resource (GET verb)
      • [x] Offset Pagination (Query param ?limit=100&offset=0)
      • [x] Sorting (Query param ?sort=-title,created_at)
      • [x] Total count (Header X-Total-Count: 99)
    • [x] Check resource (HEAD verb)
    • [x] Delete resource (DELETE verb, idempotent)
  • Testing
    • [x] Table Driven Test
    • [x] Mocking (using @mock annotation)
  • Others
    • [x] Database migration and seed tool
    • [x] Generate code, .env file and USAGE.md according the configuration (using @envconfig annotation)
    • [x] Generate code for repository layer (using @entity annotation)
    • [x] Releaser

Run/Test Project

Copy .env.sample for working configuration

cp .env.sample .env    # copy the working .env

Setup the local environment

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

Run application:

./typicalw run         # run the application

Test application:

./typicalw test        # run test 

Project descriptor at tools/typical-build/typical-build.go

var descriptor = typgo.Descriptor{
  ProjectName:    "typical-rest-server",
  ProjectVersion: "0.9.7",
  
  Tasks: []typgo.Tasker{
    // tasks ... 
  }
}

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 adopts layered architecture with SOLID Principle. Please check this article.

  • Presentation Layer at internal/app/controller
    • Parsing the request
    • Sending response
  • Business Logic Layer at internal/app/service
    • Intermediary between controller (end-point) and repository (data)
    • Logic of controller
    • Data Validation
  • Data Access Layer at internal/app/entity (database)
    • No logic except operation to database
    • Repository pattern for Database entity or Business Model

Dependency Injection

Typical-Rest encourage dependency injection using uber-dig and annotations (@ctor).

// NewConn ... 
// @ctor
func NewConn() *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

Database Transaction

In Repository layer

func (r *RepoImpl) Delete(ctx context.Context) (int64, error) {
  txn, err := dbtxn.Use(ctx, r.DB) // use transaction if begin detected 
  if err != nil {                  // create transaction error
      return -1, err
  }
  db := txn                     // transaction object or database connection
  // result, err := ...
  if err != nil {
      txn.SetError(err)            // set the error and plan for rollback
      return -1, err
  }
  // ...
}

In Service layer

func (s *SvcImpl) SomeOperation(ctx context.Context) (err error){
  // begin the transaction 
  txn := dbtxn.Begin(&ctx)

  // commit/rollback in end function
  defer func(){ err = txn.Commit() }()
  // ...
}

Server-Side Cache

Use echo middleware to handling cache

cacheStore := &cachekit.Store{
  Client:        redis.NewClient(&redis.Options{Addr: "localhost:6379"}),
  DefaultMaxAge: 30 * time.Second,
  PrefixKey:     "cache_",
}

e := echo.New()
e.GET("/", handle, cacheStore.Middleware)

References

Golang:

RESTful API:

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].