Documentation ¶
Overview ¶
Package ctxmux provides an opinionated mux. It builds on the context library and combines it with the httprouter library known for it's performance. The equivalent of the ServeHTTP in ctxmux is:
ServeHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request) error
It provides a hook to control context creation when a request arrives. Additionally an error can be returned which is passed thru to the error handler. The error handler is responsible for sending a response and possibly logging it as necessary. Similarly panics are also handled and passed to the panic handler.
Index ¶
- func ContextParams(ctx context.Context) httprouter.Params
- func WithParams(ctx context.Context, p httprouter.Params) context.Context
- type Handler
- type Mux
- func (m *Mux) DELETE(path string, h Handler)
- func (m *Mux) GET(path string, h Handler)
- func (m *Mux) HEAD(path string, h Handler)
- func (m *Mux) Handler(method, path string, h Handler)
- func (m *Mux) PATCH(path string, h Handler)
- func (m *Mux) POST(path string, h Handler)
- func (m *Mux) PUT(path string, h Handler)
- func (m *Mux) ServeHTTP(w http.ResponseWriter, req *http.Request)
- type MuxOption
- func MuxContextMaker(f func(*http.Request) (context.Context, error)) MuxOption
- func MuxErrorHandler(h func(context.Context, http.ResponseWriter, *http.Request, error)) MuxOption
- func MuxNotFoundHandler(h Handler) MuxOption
- func MuxPanicHandler(h func(context.Context, http.ResponseWriter, *http.Request, interface{})) MuxOption
- func MuxRedirectTrailingSlash() MuxOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContextParams ¶
func ContextParams(ctx context.Context) httprouter.Params
ContextParams extracts out the params from the context if possible.
func WithParams ¶
WithParams returns a new context.Context instance with the params included.
Types ¶
type Handler ¶
Handler is an augmented http.Handler.
func HTTPHandler ¶
HTTPHandler calls the underlying http.Handler.
func HTTPHandlerFunc ¶
func HTTPHandlerFunc(h http.HandlerFunc) Handler
HTTPHandlerFunc calls the underlying http.HandlerFunc.
type Mux ¶
type Mux struct {
// contains filtered or unexported fields
}
Mux provides shared context initialization and error handling.
type MuxOption ¶
MuxOption are used to set various mux options.
func MuxContextMaker ¶
MuxContextMaker sets a function to create the context for each request. The default context is context.Background(). If the assigned function returns an error, it will be passed to the error handler.
func MuxErrorHandler ¶
MuxErrorHandler configures a function which is invoked for errors returned by a Handler. If one isn't set, the default behaviour is to log it and send a static error message of "internal server error".
func MuxNotFoundHandler ¶
MuxNotFoundHandler configures a Handler that is invoked for requests where one isn't found.
func MuxPanicHandler ¶
func MuxPanicHandler(h func(context.Context, http.ResponseWriter, *http.Request, interface{})) MuxOption
MuxPanicHandler configures a function which is invoked for panics raised while serving a request. If one is not configured, the default behavior is what the net/http package does; which is to print a trace and ignore it.
func MuxRedirectTrailingSlash ¶
func MuxRedirectTrailingSlash() MuxOption
MuxRedirectTrailingSlash configures the Mux to automatically handling missing or extraneous trailing slashes by redirecting.