Documentation ¶
Overview ¶
Package mux is a minimal web framework that wraps the https://github.com/julienschmidt/httprouter.
Index ¶
- Constants
- Variables
- func FromJSON(r io.Reader, v interface{}) error
- func IsShutdownError(err error) bool
- func NewShutdownError(message string) error
- func ToJSON(ctx context.Context, w http.ResponseWriter, data interface{}, status int) error
- type Error
- type HandleFunc
- type Middleware
- type Params
- type Router
- func (mr *Router) Delete(path string, handleFunc HandleFunc)
- func (mr *Router) Get(path string, handleFunc HandleFunc)
- func (mr *Router) Handle(method, path string, handleFunc HandleFunc)
- func (mr *Router) MiddlewareHandle(method, path string, handleFunc HandleFunc, middlewares ...Middleware)
- func (mr *Router) Patch(path string, handleFunc HandleFunc)
- func (mr *Router) Post(path string, handleFunc HandleFunc)
- func (mr *Router) Put(path string, handleFunc HandleFunc)
- func (mr *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (mr *Router) SignalShutdown()
- func (mr *Router) With(middleware Middleware, middlewares ...Middleware) *withMiddleware
- type ShutdownChannel
- type State
Constants ¶
const Key = keyType(0)
Key is a key to stores and retrieves the State from the request context.
Variables ¶
var ErrJSONDecoding = errors.New("mux: error decodes JSON")
var ErrStateMissing = errors.New("mux: the initial state is missing from the request context")
ErrStateMissing is the error if the initial state is missing from the request context.
Functions ¶
func FromJSON ¶
FromJSON reads JSON-encoded data from the request body and stores it in the value pointed to by v
func IsShutdownError ¶
IsShutdownError checks if the wrapped error contains the *errorShutdown
func NewShutdownError ¶
NewShutdownError returns an error that causes the framework to signal a graceful shutdown.
Types ¶
type Error ¶
Error is used to pass an error during the request through the application with web specific context.
func NewRequestError ¶
NewRequestError wraps a provided error with an HTTP status code. This function should be used when handlers encounter expected (trusted) errors.
type HandleFunc ¶
type HandleFunc func(w http.ResponseWriter, r *http.Request) error
HandleFunc is just a http.HandleFunc that can returns an error. By returning an error, now we can make a centralized error handling.
type Middleware ¶
type Middleware func(handleFunc HandleFunc) HandleFunc
Middleware is a function that will be executed before or/and after the given handleFunc has been executed.
There are two types of middleware in this Router. First is the Global middlewares and the second is the Route middlewares. The Global middlewares will be applied to all handleFunc, meanwhile the Route middlewares only applied specific to the handleFunc in the given route.
type Params ¶
type Params struct {
// contains filtered or unexported fields
}
Params is a Param-slice, as returned by the router. The slice is ordered, the first URL parameter is also the first slice value. It is therefore safe to read values by the index.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router is an HTTP request multiplexer. It matches the URL of each incoming request against a list of registered patterns and calls the handler for the pattern that most closely matches the URL.
func NewRouter ¶
func NewRouter(sc ShutdownChannel, middlewares ...Middleware) *Router
NewRouter creates a new mux router.
func (*Router) Delete ¶
func (mr *Router) Delete(path string, handleFunc HandleFunc)
Delete is the syntactic sugar for Handle("DELETE", path, handleFunc)
func (*Router) Get ¶
func (mr *Router) Get(path string, handleFunc HandleFunc)
Get is the syntactic sugar for Handle("GET", path, handleFunc)
func (*Router) Handle ¶
func (mr *Router) Handle(method, path string, handleFunc HandleFunc)
Handle registers the handleFunc for the given HTTP method and URL path.
func (*Router) MiddlewareHandle ¶
func (mr *Router) MiddlewareHandle(method, path string, handleFunc HandleFunc, middlewares ...Middleware)
MiddlewareHandle registers the handleFunc with the Route middlewares for the given HTTP method and URL path.
func (*Router) Patch ¶
func (mr *Router) Patch(path string, handleFunc HandleFunc)
Patch is the syntactic sugar for Handle("PATCH", path, handleFunc)
func (*Router) Post ¶
func (mr *Router) Post(path string, handleFunc HandleFunc)
Post is the syntactic sugar for Handle("POST", path, handleFunc)
func (*Router) Put ¶
func (mr *Router) Put(path string, handleFunc HandleFunc)
Put is the syntactic sugar for Handle("PUT", path, handleFunc)
func (*Router) ServeHTTP ¶
func (mr *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements the http.Handler interface.
func (*Router) SignalShutdown ¶
func (mr *Router) SignalShutdown()
SignalShutdown sends a shutdown signal through the shutdown channel.
func (*Router) With ¶
func (mr *Router) With(middleware Middleware, middlewares ...Middleware) *withMiddleware
With is a helper method to make Handle with middleware at the route level more easier to read. For example:
Without using With the Handle method looks like this:
mux.Handle("GET", "/some-path", handleFunc, []Middleware{m1, m2, m3,...mk})
By using With it will look like this:
mux.With(m1, m2, m3, ..., mk).Handle("GET", "/some-path", handleFunc)
type ShutdownChannel ¶
ShutdownChannel is a channel that Mux used to tell the application to shutdown gracefully by sending a termination signal (syscall.SIGTERM).