28 lines
807 B
Go
28 lines
807 B
Go
package ginutils
|
|
|
|
import (
|
|
"git.dev.m-and-m.ovh/mderasse/gocommon/constant"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// RequestID is a middleware that will get the request_id from the incoming request if exist and
|
|
// add it to the response or generate a new ID.
|
|
func RequestID() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
|
|
requestID := c.GetHeader(string(constant.HeaderKey_RequestID))
|
|
if _, err := uuid.Parse(requestID); err != nil {
|
|
// no request ID or invalid. let's generate a new one
|
|
requestID = uuid.New().String()
|
|
}
|
|
|
|
// Adding to context
|
|
c.Set(string(constant.ContextKey_RequestID), requestID)
|
|
// Add request-id to the header before sending the response
|
|
c.Header(string(constant.HeaderKey_RequestID), requestID)
|
|
// Let's the next part run
|
|
c.Next()
|
|
}
|
|
}
|