forked from mtusi2026/mtusi2026lab6
33 lines
544 B
Go
33 lines
544 B
Go
package middleware
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const (
|
|
RequestIDKey = "request_id"
|
|
RequestIDHeader = "X-Request-Id"
|
|
)
|
|
|
|
func RequestID() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
id := c.GetHeader(RequestIDHeader)
|
|
if id == "" {
|
|
id = uuid.NewString()
|
|
}
|
|
c.Set(RequestIDKey, id)
|
|
c.Writer.Header().Set(RequestIDHeader, id)
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
func GetRequestID(c *gin.Context) string {
|
|
if v, ok := c.Get(RequestIDKey); ok {
|
|
if s, ok := v.(string); ok {
|
|
return s
|
|
}
|
|
}
|
|
return ""
|
|
}
|