github.com/wfusion/gofusion@v1.1.14/http/middleware/trace.go (about) 1 package middleware 2 3 import ( 4 "github.com/gin-gonic/gin" 5 6 "github.com/wfusion/gofusion/common/utils" 7 8 fusCtx "github.com/wfusion/gofusion/context" 9 ) 10 11 func Trace() gin.HandlerFunc { 12 return func(c *gin.Context) { 13 var ( 14 userID, traceID string 15 ) 16 utils.IfAny( 17 func() bool { traceID = c.GetHeader(fusCtx.KeyTraceID); return traceID != "" }, 18 func() bool { traceID = c.GetHeader("HTTP_TRACE_ID"); return traceID != "" }, 19 func() bool { 20 traceID = utils.LookupByFuzzyKeyword[string](c.GetHeader, "trace_id") 21 return traceID != "" 22 }, 23 func() bool { traceID = utils.NginxID(); return traceID != "" }, 24 ) 25 c.Header("Trace-Id", traceID) 26 c.Set(fusCtx.KeyTraceID, traceID) 27 28 utils.IfAny( 29 func() bool { userID = c.GetHeader(fusCtx.KeyUserID); return userID != "" }, 30 func() bool { 31 userID = utils.LookupByFuzzyKeyword[string](c.GetHeader, "user_id") 32 return userID != "" 33 }, 34 func() bool { 35 userID = utils.LookupByFuzzyKeyword[string](c.GetQuery, "user_id") 36 return userID != "" 37 }, 38 func() bool { 39 userID = utils.LookupByFuzzyKeyword[string](c.GetPostForm, "user_id") 40 return userID != "" 41 }, 42 ) 43 c.Set(fusCtx.KeyUserID, userID) 44 c.Next() 45 } 46 }