2026-01-22 16:04:12 +08:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"net/http"
|
2026-05-09 18:24:59 +08:00
|
|
|
"strings"
|
2026-01-22 16:04:12 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
PlatformKey = "X-Platform"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func GlobalSourceInterceptor(next http.HandlerFunc) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
// 获取请求头 X-Platform 的值
|
2026-05-09 18:24:59 +08:00
|
|
|
platform := strings.TrimSpace(r.Header.Get(PlatformKey))
|
2026-01-22 16:04:12 +08:00
|
|
|
|
|
|
|
|
// 将值放入新的 context 中
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
if platform != "" {
|
|
|
|
|
ctx = context.WithValue(ctx, "platform", platform)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 通过 r.WithContext 将更新后的 ctx 传递给后续的处理函数
|
|
|
|
|
r = r.WithContext(ctx)
|
|
|
|
|
|
|
|
|
|
// 传递给下一个处理器
|
|
|
|
|
next(w, r)
|
|
|
|
|
}
|
|
|
|
|
}
|