mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
refactor(deps): use foundation shared utilities
This commit is contained in:
@@ -1,35 +1,20 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"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 := strings.TrimSpace(c.GetHeader("X-Request-ID"))
|
||||
id := requestid.Extract(c.GetHeader)
|
||||
if id == "" {
|
||||
id = strings.TrimSpace(c.GetHeader("X-Request-Id"))
|
||||
id = requestid.New()
|
||||
}
|
||||
if id == "" {
|
||||
id = newRequestID()
|
||||
}
|
||||
c.Request.Header.Set("X-Request-ID", id)
|
||||
c.Writer.Header().Set("X-Request-ID", id)
|
||||
c.Request.Header.Set(requestid.HeaderName, id)
|
||||
c.Writer.Header().Set(requestid.HeaderName, id)
|
||||
c.Set("request_id", id)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func newRequestID() string {
|
||||
var b [16]byte
|
||||
if _, err := rand.Read(b[:]); err != nil {
|
||||
return hex.EncodeToString([]byte(time.Now().Format(time.RFC3339Nano)))
|
||||
}
|
||||
return hex.EncodeToString(b[:])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user