package trace

import "github.com/gin-gonic/gin"

// TraceMiddleware 为每个 HTTP 请求生成 trace ID 并注入 context
func TraceMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		traceID := GenerateTraceID()
		ctx := WithTraceID(c.Request.Context(), traceID)
		ctx = WithRequestID(ctx, traceID) // REST: trace_id == request_id

		c.Request = c.Request.WithContext(ctx)
		c.Header("X-Trace-ID", traceID) // 返回给客户端用于排查

		c.Next()
	}
}
