Documentation
¶
Overview ¶
Package r2 implements a minimalist HTTP request routing helper for Go.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Defer ¶
Defer pushes the `f` onto the stack of functions that will be called after the matched `http.Handler` for the `req` returns.
func PathParams ¶
PathParams returns parsed path parameters of the `req`.
Note that the returned `url.Values` is always non-nil, unless the `req` is not from the `http.Handler` returned by the `Router.Handler`.
Types ¶
type Middleware ¶
type Middleware interface {
// ChainHTTPHandler chains the `next` to the returned `http.Handler`.
//
// Typically, the returned `http.Handler` is a closure which does
// something with the `http.ResponseWriter` and `http.Request` passed to
// it, and then calls the `next`.
ChainHTTPHandler(next http.Handler) http.Handler
}
Middleware is used by the `Router` to chain the `http.Handler`s.
type MiddlewareFunc ¶
MiddlewareFunc is an adapter to allow the use of an ordinary function as a `Middleware`.
func (MiddlewareFunc) ChainHTTPHandler ¶
func (mwf MiddlewareFunc) ChainHTTPHandler(next http.Handler) http.Handler
ChainHTTPHandler implements the `Middleware`.
type Router ¶
type Router struct {
// Parent is the parent `Router`.
Parent *Router
// PathPrefix is the path prefix of all routes to be registered.
PathPrefix string
// Middlewares is the `Middleware` chain that performs after routing.
Middlewares []Middleware
// NotFoundHandler writes 404 not found responses.
//
// If the `NotFoundHandler` is nil, a default one is used.
//
// Note that the `NotFoundHandler` will be ignored when the `Parent` is
// not nil.
NotFoundHandler http.Handler
// MethodNotAllowedHandler writes 405 method not allowed responses.
//
// If the `MethodNotAllowedHandler` is nil, a default one is used.
//
// Note that the `MethodNotAllowedHandler` will be ignored when the
// `Parent` is not nil.
MethodNotAllowedHandler http.Handler
// TSRHandler writes TSR (trailing slash redirect) responses.
//
// If the `TSRHandler` is nil, a default one is used.
//
// Note that the `TSRHandler` will be ignored when the `Parent` is not
// nil.
TSRHandler http.Handler
// contains filtered or unexported fields
}
Router is the registry of all registered routes for request matching.
func (*Router) Handle ¶
func (r *Router) Handle(method, path string, h http.Handler, ms ...Middleware)
Handle registers a new route for the `method` ("" means any) and `path` with the matching `h` and optional `ms`.
Note that a ':' followed by a name in the `path` declares a path parameter that matches all characters except '/'. And an '*' in the `path` declares a wildcard path parameter that greedily matches all characters, with "*" as its name. The `PathParams` can be used to get those declared path parameters after a request is matched.