mw

package
v0.0.0-...-202847b Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2023 License: Apache-2.0 Imports: 6 Imported by: 18

Documentation

Overview

Package mw provides a various middleware.

TODO(CS): Make most of those middlewares optionally configurable via config package.

Index

Constants

View Source
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

func ErrorWithPanic(err error) http.Handler

ErrorWithPanic implements the ErrorHandler type and panics always. Interesting for testing. This function may leak sensitive information.

Types

type ErrorHandler

type ErrorHandler func(error) http.Handler

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

type Middleware func(http.Handler) http.Handler

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

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 SetLogger

func SetLogger(l log.Logger) Option

SetLogger sets a logger to a middleware

func SetMethodOverrideFormKey

func SetMethodOverrideFormKey(k string) Option

SetMethodOverrideFormKey sets a custom form input name

Jump to

Keyboard shortcuts

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