add pre middleware
This commit is contained in:
parent
1d8453af9d
commit
a098d141c2
8
api.go
8
api.go
@ -37,9 +37,12 @@ type BaseWithErrCodeOutput struct {
|
|||||||
ErrMsg string `json:"errMsg"`
|
ErrMsg string `json:"errMsg"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEngine() *Engine {
|
func NewEngine(pre ...gin.HandlerFunc) *Engine {
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
e := gin.New()
|
e := gin.New()
|
||||||
|
for _, m := range pre {
|
||||||
|
e.Use(m)
|
||||||
|
}
|
||||||
e.Use(middlewareMyApiEngine())
|
e.Use(middlewareMyApiEngine())
|
||||||
return &Engine{e}
|
return &Engine{e}
|
||||||
}
|
}
|
||||||
@ -111,9 +114,6 @@ func middlewareMyApiEngine() gin.HandlerFunc {
|
|||||||
|
|
||||||
// Step 3. do request
|
// Step 3. do request
|
||||||
c.Next()
|
c.Next()
|
||||||
|
|
||||||
// Step 4. metrics record
|
|
||||||
cc.recordMetric()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,6 +149,9 @@ func (c *Context) RESULT(output interface{}) error {
|
|||||||
cost := time.Now().Sub(c.starttime).Milliseconds()
|
cost := time.Now().Sub(c.starttime).Milliseconds()
|
||||||
noticeStr := fmt.Sprintf("cost: %dms, output: '%s'", cost, string(b))
|
noticeStr := fmt.Sprintf("cost: %dms, output: '%s'", cost, string(b))
|
||||||
log.NoticefWithDepth(calldepth, noticeStr)
|
log.NoticefWithDepth(calldepth, noticeStr)
|
||||||
|
|
||||||
|
//////// metrics //////////////////////////////////
|
||||||
|
c.recordMetrics()
|
||||||
}(&output)
|
}(&output)
|
||||||
|
|
||||||
if _, ok := t.FieldByName("ErrCode"); !ok {
|
if _, ok := t.FieldByName("ErrCode"); !ok {
|
||||||
@ -212,7 +215,7 @@ func (c *Context) shapeOutput(o interface{}) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) recordMetric() {
|
func (c *Context) recordMetrics() {
|
||||||
if metricApiCounter == nil ||
|
if metricApiCounter == nil ||
|
||||||
metricApiSummary == nil {
|
metricApiSummary == nil {
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user