-
Notifications
You must be signed in to change notification settings - Fork 50
/
middleware.go
37 lines (32 loc) 路 942 Bytes
/
middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package admin
import (
"time"
"github.com/bricks-cloud/bricksllm/internal/util"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
func getAdminLoggerMiddleware(log *zap.Logger, prefix string, prod bool, adminPass string) gin.HandlerFunc {
return func(c *gin.Context) {
if len(adminPass) != 0 && c.Request.Header.Get("X-API-KEY") != adminPass {
c.Status(200)
c.Abort()
return
}
c.Set(correlationId, util.NewUuid())
start := time.Now()
c.Next()
latency := time.Since(start).Milliseconds()
if !prod {
log.Sugar().Infof("%s | %d | %s | %s | %dms", prefix, c.Writer.Status(), c.Request.Method, c.FullPath(), latency)
}
if prod {
log.Info("request to admin management api",
zap.String(correlationId, c.GetString(correlationId)),
zap.Int("code", c.Writer.Status()),
zap.String("method", c.Request.Method),
zap.String("path", c.FullPath()),
zap.Int64("lantecyInMs", latency),
)
}
}
}