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"`
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user