首次提交

This commit is contained in:
yangy
2026-05-21 15:20:12 +08:00
commit 3224041990
32 changed files with 1504 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
package middleware
import (
"crypto/rand"
"encoding/hex"
"github.com/gin-gonic/gin"
)
const RequestIDKey = "X-Request-ID"
func newRequestID() string {
b := make([]byte, 16)
if _, err := rand.Read(b); err != nil {
return "unknown"
}
return hex.EncodeToString(b)
}
func RequestID() gin.HandlerFunc {
return func(c *gin.Context) {
id := c.GetHeader(RequestIDKey)
if id == "" {
id = newRequestID()
}
c.Set(RequestIDKey, id)
c.Header(RequestIDKey, id)
c.Next()
}
}