Documentation
¶
Overview ¶
Package mux is an extension of the net/http package, with some added benefits: path prefix groupings, flexible middleware handling, a wrapper for returning errors from handlers, and register handlers for each method per path.
Index ¶
- func Error(err error, status int, responseMsg ...string) error
- func Methods(options ...methodOption) http.Handler
- func WithDELETE(h http.Handler) methodOption
- func WithGET(h http.Handler) methodOption
- func WithMethod(method string, h http.Handler) methodOption
- func WithOPTIONS(h http.Handler) methodOption
- func WithPATCH(h http.Handler) methodOption
- func WithPOST(h http.Handler) methodOption
- func WithPUT(h http.Handler) methodOption
- func WrapMiddleware(mw []Middleware, handler http.Handler) http.Handler
- type ErrHandlerFunc
- type ErrorHandler
- type Middleware
- type Mux
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Error ¶
Error will return an error that can be used by the ErrorHandler. The error itself is not sent back to the client, but logged instead. The status and optional responseMsg are both used to respond to the client.
func Methods ¶
Methods will return a handler that will gate handlers by method for a path. If no OPTIONS handler was provided, one will be created.
func WithDELETE ¶
WithDELETE will register the handler against method DELETE
func WithMethod ¶
WithMethod will register the handler against the http method
func WithOPTIONS ¶
WithOPTIONS will register the handler against method OPTIONS. Provide if you need to use a custom OPTIONS handler for this path.
func WrapMiddleware ¶
func WrapMiddleware(mw []Middleware, handler http.Handler) http.Handler
Types ¶
type ErrHandlerFunc ¶
type ErrHandlerFunc func(w http.ResponseWriter, r *http.Request) error
ErrHandlerFunc is the function signature for handlers that return an error.
type ErrorHandler ¶
type ErrorHandler struct {
ErrWriter io.Writer
ErrFunc func(w http.ResponseWriter, error string, code int)
}
ErrorHandler holds resources for returning errors from handlers. If the writer is nil, it will not write the error to it. You can use the writer to capture a log of errors being returned to the handler. The errFunc uses http.Error if no function is provided.
func (*ErrorHandler) Err ¶
func (eh *ErrorHandler) Err(h ErrHandlerFunc) http.Handler
Err will accept a handler that can return an error and handle it according to the errFunc provided or http.Error by default.
type Mux ¶
type Mux struct {
// contains filtered or unexported fields
}
Mux wraps the http.ServeMux and provides a mechanism for registering middleware
func New ¶
func New(mw ...Middleware) *Mux
New will return an instance of a new Mux. The provided middleware will wrap every handler registered to the Mux.
func (*Mux) Group ¶
func (m *Mux) Group(prefix string, h http.Handler, mw ...Middleware)
Group will register the provided handler under the prefix. The prefix must end with a trailing slash.
func (*Mux) Handle ¶
func (m *Mux) Handle(pattern string, handler http.Handler, mw ...Middleware)
Handle will register the provided handler on the mux, wrapped in the provided middleware(s). Middleware is envoked from left to right per request, after any mux level middleware.
func (*Mux) HandleFunc ¶
func (m *Mux) HandleFunc(pattern string, handler http.HandlerFunc, mw ...Middleware)
HandleFunc will register the provided handler function on the mux, wrapped in the provided middleware(s). Middleware is envoked from left to right per request, after any mux level middleware.