tyc-server/app/main/api/internal/middleware/global/reqHeaderCtxMiddleware.go
2025-05-09 20:26:22 +08:00

27 lines
609 B
Go

package middleware
import (
"context"
"net/http"
)
func ReqHeaderCtxMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
brand := r.Header.Get("X-Brand")
platform := r.Header.Get("X-Platform")
promoteValue := r.Header.Get("X-Promote-Key")
ctx := r.Context()
if brand != "" {
ctx = context.WithValue(ctx, "brand", brand)
}
if platform != "" {
ctx = context.WithValue(ctx, "platform", platform)
}
if promoteValue != "" {
ctx = context.WithValue(ctx, "promoteKey", promoteValue)
}
r = r.WithContext(ctx)
next(w, r)
}
}