Documentation
¶
Overview ¶
Package routing provides a naive router with regular expression support.
When using named sub groups in a regex path, the named groups matched expression will be set on the request's context by it's name. So if you register the route ^/foo/(?P<param>[0-9]+)$, you can get it by doing *http.Request.Context().Value("param") in your handler.
The routes can be supplied with metadata though a fluent API to generate HTML documentation.
If regular expression matchings overlap, they take precedence by the order they have been added.
Index ¶
- func WriteDocs(out io.Writer, name string, version string, baseURL string, router *Router) error
- type API
- type Body
- type ErrInvalidRouteMatchType
- type HTTPArgument
- type MatchType
- type Middleware
- type Middlewarer
- type PathArgument
- type QueryParameter
- type Route
- func (r *Route) AddMiddleware(mw Middlewarer) *Route
- func (r *Route) AddPathArgument(a *PathArgument) *Route
- func (r *Route) AddQueryParameter(p *QueryParameter) *Route
- func (route *Route) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (r *Route) SetDescription(d string) *Route
- func (r *Route) SetHandler(h http.Handler) *Route
- func (r *Route) SetHandlerFunc(h http.HandlerFunc) *Route
- func (r *Route) SetMethod(m string) *Route
- func (r *Route) SetRequestBody(b *Body) *Route
- func (r *Route) SetResponse(b *Body) *Route
- func (r *Route) SetTag(t string) *Route
- type Router
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ErrInvalidRouteMatchType ¶
type ErrInvalidRouteMatchType struct {
// contains filtered or unexported fields
}
func (*ErrInvalidRouteMatchType) Error ¶
func (e *ErrInvalidRouteMatchType) Error() string
type HTTPArgument ¶
type HTTPArgument struct { Name string Example interface{} }
func NewHTTPArgument ¶
func NewHTTPArgument(name string, example interface{}) *HTTPArgument
type Middlewarer ¶
type Middlewarer interface { Middleware() Middleware Name() string Description() string }
Middlewarer represents the interface for middlewares that can be documented automatically.
type PathArgument ¶
type PathArgument HTTPArgument
func NewPathArgument ¶
func NewPathArgument(name string, example interface{}) *PathArgument
type QueryParameter ¶
type QueryParameter HTTPArgument
func NewQueryParameter ¶
func NewQueryParameter(name string, example interface{}) *QueryParameter
type Route ¶
type Route struct { MatchType MatchType Rex *regexp.Regexp Handler http.Handler RequestBody *Body Response *Body QueryParameters []*QueryParameter PathArguments []*PathArgument Method string Middleware []Middlewarer Tag string Description string }
Type Route represents a path and is registered with the Router.
A Route exposesa a fluid api to set metadata that is used to create an HTML document.
func NewRexRoute ¶
func (*Route) AddMiddleware ¶
func (r *Route) AddMiddleware(mw Middlewarer) *Route
AddMiddleware adds a middleware to a route. Middlewares are sorted by their name.
func (*Route) AddPathArgument ¶
func (r *Route) AddPathArgument(a *PathArgument) *Route
func (*Route) AddQueryParameter ¶
func (r *Route) AddQueryParameter(p *QueryParameter) *Route
func (*Route) SetDescription ¶
func (*Route) SetHandlerFunc ¶
func (r *Route) SetHandlerFunc(h http.HandlerFunc) *Route
func (*Route) SetRequestBody ¶
func (*Route) SetResponse ¶
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
func (*Router) Match ¶
Match will search and return the route registered for the HTTP method and URL path.
Returns nil if none found.
If two regex pattern match, the one added sooner takes precedence.
func (*Router) RoutesForTag ¶
RoutesFor Tag returns all routes for t sorted by path.
It returns an empty slice even if there are no routes for t.