26 lines
522 B
Go
26 lines
522 B
Go
package middleware
|
|
|
|
import (
|
|
"slices"
|
|
|
|
"github.com/gin-contrib/slog"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func RequestID(skipPaths []string) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
logger := slog.Get(c)
|
|
id := c.GetHeader("X-Request-ID")
|
|
if id == "" {
|
|
id = uuid.NewString()
|
|
}
|
|
if len(skipPaths) > 0 && !slices.Contains(skipPaths, c.Request.URL.Path) {
|
|
logger.Info("RequestID", "id", id)
|
|
}
|
|
c.Set("request_id", id)
|
|
c.Writer.Header().Set("X-Request-ID", id)
|
|
c.Next()
|
|
}
|
|
}
|