feat: allow modification of proxy via custom factory function
Cadoles/go-proxy/pipeline/head This commit looks good Details

This commit is contained in:
wpetit 2023-07-01 13:41:11 -06:00
parent e2dc3e1a03
commit c6b3d482cc
2 changed files with 27 additions and 1 deletions

View File

@ -1,9 +1,16 @@
package proxy package proxy
import (
"context"
"net/http/httputil"
"net/url"
)
type Options struct { type Options struct {
Middlewares []Middleware Middlewares []Middleware
RequestTransformers []RequestTransformer RequestTransformers []RequestTransformer
ResponseTransformers []ResponseTransformer ResponseTransformers []ResponseTransformer
ReverseProxyFactory ReverseProxyFactory
} }
func defaultOptions() *Options { func defaultOptions() *Options {
@ -11,6 +18,7 @@ func defaultOptions() *Options {
Middlewares: make([]Middleware, 0), Middlewares: make([]Middleware, 0),
RequestTransformers: make([]RequestTransformer, 0), RequestTransformers: make([]RequestTransformer, 0),
ResponseTransformers: make([]ResponseTransformer, 0), ResponseTransformers: make([]ResponseTransformer, 0),
ReverseProxyFactory: DefaultReverseProxyFactory,
} }
} }
@ -33,3 +41,15 @@ func WithResponseTransformers(transformers ...ResponseTransformer) OptionFunc {
o.ResponseTransformers = transformers o.ResponseTransformers = transformers
} }
} }
type ReverseProxyFactory func(ctx context.Context, target *url.URL) *httputil.ReverseProxy
func WithReverseProxyFactory(fn ReverseProxyFactory) OptionFunc {
return func(o *Options) {
o.ReverseProxyFactory = fn
}
}
func DefaultReverseProxyFactory(ctx context.Context, target *url.URL) *httputil.ReverseProxy {
return httputil.NewSingleHostReverseProxy(target)
}

View File

@ -16,6 +16,7 @@ type Proxy struct {
handler http.Handler handler http.Handler
responseTransformers []ResponseTransformer responseTransformers []ResponseTransformer
requestTransformers []RequestTransformer requestTransformers []RequestTransformer
reverseProxyFactory ReverseProxyFactory
} }
// ServeHTTP implements http.Handler // ServeHTTP implements http.Handler
@ -27,6 +28,7 @@ func (p *Proxy) proxyRequest(w http.ResponseWriter, r *http.Request) {
var reverser *httputil.ReverseProxy var reverser *httputil.ReverseProxy
key := fmt.Sprintf("%s://%s", r.URL.Scheme, r.URL.Host) key := fmt.Sprintf("%s://%s", r.URL.Scheme, r.URL.Host)
ctx := r.Context()
createAndStore := func() { createAndStore := func() {
target := &url.URL{ target := &url.URL{
@ -38,7 +40,10 @@ func (p *Proxy) proxyRequest(w http.ResponseWriter, r *http.Request) {
return return
} }
reverser = httputil.NewSingleHostReverseProxy(target) reverser = p.reverseProxyFactory(ctx, target)
if reverser == nil {
return
}
originalDirector := reverser.Director originalDirector := reverser.Director
@ -97,6 +102,7 @@ func New(funcs ...OptionFunc) *Proxy {
proxy.handler = createMiddlewareChain(handler, opts.Middlewares) proxy.handler = createMiddlewareChain(handler, opts.Middlewares)
proxy.requestTransformers = opts.RequestTransformers proxy.requestTransformers = opts.RequestTransformers
proxy.responseTransformers = opts.ResponseTransformers proxy.responseTransformers = opts.ResponseTransformers
proxy.reverseProxyFactory = opts.ReverseProxyFactory
return proxy return proxy
} }