Now, in net/http, for server side, we have Handler & HandlerFunc, HandlerFunc is the convenient way for user to define new handler, but in client side, we only have RoundTripper, so I proposal to add RoundTripperFunc to net/http.
With this new type, we can easily implement RoundTripper interface.
In modern web application, middleware pattern is widely used. With Middleware we can add more action before/after handler/request call.
For the server side, we can define Middleware or similar:
typeMiddlewareinterface {
Next(h http.Handler) http.Handler
}
// MiddlewareFn support wrap function with same signature as Middleware.typeMiddlewareFnfunc(h http.Handler) http.Handlerfunc (fnMiddlewareFn) Next(h http.Handler) http.Handler { returnfn(h) }
// and compose chains of middleware// ComposeInterceptor compose interceptors to given http.RoundTripperfuncComposeInterceptor(rt http.RoundTripper, interceptors...Interceptor) http.RoundTripper {
iflen(interceptors) ==0 {
returnrt
}
returnComposeInterceptor(interceptors[0].Next(rt), interceptors[1:]...)
}
For the client side, we define Interceptor or similar:
typeInterceptorinterface {
Next(fn http.RoundTripper) http.RoundTripper
}
// InterceptorFn implement Interceptor for convenient usage.typeInterceptorFnfunc(rt http.RoundTripper) http.RoundTripperfunc (fnInterceptorFn) Next(rt http.RoundTripper) http.RoundTripper { returnfn(rt) }
// and a function compose chains of interceptorfuncComposeInterceptor(rt http.RoundTripper, interceptors...Interceptor) http.RoundTripper {
iflen(interceptors) ==0 {
returnrt
}
returnComposeInterceptor(interceptors[0].Next(rt), interceptors[1:]...)
}
All the above are not necessary, but can reduce and simplify user's boilerplate code.
Please consider this proposal.
The text was updated successfully, but these errors were encountered:
bradfitz
changed the title
net/http: add RoundTripperFunc and Middleware for server & client
proposal: net/http: add RoundTripperFunc and Middleware for server & client
Apr 16, 2020
I definitely want the HTTP client to be composable like the server is and was assuming that would happen as part of the new HTTP client (#23707). I don't think there's a clean way to add it to the existing client so I'm not very excited about this particular proposal.
I definitely want the HTTP client to be composable like the server is and was assuming that would happen as part of the new HTTP client (#23707). I don't think there's a clean way to add it to the existing client so I'm not very excited about this particular proposal.
It's sound good to me, is there any plan on that issue.
Now, in
net/http
, for server side, we haveHandler
&HandlerFunc
,HandlerFunc
is the convenient way for user to define new handler, but in client side, we only haveRoundTripper
, so I proposal to addRoundTripperFunc
tonet/http
.With this new type, we can easily implement
RoundTripper
interface.In modern web application, middleware pattern is widely used. With
Middleware
we can add more action before/after handler/request call.For the server side, we can define
Middleware
or similar:For the client side, we define
Interceptor
or similar:All the above are not necessary, but can reduce and simplify user's boilerplate code.
Please consider this proposal.
The original code is middleware, interceptor & RoundTripperFunc
The text was updated successfully, but these errors were encountered: