go-proxy/options.go

56 lines
1.3 KiB
Go

package proxy
import (
"context"
"net/http/httputil"
"net/url"
)
type Options struct {
Middlewares []Middleware
RequestTransformers []RequestTransformer
ResponseTransformers []ResponseTransformer
ReverseProxyFactory ReverseProxyFactory
}
func defaultOptions() *Options {
return &Options{
Middlewares: make([]Middleware, 0),
RequestTransformers: make([]RequestTransformer, 0),
ResponseTransformers: make([]ResponseTransformer, 0),
ReverseProxyFactory: DefaultReverseProxyFactory,
}
}
type OptionFunc func(*Options)
func WithMiddlewares(middlewares ...Middleware) OptionFunc {
return func(o *Options) {
o.Middlewares = middlewares
}
}
func WithRequestTransformers(transformers ...RequestTransformer) OptionFunc {
return func(o *Options) {
o.RequestTransformers = transformers
}
}
func WithResponseTransformers(transformers ...ResponseTransformer) OptionFunc {
return func(o *Options) {
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)
}