83 lines
1.7 KiB
Go
83 lines
1.7 KiB
Go
package api
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/go-playground/validator/v10"
|
|
"qoobing.com/gomod/log"
|
|
"qoobing.com/gomod/str"
|
|
)
|
|
|
|
var (
|
|
validate = validator.New()
|
|
requestLogidGetter = defaultLogidGetter
|
|
errCodeUnknownError = -10000
|
|
errCodeUnimplementApi = -20401
|
|
)
|
|
|
|
type Engine struct {
|
|
*gin.Engine
|
|
}
|
|
|
|
type BaseWithEnoOutput struct {
|
|
Eno int `json:"eno"`
|
|
Err string `json:"err"`
|
|
}
|
|
|
|
type BaseWithCodeOutput struct {
|
|
Code int `json:"code"`
|
|
Msg string `json:"msg"`
|
|
}
|
|
|
|
type BaseWithErrCodeOutput struct {
|
|
ErrCode int `json:"errCode"`
|
|
ErrMsg string `json:"errMsg"`
|
|
}
|
|
|
|
func NewEngine() *Engine {
|
|
gin.SetMode(gin.ReleaseMode)
|
|
e := gin.New()
|
|
e.Use(middlewareMyApiEngine())
|
|
return &Engine{e}
|
|
}
|
|
|
|
func (e *Engine) POST(uri string, handler HandlerFunc) {
|
|
e.Engine.POST(uri, handlerWrapper(handler))
|
|
}
|
|
|
|
// middlewareMyApiEngine myapi engine middleware
|
|
func middlewareMyApiEngine() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
// Step 1. set logid
|
|
id := requestLogidGetter(c)
|
|
log.SetLogid(id)
|
|
defer log.Cleanup()
|
|
|
|
// Step 2. init *api.Context
|
|
cc := New(c)
|
|
c.Set("cc", cc)
|
|
|
|
// Step 3. do request
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
// defaultLogidGetter get logid from "X-Request-Id" or create a new one.
|
|
func defaultLogidGetter(c *gin.Context) (logid string) {
|
|
if logid = c.Request.Header.Get("X-Request-Id"); logid != "" {
|
|
return logid
|
|
}
|
|
if logid = str.GetRandomNumString(13); logid != "" {
|
|
return logid
|
|
}
|
|
panic("unreachable code")
|
|
}
|
|
|
|
func InitErrCodes(errcodes map[string]int) {
|
|
if code, ok := errcodes["ErrCodeUnknownError"]; ok {
|
|
errCodeUnknownError = code
|
|
}
|
|
if code, ok := errcodes["ErrCodeUnimplementApi"]; ok {
|
|
errCodeUnknownError = code
|
|
}
|
|
}
|