package middleware import ( "github.com/gin-gonic/gin" "github.com/google/uuid" ) const ( // XRequestIDKey defines X-Request-ID key string. XRequestIDKey = "X-Request-ID" ) // RequestID is a middleware that injects a 'X-Request-ID' into the context and request/response header of each request. func RequestID() gin.HandlerFunc { return func(c *gin.Context) { // Check for incoming header, use it if exists rid := c.GetHeader(XRequestIDKey) if rid == "" { rid = uuid.Must(uuid.NewV6()).String() c.Request.Header.Set(XRequestIDKey, rid) c.Set(XRequestIDKey, rid) } // Set XRequestIDKey header c.Writer.Header().Set(XRequestIDKey, rid) c.Next() } }