This commit is contained in:
liangzai 2025-05-09 20:26:22 +08:00
parent e43e76697b
commit 28879b02fb
3 changed files with 10 additions and 22 deletions

View File

@ -26,7 +26,6 @@ service main {
@server ( @server (
prefix: api/v1/product prefix: api/v1/product
group: product group: product
middleware: SourceInterceptor
) )
service main { service main {
@handler GetProductRenderList @handler GetProductRenderList

View File

@ -304,16 +304,13 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
) )
server.AddRoutes( server.AddRoutes(
rest.WithMiddlewares( []rest.Route{
[]rest.Middleware{serverCtx.SourceInterceptor}, {
[]rest.Route{ Method: http.MethodGet,
{ Path: "/render_list/:module",
Method: http.MethodGet, Handler: product.GetProductRenderListHandler(serverCtx),
Path: "/render_list/:module", },
Handler: product.GetProductRenderListHandler(serverCtx), },
},
}...,
),
rest.WithPrefix("/api/v1/product"), rest.WithPrefix("/api/v1/product"),
) )

View File

@ -5,14 +5,6 @@ import (
"net/http" "net/http"
) )
type contextKey string
const (
brandKey contextKey = "brand"
platformKey contextKey = "platform"
promoteKey contextKey = "promoteKey"
)
func ReqHeaderCtxMiddleware(next http.HandlerFunc) http.HandlerFunc { func ReqHeaderCtxMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
brand := r.Header.Get("X-Brand") brand := r.Header.Get("X-Brand")
@ -20,13 +12,13 @@ func ReqHeaderCtxMiddleware(next http.HandlerFunc) http.HandlerFunc {
promoteValue := r.Header.Get("X-Promote-Key") promoteValue := r.Header.Get("X-Promote-Key")
ctx := r.Context() ctx := r.Context()
if brand != "" { if brand != "" {
ctx = context.WithValue(ctx, brandKey, brand) ctx = context.WithValue(ctx, "brand", brand)
} }
if platform != "" { if platform != "" {
ctx = context.WithValue(ctx, platformKey, platform) ctx = context.WithValue(ctx, "platform", platform)
} }
if promoteValue != "" { if promoteValue != "" {
ctx = context.WithValue(ctx, promoteKey, promoteValue) ctx = context.WithValue(ctx, "promoteKey", promoteValue)
} }
r = r.WithContext(ctx) r = r.WithContext(ctx)
next(w, r) next(w, r)