Documentation ¶
Overview ¶
Package mw provides a various middleware.
TODO(CS): Make most of those middlewares optionally configurable via config package.
Index ¶
Constants ¶
const ( // MethodOverrideHeader represents a commonly used http header to override a request method. MethodOverrideHeader = "X-HTTP-Method-Override" // MethodOverrideFormKey represents a commonly used HTML form key to override a request method. MethodOverrideFormKey = "_method" )
Variables ¶
This section is empty.
Functions ¶
func Chain ¶
func Chain(h http.Handler, mws ...Middleware) http.Handler
Chain will iterate over all middleware functions, calling them one by one in a chained manner, returning the result of the final middleware.
func ChainFunc ¶
func ChainFunc(hf http.HandlerFunc, mws ...Middleware) http.Handler
ChainFunc will iterate over all middleware functions, calling them one by one in a chained manner, returning the result of the final middleware.
func ErrorWithPanic ¶
ErrorWithPanic implements the ErrorHandler type and panics always. Interesting for testing. This function may leak sensitive information.
Types ¶
type ErrorHandler ¶
ErrorHandler passes an error to an handler and returns the handler with the wrapped error.
func ErrorWithStatusCode ¶
func ErrorWithStatusCode(code int) ErrorHandler
ErrorWithStatusCode wraps an HTTP Status Code into an ErrorHandler. The status text message gets printed first followed by the verbose error string. This function may leak sensitive information.
func LogErrorWithStatusCode ¶
func LogErrorWithStatusCode(l log.Logger, code int) ErrorHandler
LogErrorWithStatusCode same as ErrorWithStatusCode but does not print the error message and logs it instead with level debug or info.
type Middleware ¶
Middleware is a wrapper for the interface http.Handler to create middleware functions.
func WithCloseNotify ¶
func WithCloseNotify(opts ...Option) Middleware
WithCloseNotify returns a net.Handler cancelling the context when the client connection close unexpectedly. Supported options are: SetLogger().
func WithHeader ¶
func WithHeader(kv ...string) Middleware
WithHeader is a middleware that sets multiple HTTP headers. Will panic if kv is imbalanced. len(kv)%2 == 0.
func WithTimeout ¶
func WithTimeout(timeout time.Duration) Middleware
WithTimeout returns a net.Handler which adds a timeout to the context.
Child handlers have the responsibility to obey the context deadline and to return an appropriate error (or not) response in case of timeout.
func WithXHTTPMethodOverride ¶
func WithXHTTPMethodOverride(opts ...Option) Middleware
WithXHTTPMethodOverride adds support for the X-HTTP-Method-Override header. Submitted value will be checked against known methods. Adding HTTPMethodOverrideFormKey to any form will take precedence before HTTP header. If an unknown method will be submitted it gets logged as an Info log. This function is chainable. Suported options are: SetMethodOverrideFormKey() and SetLogger().
type MiddlewareSlice ¶
type MiddlewareSlice []Middleware
MiddlewareSlice a slice full of middleware functions and with function receivers attached
func (MiddlewareSlice) Append ¶
func (c MiddlewareSlice) Append(mws ...Middleware) MiddlewareSlice
Append extends a slice, adding the specified Middleware as the last ones in the request flow.
Append returns a new slice, leaving the original one untouched.
func (MiddlewareSlice) Chain ¶
func (c MiddlewareSlice) Chain(h http.Handler) http.Handler
Chain will iterate over all middleware functions, calling them one by one in a chained manner, returning the result of the final middleware.
func (MiddlewareSlice) ChainFunc ¶
func (c MiddlewareSlice) ChainFunc(hf http.HandlerFunc) http.Handler
Chain will iterate over all middleware functions, calling them one by one in a chained manner, returning the result of the final middleware.
type Option ¶
type Option func(ob *optionBox)
Option contains multiple functional options for middlewares.
func SetMethodOverrideFormKey ¶
SetMethodOverrideFormKey sets a custom form input name