api/context.go
2022-12-05 23:45:59 +08:00

161 lines
3.9 KiB
Go

package api
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"reflect"
"runtime/debug"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
"qoobing.com/gomod/log"
"qoobing.com/gomod/str"
)
type Context struct {
*gin.Context
AppId string //current request appid, must not be empty
UserId uint64 //current request userid, maybe 0 if user unknown
resultentry string
httpstatus int
starttime time.Time
}
func New(c *gin.Context) *Context {
return &Context{
Context: c,
httpstatus: 200,
starttime: time.Now(),
AppId: "",
UserId: 0,
}
}
func (c *Context) TryRecover() {
if err := recover(); err != nil {
calldepth := 3
stack := strings.Replace(string(debug.Stack()), "\n", "\n== ", -1)
stack = str.SkipLine(stack, calldepth*2+1)
t := reflect.TypeOf(err)
switch t.Kind() {
case reflect.String:
log.Fatalf("panic err:%v. Panic stack:\n%s", err, stack)
c.RESULT_ERROR(errCodeUnknownError, "unknown error")
default:
log.Fatalf("panic err type: %s:%s. Panic stack:\n%s", t.Name(), t.String(), stack)
c.RESULT_ERROR(errCodeUnknownError, "unknown error")
}
}
}
func (c *Context) BindInput(i interface{}) (err error) {
req := c.Request
log.DebugfWithDepth(1, "[%s|%s|%s]", req.Method, req.Host, req.RequestURI)
logstr := fmt.Sprintf("input:[have not initialized]")
defer func(plogstr *string) {
log.DebugfWithDepth(2, "%s", *plogstr)
}(&logstr)
err = c.ShouldBind(i)
if err != nil {
b, _ := json.Marshal(i)
logstr = fmt.Sprintf("input:%s binderror:%s", string(b), err.Error())
return err
}
err = validate.Struct(i)
if err != nil {
outputstr := ""
for _, err := range err.(validator.ValidationErrors) {
b, _ := json.Marshal(i)
logstr = fmt.Sprintf("input:%s validateerror:%s", string(b), err.Error())
outputstr = fmt.Sprintf("invalide field: [%s(%s)]", err.Field(), err.Tag())
return errors.New(outputstr)
}
panic("Unreachable Code")
}
// recode input parameters.
b, _ := json.Marshal(i)
logstr = fmt.Sprintf("input:%s", string(b))
return nil
}
func (c *Context) SetCookie(cookie *http.Cookie) {
if cookie.Path == "" {
cookie.Path = "/"
}
http.SetCookie(c.Writer, cookie)
}
func (c *Context) RESULT(output interface{}) error {
var t = reflect.TypeOf(output)
defer func(o *interface{}) {
b, err := json.Marshal(o)
if err != nil {
return
}
calldepth := 0
switch c.resultentry {
case "RESULT":
calldepth = 0
case "RESULT_ERROR":
calldepth = 1
case "RESULT_PARAMETER_ERROR":
calldepth = 2
}
//// cut log if too long ////// begin //////////////
if len(b) > 1024 {
//no := retag.Convert(*o, retag.NewView("json", "!logignore"))
//if nb, err := json.Marshal(no); err != nil {
// return
//} else {
// b = nb
//}
}
//// cut log if too long ////// end ////////////////
cost := time.Now().Sub(c.starttime).Milliseconds()
noticeStr := fmt.Sprintf("cost: %dms, output: '%s'", cost, string(b))
log.NoticefWithDepth(calldepth, noticeStr)
}(&output)
if _, ok := t.FieldByName("ErrCode"); !ok {
errstr := "Result MUST have 'ErrCode' field"
output = BaseWithErrCodeOutput{errCodeUnknownError, errstr}
c.IndentedJSON(c.httpstatus, output)
return errors.New(errstr)
}
if _, ok := t.FieldByName("ErrMsg"); !ok {
errstr := "Result MUST have 'ErrMsg' field"
output = BaseWithErrCodeOutput{errCodeUnknownError, errstr}
c.IndentedJSON(c.httpstatus, output)
return errors.New(errstr)
}
c.IndentedJSON(c.httpstatus, output)
return nil
}
func (c *Context) RESULT_ERROR(eno int, err string) error {
if c.resultentry == "" {
c.resultentry = "RESULT_ERROR"
}
result := BaseWithErrCodeOutput{eno, err}
return c.RESULT(result)
}
func (c *Context) RESULT_PARAMETER_ERROR(err string) error {
if c.resultentry == "" {
c.resultentry = "RESULT_PARAMETER_ERROR"
}
return c.RESULT_ERROR(errCodeUnknownError, err)
}