ycc-server/app/main/api/internal/middleware/global_sourceinterceptor_middleware.go

33 lines
754 B
Go
Raw Normal View History

2025-06-19 17:12:48 +08:00
package middleware
import (
"context"
"net/http"
)
const (
PlatformKey = "X-Platform"
2025-06-20 15:12:34 +08:00
PromoteKey = "X-Promote-Key"
2025-06-19 17:12:48 +08:00
)
func GlobalSourceInterceptor(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// 获取请求头 X-Platform 的值
platform := r.Header.Get(PlatformKey)
2025-06-20 15:12:34 +08:00
promoteValue := r.Header.Get(PromoteKey)
2025-06-19 17:12:48 +08:00
// 将值放入新的 context 中
ctx := r.Context()
if platform != "" {
ctx = context.WithValue(ctx, "platform", platform)
}
2025-06-20 15:12:34 +08:00
if promoteValue != "" {
ctx = context.WithValue(ctx, "promoteKey", promoteValue)
}
2025-06-19 17:12:48 +08:00
// 通过 r.WithContext 将更新后的 ctx 传递给后续的处理函数
r = r.WithContext(ctx)
// 传递给下一个处理器
next(w, r)
}
}