gateway/middleware/request.go

25 lines
544 B
Go
Raw Normal View History

2021-09-07 13:40:44 +08:00
package middleware
import (
2021-09-07 17:12:29 +08:00
"gateway/core"
2021-09-07 13:40:44 +08:00
"github.com/gin-gonic/gin"
"github.com/nacos-group/nacos-sdk-go/inner/uuid"
)
// GenRequestId 生成RequestId
func GenRequestId() gin.HandlerFunc {
return func(c *gin.Context) {
requestId := c.Request.Header.Get("X-Request-Id")
if requestId == "" {
u, err := uuid.NewV4()
if err != nil {
2021-09-07 17:12:29 +08:00
core.Log.Error("生成UUID错误: %v\n", err.Error())
2021-09-07 13:40:44 +08:00
} else {
requestId = u.String()
}
}
c.Set("X-Request-Id", requestId)
c.Writer.Header().Set("X-Request-Id", requestId)
}
}