ai-gateway/middleware/request-id.go

20 lines
450 B
Go
Raw Permalink Normal View History

2023-09-17 07:39:46 +00:00
package middleware
import (
"context"
"github.com/gin-gonic/gin"
2024-01-21 15:18:32 +00:00
"one-api/common/helper"
"one-api/common/logger"
2023-09-17 07:39:46 +00:00
)
func RequestId() func(c *gin.Context) {
return func(c *gin.Context) {
2024-01-21 15:18:32 +00:00
id := helper.GetTimeString() + helper.GetRandomString(8)
c.Set(logger.RequestIdKey, id)
ctx := context.WithValue(c.Request.Context(), logger.RequestIdKey, id)
2023-09-17 07:39:46 +00:00
c.Request = c.Request.WithContext(ctx)
2024-01-21 15:18:32 +00:00
c.Header(logger.RequestIdKey, id)
2023-09-17 07:39:46 +00:00
c.Next()
}
}