2024-10-03 11:41:32 +00:00
|
|
|
package middleware
|
|
|
|
|
2024-10-03 11:53:30 +00:00
|
|
|
import (
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
2024-10-03 11:41:32 +00:00
|
|
|
|
2024-10-03 11:48:36 +00:00
|
|
|
const requestID = "X-Request-Id"
|
|
|
|
|
2024-10-03 11:41:32 +00:00
|
|
|
func RequestID() gin.HandlerFunc {
|
|
|
|
return func(ctx *gin.Context) {
|
2024-10-03 11:48:36 +00:00
|
|
|
var rid string
|
|
|
|
|
|
|
|
if rid = ctx.GetString(requestID); rid != "" {
|
|
|
|
// request id exists already
|
|
|
|
ctx.Next()
|
|
|
|
}
|
|
|
|
|
|
|
|
if rid = ctx.GetHeader(requestID); rid != "" {
|
|
|
|
ctx.Set(requestID, rid)
|
2024-10-03 11:53:30 +00:00
|
|
|
ctx.Next()
|
2024-10-03 11:48:36 +00:00
|
|
|
}
|
2024-10-03 11:53:30 +00:00
|
|
|
|
|
|
|
rid = uuid.NewString()
|
|
|
|
ctx.Set(requestID, rid)
|
2024-10-03 11:41:32 +00:00
|
|
|
}
|
|
|
|
}
|