2023-09-17 07:39:46 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-04-25 03:02:20 +00:00
|
|
|
|
2023-09-17 07:39:46 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2024-04-25 03:02:20 +00:00
|
|
|
"github.com/songquanpeng/one-api/common/ctxkey"
|
2024-01-28 11:38:58 +00:00
|
|
|
"github.com/songquanpeng/one-api/common/helper"
|
2023-09-17 07:39:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func RequestId() func(c *gin.Context) {
|
|
|
|
return func(c *gin.Context) {
|
2024-03-13 17:02:47 +00:00
|
|
|
id := helper.GenRequestID()
|
2024-04-25 03:02:20 +00:00
|
|
|
c.Set(ctxkey.RequestId, id)
|
|
|
|
ctx := context.WithValue(c.Request.Context(), ctxkey.RequestId, id)
|
2023-09-17 07:39:46 +00:00
|
|
|
c.Request = c.Request.WithContext(ctx)
|
2024-04-25 03:02:20 +00:00
|
|
|
c.Header(ctxkey.RequestId, id)
|
2023-09-17 07:39:46 +00:00
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|