mirror of
https://github.com/gl-inet/glkvm-cloud.git
synced 2026-09-21 01:53:23 +00:00
a5bce3b289
Complete the initial framework for resource-based permission management, providing the foundation for role-based access control and future permission extensions. Signed-off-by: GL.iNet-Yongping.Xie <yongping.xie@gl-inet.com>
32 lines
499 B
Go
Executable File
32 lines
499 B
Go
Executable File
package middleware
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
const TraceIDKey = "traceId"
|
|
|
|
func Trace() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
b := make([]byte, 8)
|
|
_, _ = rand.Read(b)
|
|
traceID := hex.EncodeToString(b)
|
|
|
|
c.Set(TraceIDKey, traceID)
|
|
c.Writer.Header().Set("X-Trace-Id", traceID)
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
func GetTraceID(c *gin.Context) string {
|
|
if v, ok := c.Get(TraceIDKey); ok {
|
|
if s, ok := v.(string); ok {
|
|
return s
|
|
}
|
|
}
|
|
return ""
|
|
}
|