mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
21 lines
481 B
Go
21 lines
481 B
Go
package middleware
|
|
|
|
import (
|
|
"github.com/ez-api/foundation/requestid"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// RequestID ensures every request has an X-Request-ID and echoes it back to the client.
|
|
func RequestID() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
id := requestid.Extract(c.GetHeader)
|
|
if id == "" {
|
|
id = requestid.New()
|
|
}
|
|
c.Request.Header.Set(requestid.HeaderName, id)
|
|
c.Writer.Header().Set(requestid.HeaderName, id)
|
|
c.Set("request_id", id)
|
|
c.Next()
|
|
}
|
|
}
|