mux

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2024 License: MIT Imports: 5 Imported by: 1

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error

func Error(err error, status int, responseMsg ...string) 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

func Methods(options ...methodOption) http.Handler

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

func WithDELETE(h http.Handler) methodOption

WithDELETE will register the handler against method DELETE

func WithGET

func WithGET(h http.Handler) methodOption

WithGET will register the handler against method GET

func WithMethod

func WithMethod(method string, h http.Handler) methodOption

WithMethod will register the handler against the http method

func WithOPTIONS

func WithOPTIONS(h http.Handler) methodOption

WithOPTIONS will register the handler against method OPTIONS. Provide if you need to use a custom OPTIONS handler for this path.

func WithPATCH

func WithPATCH(h http.Handler) methodOption

WithPATCH will register the handler against method PATCH

func WithPOST

func WithPOST(h http.Handler) methodOption

WithPOST will register the handler against method POST

func WithPUT

func WithPUT(h http.Handler) methodOption

WithPUT will register the handler against method PUT

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

Err will accept a handler that can return an error and handle it according to the errFunc provided or http.Error by default.

type Middleware

type Middleware func(http.Handler) http.Handler

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.

func (*Mux) ServeHTTP

func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP satisfies the handler interface.

Jump to

Keyboard shortcuts

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