From a098d141c2bcf12bbc1057e57e605d0b6053515d Mon Sep 17 00:00:00 2001 From: bryanqiu Date: Wed, 21 Dec 2022 19:14:14 +0800 Subject: [PATCH] add pre middleware --- api.go | 8 ++++---- context.go | 5 ++++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/api.go b/api.go index 109f4dd..bc6acb3 100644 --- a/api.go +++ b/api.go @@ -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() } } diff --git a/context.go b/context.go index bf0bc10..adb5e65 100644 --- a/context.go +++ b/context.go @@ -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