gateway/middleware/request.go
2021-09-07 17:12:29 +08:00

25 lines
544 B
Go

package middleware
import (
"gateway/core"
"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 {
core.Log.Error("生成UUID错误: %v\n", err.Error())
} else {
requestId = u.String()
}
}
c.Set("X-Request-Id", requestId)
c.Writer.Header().Set("X-Request-Id", requestId)
}
}