12345678910111213141516171819202122232425262728293031323334353637 |
- package middleware
- import (
- "net/http"
- "strings"
- "github.com/gin-gonic/gin"
- "github.com/google/uuid"
- log "gogs.baozhida.cn/zoie/OAuth-core/logger"
- "gogs.baozhida.cn/zoie/OAuth-core/pkg"
- )
- // RequestId 自动增加requestId
- func RequestId(trafficKey string) gin.HandlerFunc {
- return func(c *gin.Context) {
- if c.Request.Method == http.MethodOptions {
- c.Next()
- return
- }
- requestId := c.GetHeader(trafficKey)
- if requestId == "" {
- requestId = c.GetHeader(strings.ToLower(trafficKey))
- }
- if requestId == "" {
- requestId = uuid.New().String()
- }
- c.Request.Header.Set(trafficKey, requestId)
- c.Set(trafficKey, requestId)
- c.Set(pkg.LoggerKey,
- log.NewHelper(log.DefaultLogger).
- WithFields(map[string]interface{}{
- trafficKey: requestId,
- }))
- c.Next()
- }
- }
|