Documentation ¶
Overview ¶
Package middleware will hold common use HTTP middlewares. The middlewares are totally compliant with the standard lib interfaces and with most of the routers out there.
Index ¶
- func AllMethods() []string
- func For(h http.Handler, m ...Middleware) http.Handler
- type AuthConfig
- type AuthConfigFunc
- type Authorization
- type DefaultHeaders
- type EndpointMapper
- type Middleware
- func AuthChecker(cfgFunc AuthConfigFunc) Middleware
- func PanicHandler(panicHandler PanicHandlerFunc) Middleware
- func RequestDurationObserver(registry prometheus.Registerer, buckets []float64, ...) Middleware
- func RequestLogger(logger *logrus.Entry, level logrus.Level) Middleware
- func ResponseHeaders(headers DefaultHeaders) Middleware
- func ResponseSizeObserver(registry prometheus.Registerer, buckets []float64, ...) Middleware
- type PanicHandlerFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllMethods ¶
func AllMethods() []string
AllMethods is a helper that provides all available HTTP methods.
Useful whenever we want to configure ALL methods.
func For ¶
func For(h http.Handler, m ...Middleware) http.Handler
For will take the handler as first parameter. The variadic part of function accepts any number of middlewares that will be called in the passed order. Beware that the handler will always be called as the last element of the chain, after all the middlewares are called.
Types ¶
type AuthConfig ¶
type AuthConfig struct { Methods []string Paths []*regexp.Regexp Auth Authorization }
AuthConfig are the elements the AuthConfigFunc needs to return in order to configure the AuthChecker middleware.
Having the possibility of returning multiple of this configurations in AuthConfigFunc allows configuring different user accesses for maybe different set of endpoints.
func NewAuthConfig ¶
func NewAuthConfig() *AuthConfig
NewAuthConfig returns a builder pattern that helps to build a config for setup the AuthChecker middleware.
func (*AuthConfig) WithAuth ¶
func (a *AuthConfig) WithAuth(auth Authorization) *AuthConfig
WithAuth maps the user/password permissions. See Authorization for more information.
func (*AuthConfig) WithMethods ¶
func (a *AuthConfig) WithMethods(m []string) *AuthConfig
WithMethods configures the methods that are going to be protected.
func (*AuthConfig) WithPathRegex ¶
func (a *AuthConfig) WithPathRegex(p string) *AuthConfig
WithPathRegex appends a regex pattern to be matched with the input URL of the user.
The HTTP paths that matches this regex's will be protected. This method can be called multiple times to add multiple regex.
type AuthConfigFunc ¶
type AuthConfigFunc func() []*AuthConfig
AuthConfigFunc is the only one input parameter of the AuthChecker middleware. It must return an []*AuthConfig, That can come from any source, like a database.
If the source is going to be a database, its encouraged, to some kind of caching, as this is going to be executed for EVERY request.
type Authorization ¶
Authorization represent the user/encrypted-password table. The keys are reserved for the user and the values for their respective bcrypt encrypted passwords.
type DefaultHeaders ¶
func (DefaultHeaders) Set ¶
func (d DefaultHeaders) Set(k, v string)
type EndpointMapper ¶
EndpointMapper represents a way to map an http.Request.URL.String(), to its respective handler. This responsibility is delegated to the client code. http.Request.URL.String() cannot be mapped directly to a metrics label due to combinatorial explosion. This means a path can contain /product/[0-9] and each ID will generate a new whole time series in a metrics system like prometheus.
The recommendation is to wrap some efficient implementation for properly matching prefixes. The recommendation is to use something like a Radix tree implementation. See https://github.com/hashicorp/go-immutable-radix .
type Middleware ¶
Middleware accepts the next http.Handler as parameter and returns current one that may modify request/writer and finally calls the handler passed as parameter.
func AuthChecker ¶
func AuthChecker(cfgFunc AuthConfigFunc) Middleware
AuthChecker is a middleware to prevent/allow access to a specific combination of HTTP method/path. Read AuthConfig for more information about how to configure this middleware. This middleware should be the executed just before your business logic.
func PanicHandler ¶
func PanicHandler(panicHandler PanicHandlerFunc) Middleware
PanicHandler its a middleware that will intercept panics and act according to an user provided function (See PanicHandlerFunc).
It should be invoked before any other handler logic and other middlewares, protecting them from panics.
Its only able to intercept panics that happens in the same goroutine.
func RequestDurationObserver ¶
func RequestDurationObserver(registry prometheus.Registerer, buckets []float64, endpointMapper EndpointMapper) Middleware
RequestDurationObserver will observe the duration of all the requests and register them on a given Prometheus registry.
It will use an histogram where the client code can define its custom buckets. Labels will reflect the HTTP method, code and the endpoint mapped by EndpointMapper.
func RequestLogger ¶
func RequestLogger(logger *logrus.Entry, level logrus.Level) Middleware
RequestLogger will log the client request information on each request at Debug level. Accepts logrus as logger.
func ResponseHeaders ¶
func ResponseHeaders(headers DefaultHeaders) Middleware
ResponseHeaders middleware will setup the default response headers.
It will override any other previous settled headers in the http.ResponseWriter. Downstream handlers can still modify the default headers.
Users must take into account where this middleware its placed in the handler chain. i.e adding this middleware too early, can cause other middlewares to overwrite the wanted defaults. And vice-versa, putting this too late, will override other middleware headers.
func ResponseSizeObserver ¶
func ResponseSizeObserver(registry prometheus.Registerer, buckets []float64, endpointMapper EndpointMapper) Middleware
ResponseSizeObserver will observe the size of the body of all the responses.
It will use an histogram where the client code can define its custom buckets. Labels will reflect the HTTP method, code and the endpoint mapped by EndpointMapper.
type PanicHandlerFunc ¶
type PanicHandlerFunc func(v interface{})
PanicHandlerFunc represents the function that will handle the panic. This is an user provided func.