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 ¶
- func If(cond func(ctx context.Context, w http.ResponseWriter, r *http.Request) bool, ...) func(next HandlerC) HandlerC
- func New(ctx context.Context, h HandlerC) http.Handler
- func TimeoutHandler(timeout time.Duration) func(next HandlerC) HandlerC
- type Chain
- func (c Chain) Handler(xh HandlerC) http.Handler
- func (c Chain) HandlerC(xh HandlerC) HandlerC
- func (c Chain) HandlerCF(xhc HandlerFuncC) HandlerC
- func (c Chain) HandlerCtx(ctx context.Context, xh HandlerC) http.Handler
- func (c Chain) HandlerF(hf http.HandlerFunc) http.Handler
- func (c Chain) HandlerFC(xhf HandlerFuncC) http.Handler
- func (c Chain) HandlerH(h http.Handler) http.Handler
- func (c *Chain) Use(f func(next http.Handler) http.Handler)
- func (c *Chain) UseC(f func(next HandlerC) HandlerC)
- type HandlerC
- type HandlerFuncC
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) HandlerCF ¶
func (c Chain) HandlerCF(xhc HandlerFuncC) HandlerC
HandlerCF wraps the provided final handler func with all the middleware appended to the chain and returns a HandlerC instance.
HandlerCF is equivalent to:
c.HandlerC(xhandler.HandlerFuncC(xhc))
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.
func (Chain) HandlerF ¶
func (c Chain) HandlerF(hf http.HandlerFunc) http.Handler
HandlerF is an helper to provide a standard http handler function (http.HandlerFunc) to Handler(). Your final handler won't have access the context though.
func (Chain) HandlerFC ¶
func (c Chain) HandlerFC(xhf HandlerFuncC) http.Handler
HandlerFC is an helper to provide a function (HandlerFuncC) to Handler().
HandlerFC is equivalent to:
c.Handler(xhandler.HandlerFuncC(xhc))
func (Chain) HandlerH ¶
HandlerH is an helper to provide a standard http handler (http.HandlerFunc) to Handler(). Your final handler won't have access the context though.
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.HandlerFuncC(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).