v1.3.0 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2021 License: Apache-2.0 Imports: 20 Imported by: 0



Package proxy provides proxy and proxy middleware interfaces and implementations.



View Source
const Namespace = ""

Namespace to be used in extra config


View Source
var (
	// ErrNoBackends is the error returned when an endpoint has no backends defined
	ErrNoBackends = errors.New("all endpoints must have at least one backend")
	// ErrTooManyBackends is the error returned when an endpoint has too many backends defined
	ErrTooManyBackends = errors.New("too many backends for this proxy")
	// ErrTooManyProxies is the error returned when a middleware has too many proxies defined
	ErrTooManyProxies = errors.New("too many proxies for this proxy middleware")
	// ErrNotEnoughProxies is the error returned when an endpoint has not enough proxies defined
	ErrNotEnoughProxies = errors.New("not enough proxies for this endpoint")
View Source
var DefaultHTTPResponseParserConfig = HTTPResponseParserConfig{
	func(_ io.Reader, _ *map[string]interface{}) error { return nil },
	EntityFormatterFunc(func(r Response) Response { return r }),

DefaultHTTPResponseParserConfig defines a default HTTPResponseParserConfig


func CloneRequestHeaders

func CloneRequestHeaders(headers map[string][]string) map[string][]string

CloneRequestHeaders returns a copy of the received request headers

func CloneRequestParams

func CloneRequestParams(params map[string]string) map[string]string

CloneRequestParams returns a copy of the received request params

func NewReadCloserWrapper

func NewReadCloserWrapper(ctx context.Context, in io.ReadCloser) io.Reader

NewReadCloserWrapper Creates a new closeable io.Read

func RegisterResponseCombiner

func RegisterResponseCombiner(name string, f ResponseCombiner)

RegisterResponseCombiner adds a new response combiner into the internal register


type BackendFactory

type BackendFactory func(remote *config.Backend) Proxy

BackendFactory creates a proxy based on the received backend configuration

func CustomHTTPProxyFactory

func CustomHTTPProxyFactory(cf client.HTTPClientFactory) BackendFactory

CustomHTTPProxyFactory returns a BackendFactory. The Proxies it creates will use the received HTTPClientFactory

func HTTPProxyFactory

func HTTPProxyFactory(client *http.Client) BackendFactory

HTTPProxyFactory returns a BackendFactory. The Proxies it creates will use the received net/http.Client

type EntityFormatter

type EntityFormatter interface {
	Format(Response) Response

EntityFormatter formats the response data

func NewEntityFormatter

func NewEntityFormatter(remote *config.Backend) EntityFormatter

NewEntityFormatter creates an entity formatter with the received backend definition

type EntityFormatterFunc

type EntityFormatterFunc func(Response) Response

EntityFormatterFunc holds the formatter function

func (EntityFormatterFunc) Format

func (e EntityFormatterFunc) Format(entity Response) Response

Format implements the EntityFormatter interface

type Factory

type Factory interface {
	New(cfg *config.EndpointConfig) (Proxy, error)

Factory creates proxies based on the received endpoint configuration.

Both, factories and backend factories, create proxies but factories are designed as a stack makers because they are intended to generate the complete proxy stack for a given frontend endpoint the app would expose and they could wrap several proxies provided by a backend factory

func DefaultFactory

func DefaultFactory(logger logging.Logger) Factory

DefaultFactory returns a default http proxy factory with the injected logger

func DefaultFactoryWithSubscriber

func DefaultFactoryWithSubscriber(logger logging.Logger, sF sd.SubscriberFactory) Factory

DefaultFactoryWithSubscriber returns a default proxy factory with the injected logger and subscriber factory

func NewDefaultFactory

func NewDefaultFactory(backendFactory BackendFactory, logger logging.Logger) Factory

NewDefaultFactory returns a default proxy factory with the injected proxy builder and logger

func NewDefaultFactoryWithSubscriber

func NewDefaultFactoryWithSubscriber(backendFactory BackendFactory, logger logging.Logger, sF sd.SubscriberFactory) Factory

NewDefaultFactoryWithSubscriber returns a default proxy factory with the injected proxy builder, logger and subscriber factory

func NewShadowFactory

func NewShadowFactory(f Factory) Factory

NewShadowFactory creates a new shadowFactory using the provided Factory

type FactoryFunc

type FactoryFunc func(*config.EndpointConfig) (Proxy, error)

FactoryFunc type is an adapter to allow the use of ordinary functions as proxy factories. If f is a function with the appropriate signature, FactoryFunc(f) is a Factory that calls f.

func (FactoryFunc) New

func (f FactoryFunc) New(cfg *config.EndpointConfig) (Proxy, error)

New implements the Factory interface

type HTTPResponseParser

type HTTPResponseParser func(context.Context, *http.Response) (*Response, error)

HTTPResponseParser defines how the response is parsed from http.Response to Response object

func DefaultHTTPResponseParserFactory

func DefaultHTTPResponseParserFactory(cfg HTTPResponseParserConfig) HTTPResponseParser

DefaultHTTPResponseParserFactory is the default implementation of HTTPResponseParserFactory

type HTTPResponseParserConfig

type HTTPResponseParserConfig struct {
	Decoder         encoding.Decoder
	EntityFormatter EntityFormatter

HTTPResponseParserConfig contains the config for a given HttpResponseParser

type HTTPResponseParserFactory

type HTTPResponseParserFactory func(HTTPResponseParserConfig) HTTPResponseParser

HTTPResponseParserFactory creates HTTPResponseParser from a given HTTPResponseParserConfig

type Metadata

type Metadata struct {
	Headers    map[string][]string
	StatusCode int

Metadata is the Metadata of the Response which contains Headers and StatusCode

type Middleware

type Middleware func(next ...Proxy) Proxy

Middleware adds a middleware, decorator or wrapper over a collection of proxies, exposing a proxy interface.

Proxy middlewares can be stacked:

var p Proxy
p := EmptyMiddleware(NoopProxy)
response, err := p(ctx, r)

func NewConcurrentMiddleware

func NewConcurrentMiddleware(remote *config.Backend) Middleware

NewConcurrentMiddleware creates a proxy middleware that enables sending several requests concurrently

func NewFlatmapMiddleware added in v1.2.0

func NewFlatmapMiddleware(cfg *config.EndpointConfig) Middleware

NewFlatmapMiddleware creates a proxy middleware that enables applying flatmap operations to the proxy response

func NewLoadBalancedMiddleware added in v1.1.0

func NewLoadBalancedMiddleware(remote *config.Backend) Middleware

NewLoadBalancedMiddleware creates proxy middleware adding the most perfomant balancer over a default subscriber

func NewLoadBalancedMiddlewareWithSubscriber added in v1.1.0

func NewLoadBalancedMiddlewareWithSubscriber(subscriber sd.Subscriber) Middleware

NewLoadBalancedMiddlewareWithSubscriber creates proxy middleware adding the most perfomant balancer over the received subscriber

func NewLoggingMiddleware

func NewLoggingMiddleware(logger logging.Logger, name string) Middleware

NewLoggingMiddleware creates proxy middleware for logging requests and responses

func NewMergeDataMiddleware

func NewMergeDataMiddleware(endpointConfig *config.EndpointConfig) Middleware

NewMergeDataMiddleware creates proxy middleware for merging responses from several backends

func NewRandomLoadBalancedMiddleware

func NewRandomLoadBalancedMiddleware(remote *config.Backend) Middleware

NewRandomLoadBalancedMiddleware creates proxy middleware adding a random balancer over a default subscriber

func NewRandomLoadBalancedMiddlewareWithSubscriber

func NewRandomLoadBalancedMiddlewareWithSubscriber(subscriber sd.Subscriber) Middleware

NewRandomLoadBalancedMiddlewareWithSubscriber creates proxy middleware adding a random balancer over the received subscriber

func NewRequestBuilderMiddleware

func NewRequestBuilderMiddleware(remote *config.Backend) Middleware

NewRequestBuilderMiddleware creates a proxy middleware that parses the request params received from the outter layer and generates the path to the backend endpoints

func NewRoundRobinLoadBalancedMiddleware

func NewRoundRobinLoadBalancedMiddleware(remote *config.Backend) Middleware

NewRoundRobinLoadBalancedMiddleware creates proxy middleware adding a round robin balancer over a default subscriber

func NewRoundRobinLoadBalancedMiddlewareWithSubscriber

func NewRoundRobinLoadBalancedMiddlewareWithSubscriber(subscriber sd.Subscriber) Middleware

NewRoundRobinLoadBalancedMiddlewareWithSubscriber creates proxy middleware adding a round robin balancer over the received subscriber

func NewStaticMiddleware

func NewStaticMiddleware(endpointConfig *config.EndpointConfig) Middleware

NewStaticMiddleware creates proxy middleware for adding static values to the processed responses

type Proxy

type Proxy func(ctx context.Context, request *Request) (*Response, error)

Proxy processes a request in a given context and returns a response and an error

func EmptyMiddleware

func EmptyMiddleware(next ...Proxy) Proxy

EmptyMiddleware is a dummy middleware, useful for testing and fallback

func NewHTTPProxy

func NewHTTPProxy(remote *config.Backend, cf client.HTTPClientFactory, decode encoding.Decoder) Proxy

NewHTTPProxy creates a http proxy with the injected configuration, HTTPClientFactory and Decoder

func NewHTTPProxyDetailed

func NewHTTPProxyDetailed(remote *config.Backend, re client.HTTPRequestExecutor, ch client.HTTPStatusHandler, rp HTTPResponseParser) Proxy

NewHTTPProxyDetailed creates a http proxy with the injected configuration, HTTPRequestExecutor, Decoder and HTTPResponseParser

func NewHTTPProxyWithHTTPExecutor

func NewHTTPProxyWithHTTPExecutor(remote *config.Backend, re client.HTTPRequestExecutor, dec encoding.Decoder) Proxy

NewHTTPProxyWithHTTPExecutor creates a http proxy with the injected configuration, HTTPRequestExecutor and Decoder

func NewShadowProxy

func NewShadowProxy(p1, p2 Proxy) Proxy

NewShadowProxy returns a Proxy that sends requests to p1 and p2 but ignores the response of p2

func ShadowMiddleware

func ShadowMiddleware(next ...Proxy) Proxy

ShadowMiddleware is a Middleware that creates a shadowProxy

type Register

type Register struct {
	// contains filtered or unexported fields

func NewRegister

func NewRegister() *Register

func (Register) GetResponseCombiner

func (r Register) GetResponseCombiner(name string) (ResponseCombiner, bool)

func (Register) SetResponseCombiner

func (r Register) SetResponseCombiner(name string, rc ResponseCombiner)

type Request

type Request struct {
	Method  string
	URL     *url.URL
	Query   url.Values
	Path    string
	Body    io.ReadCloser
	Params  map[string]string
	Headers map[string][]string

Request contains the data to send to the backend

func CloneRequest

func CloneRequest(r *Request) *Request

CloneRequest returns a deep copy of the received request, so the received and the returned proxy.Request do not share a pointer

func (*Request) Clone

func (r *Request) Clone() Request

Clone clones itself into a new request. The returned cloned request is not thread-safe, so changes on request.Params and request.Headers could generate race-conditions depending on the part of the pipe they are being executed. For thread-safe request headers and/or params manipulation, use the proxy.CloneRequest function.

func (*Request) GeneratePath

func (r *Request) GeneratePath(URLPattern string)

GeneratePath takes a pattern and updates the path of the request

type Response

type Response struct {
	Data       map[string]interface{}
	IsComplete bool
	Metadata   Metadata
	Io         io.Reader

Response is the entity returned by the proxy

func NoOpHTTPResponseParser

func NoOpHTTPResponseParser(ctx context.Context, resp *http.Response) (*Response, error)

NoOpHTTPResponseParser is a HTTPResponseParser implementation that just copies the http response body into the proxy response IO

func NoopProxy

func NoopProxy(_ context.Context, _ *Request) (*Response, error)

NoopProxy is a do nothing proxy, useful for testing

type ResponseCombiner

type ResponseCombiner func(int, []*Response) *Response

ResponseCombiner func to merge the collected responses into a single one

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL