tyc-server/app/main/api/internal/middleware/global/reqHeaderCtxMiddleware.go
2025-05-09 17:54:28 +08:00

35 lines
754 B
Go

package middleware
import (
"context"
"net/http"
)
type contextKey string
const (
brandKey contextKey = "brand"
platformKey contextKey = "platform"
promoteKey contextKey = "promoteKey"
)
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, brandKey, brand)
}
if platform != "" {
ctx = context.WithValue(ctx, platformKey, platform)
}
if promoteValue != "" {
ctx = context.WithValue(ctx, promoteKey, promoteValue)
}
r = r.WithContext(ctx)
next(w, r)
}
}