Documentation ¶
Overview ¶
Package router implements a simple URL pattern muxer router with hierarchical middleware layer.
Index ¶
- Variables
- func Match(pat, path string) (url.Values, bool)
- func Tail(pat, path string) string
- type Route
- func (r *Route) Flush()
- func (r *Route) Forward(uri string)
- func (r *Route) Handle(handler http.HandlerFunc)
- func (r *Route) Match(path string) (url.Values, bool)
- func (r *Route) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (r *Route) SetParent(parent layer.Middleware)
- func (r *Route) Use(handler interface{}) *Route
- func (r *Route) UseFinalHandler(handler http.Handler) *Route
- func (r *Route) UsePhase(phase string, handler interface{}) *Route
- type Router
- func (r *Router) All(path string) *Route
- func (r *Router) Delete(path string) *Route
- func (r *Router) FindRoute(method, path string) (*Route, error)
- func (r *Router) Flush()
- func (r *Router) Forward(uri string) *Router
- func (r *Router) Get(path string) *Route
- func (r *Router) HandleHTTP(w http.ResponseWriter, req *http.Request, h http.Handler)
- func (r *Router) Head(path string) *Route
- func (r *Router) Options(path string) *Route
- func (r *Router) Patch(path string) *Route
- func (r *Router) Post(path string) *Route
- func (r *Router) Put(path string) *Route
- func (r *Router) Remove(method, path string) bool
- func (r *Router) Route(method, path string) *Route
- func (r *Router) SetParent(parent layer.Middleware)
- func (r *Router) Use(handler ...interface{}) *Router
- func (r *Router) UseFinalHandler(fn http.Handler) *Router
- func (r *Router) UsePhase(phase string, handler ...interface{}) *Router
Constants ¶
This section is empty.
Variables ¶
var DefaultForwarder = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fwd, _ := forward.New(forward.PassHostHeader(true)) fwd.ServeHTTP(w, r) })
DefaultForwarder stores the default http.Handler to be used to forward the traffic. By default the proxy will reply with 502 Bad Gateway if no custom forwarder is defined.
var ( // ErrNoRouteMatch is returned as typed error when no route can be matched. ErrNoRouteMatch = errors.New("router: cannot match any route") )
Functions ¶
Types ¶
type Route ¶
type Route struct { // Pattern stores the path pattern Pattern string // Layer provides the middleware layer at route level. Layer *layer.Layer // Handler stores the final route handler function. Handler http.Handler }
Route represents an HTTP route based on the given
func (*Route) Handle ¶
func (r *Route) Handle(handler http.HandlerFunc)
Handle defines a custom route final handler function. Use this method only if you really want to handle the route in a very specific way.
func (*Route) Match ¶
Match matches an incoming request againts the registered matchers returning true if all matchers passes.
func (*Route) ServeHTTP ¶
func (r *Route) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP handlers the incoming request and implemented the vinxi specific handler interface.
func (*Route) SetParent ¶
func (r *Route) SetParent(parent layer.Middleware)
SetParent sets a middleware parent layer. This method is tipically called via inversion of control from parent router.
func (*Route) UseFinalHandler ¶
UseFinalHandler attaches a new http.Handler as final middleware layer handler.
type Router ¶
type Router struct { // ForceMethodNotAllowed, if true, is used to return a 405 Method Not Allowed response // in case that the router has no configured routes for the incoming request method. // Defaults to false. ForceMethodNotAllowed bool // NotFound, if set, is used whenever the request doesn't match any // pattern for its method. NotFound should be set before serving any // requests. NotFound http.Handler // Layer provides middleware layer capabilities to the router. Layer *layer.Layer // Routes associates HTTP methods with path patterns handlers. Routes map[string][]*Route }
Router is an HTTP request multiplexer. It matches the URL of each incoming request against a list of registered patterns with their associated methods and calls the handler for the pattern that most closely matches the URL.
Pattern matching attempts each pattern in the order in which they were registered.
Patterns may contain literals or captures. Capture names start with a colon and consist of letters A-Z, a-z, _, and 0-9. The rest of the pattern matches literally. The portion of the URL matching each name ends with an occurrence of the character in the pattern immediately following the name, or a /, whichever comes first. It is possible for a name to match the empty string.
Example pattern with one capture:
/hello/:name
Will match:
/hello/blake /hello/keith
Will not match:
/hello/blake/ /hello/blake/foo /foo /foo/bar
Example 2:
/hello/:name/
Will match:
/hello/blake/ /hello/keith/foo /hello/blake /hello/keith
Will not match:
/foo /foo/bar
A pattern ending with a slash will add an implicit redirect for its non-slash version. For example: Get("/foo/", handler) also registers Get("/foo", handler) as a redirect. You may override it by registering Get("/foo", anotherhandler) before the slash version.
Retrieve the capture from the r.URL.Query().Get(":name") in a handler (note the colon). If a capture name appears more than once, the additional values are appended to the previous values (see http://golang.org/pkg/net/url/#Values)
A trivial example server is:
package main import ( "io" "net/http" "github.com/bmizerany/pat" "log" ) // hello world, the web server func HelloServer(w http.ResponseWriter, req *http.Request) { io.WriteString(w, "hello, "+req.URL.Query().Get(":name")+"!\n") } func main() { m := pat.New() m.Get("/hello/:name", http.HandlerFunc(HelloServer)) // Register this pat with the default serve mux so that other packages // may also be exported. (i.e. /debug/pprof/*) http.Handle("/", m) err := http.ListenAndServe(":12345", nil) if err != nil { log.Fatal("ListenAndServe: ", err) } }
When "Method Not Allowed":
Router knows what methods are allowed given a pattern and a URI. For convenience, Router will add the Allow header for requests that match a pattern for a method other than the method requested and set the Status to "405 Method Not Allowed".
If the NotFound handler is set, then it is used whenever the pattern doesn't match the request path for the current method (and the Allow header is not altered).
func (*Router) FindRoute ¶
FindRoute tries to find a registered route who matches with the given method and path.
func (*Router) Get ¶
Get will register a pattern for GET requests. It also registers pat for HEAD requests. If this needs to be overridden, use Head before Get with pat.
func (*Router) HandleHTTP ¶
HandleHTTP matches r.URL.Path against its routing table using the rules described above.
func (*Router) SetParent ¶
func (r *Router) SetParent(parent layer.Middleware)
SetParent sets a middleware parent layer. This method is tipically called via inversion of control from vinxi layer.
func (*Router) UseFinalHandler ¶
UseFinalHandler uses a new middleware handler function as final handler.