add pre middleware

This commit is contained in:
bryanqiu 2022-12-21 19:14:14 +08:00
parent 1d8453af9d
commit a098d141c2
2 changed files with 8 additions and 5 deletions

8
api.go
View File

@ -37,9 +37,12 @@ type BaseWithErrCodeOutput struct {
ErrMsg string `json:"errMsg"`
}
func NewEngine() *Engine {
func NewEngine(pre ...gin.HandlerFunc) *Engine {
gin.SetMode(gin.ReleaseMode)
e := gin.New()
for _, m := range pre {
e.Use(m)
}
e.Use(middlewareMyApiEngine())
return &Engine{e}
}
@ -111,9 +114,6 @@ func middlewareMyApiEngine() gin.HandlerFunc {
// Step 3. do request
c.Next()
// Step 4. metrics record
cc.recordMetric()
}
}

View File

@ -149,6 +149,9 @@ func (c *Context) RESULT(output interface{}) error {
cost := time.Now().Sub(c.starttime).Milliseconds()
noticeStr := fmt.Sprintf("cost: %dms, output: '%s'", cost, string(b))
log.NoticefWithDepth(calldepth, noticeStr)
//////// metrics //////////////////////////////////
c.recordMetrics()
}(&output)
if _, ok := t.FieldByName("ErrCode"); !ok {
@ -212,7 +215,7 @@ func (c *Context) shapeOutput(o interface{}) {
return
}
func (c *Context) recordMetric() {
func (c *Context) recordMetrics() {
if metricApiCounter == nil ||
metricApiSummary == nil {
return