Documentation ¶
Overview ¶
Package xhandler provides a bridge between http.Handler and net/context.
xhandler enforces net/context in your handlers without sacrificing compatibility with existing http.Handlers nor imposing a specific router.
Thanks to net/context deadline management, xhandler is able to enforce a per request deadline and will cancel the context in when the client close the connection unexpectedly.
You may create net/context aware middlewares pretty much the same way as you would do with http.Handler.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func If ¶
func If(cond func(ctx context.Context, w http.ResponseWriter, r *http.Request) bool, condNext func(next HandlerC) HandlerC) func(next HandlerC) HandlerC
If is a special handler that will skip insert the condNext handler only if a condition applies at runtime.
func New ¶
New creates a conventional http.Handler injecting the provided root context to sub handlers. This handler is used as a bridge between conventional http.Handler and context aware handlers.
func TimeoutHandler ¶
TimeoutHandler returns a Handler which adds a timeout to the context.
Child handlers have the responsability to obey the context deadline and to return an appropriate error (or not) response in case of timeout.
Types ¶
type Chain ¶
Chain is an helper to chain middleware handlers together for an easier management.
func (Chain) Handler ¶
Handler wraps the provided final handler with all the middleware appended to the chain and return a new standard http.Handler instance. The context.Background() context is injected automatically.
func (Chain) HandlerC ¶
HandlerC wraps the provided final handler with all the middleware appended to the chain and returns a HandlerC instance.
func (Chain) HandlerCtx ¶
HandlerCtx wraps the provided final handler with all the middleware appended to the chain and return a new standard http.Handler instance.
type HandlerC ¶
HandlerC is a net/context aware http.Handler
func CloseHandler ¶
CloseHandler returns a Handler cancelling the context when the client connection close unexpectedly.
type HandlerFuncC ¶
HandlerFuncC type is an adapter to allow the use of ordinary functions as a xhandler.Handler. If f is a function with the appropriate signature, xhandler.HandlerFunc(f) is a xhandler.Handler object that calls f.
func (HandlerFuncC) ServeHTTPC ¶
func (f HandlerFuncC) ServeHTTPC(ctx context.Context, w http.ResponseWriter, r *http.Request)
ServeHTTPC calls f(ctx, w, r).