@@ -27,12 +27,16 @@ import (
2727const defaultHttpScheme = "http"
2828
2929type (
30+ // MiddlewareFunc defines the function signature for middleware.
31+ MiddlewareFunc func (next http.HandlerFunc ) http.HandlerFunc
32+
3033 // Server is a gateway server.
3134 Server struct {
3235 * rest.Server
3336 upstreams []Upstream
3437 conns []zrpc.Client
3538 processHeader func (http.Header ) []string
39+ middlewares []MiddlewareFunc
3640 dialer func (conf zrpc.RpcClientConf ) zrpc.Client
3741 }
3842
@@ -105,7 +109,7 @@ func (s *Server) build() error {
105109
106110func (s * Server ) buildGrpcHandler (source grpcurl.DescriptorSource , resolver jsonpb.AnyResolver ,
107111 cli zrpc.Client , rpcPath string ) func (http.ResponseWriter , * http.Request ) {
108- return func (w http.ResponseWriter , r * http.Request ) {
112+ handler := func (w http.ResponseWriter , r * http.Request ) {
109113 parser , err := internal .NewRequestParser (r , resolver )
110114 if err != nil {
111115 httpx .ErrorCtx (r .Context (), w , err )
@@ -124,6 +128,8 @@ func (s *Server) buildGrpcHandler(source grpcurl.DescriptorSource, resolver json
124128 httpx .ErrorCtx (r .Context (), w , st .Err ())
125129 }
126130 }
131+
132+ return s .buildChainHandler (handler )
127133}
128134
129135func (s * Server ) buildGrpcRoute (up Upstream , writer mr.Writer [rest.Route ], cancel func (error )) {
@@ -177,7 +183,7 @@ func (s *Server) buildGrpcRoute(up Upstream, writer mr.Writer[rest.Route], cance
177183}
178184
179185func (s * Server ) buildHttpHandler (target * HttpClientConf ) http.HandlerFunc {
180- return func (w http.ResponseWriter , r * http.Request ) {
186+ handler := func (w http.ResponseWriter , r * http.Request ) {
181187 w .Header ().Set (httpx .ContentType , httpx .JsonContentType )
182188 req , err := buildRequestWithNewTarget (r , target )
183189 if err != nil {
@@ -213,6 +219,8 @@ func (s *Server) buildHttpHandler(target *HttpClientConf) http.HandlerFunc {
213219 logc .Error (r .Context (), err )
214220 }
215221 }
222+
223+ return s .buildChainHandler (handler )
216224}
217225
218226func (s * Server ) buildHttpRoute (up Upstream , writer mr.Writer [rest.Route ]) {
@@ -263,6 +271,21 @@ func WithHeaderProcessor(processHeader func(http.Header) []string) func(*Server)
263271 }
264272}
265273
274+ // WithMiddleware adds one or more middleware functions to process HTTP requests.
275+ // Multiple middlewares will be executed in the order they were passed (like an onion model).
276+ func WithMiddleware (middlewares ... MiddlewareFunc ) func (* Server ) {
277+ return func (s * Server ) {
278+ s .middlewares = append (s .middlewares , middlewares ... )
279+ }
280+ }
281+
282+ func (s * Server ) buildChainHandler (handler http.HandlerFunc ) http.HandlerFunc {
283+ for i := len (s .middlewares ) - 1 ; i >= 0 ; i -- {
284+ handler = s.middlewares [i ](handler )
285+ }
286+ return handler
287+ }
288+
266289func buildRequestWithNewTarget (r * http.Request , target * HttpClientConf ) (* http.Request , error ) {
267290 u := * r .URL
268291 u .Host = target .Target
0 commit comments