Documentation ¶
Index ¶
- func Var(r *http.Request, name string) string
- type Matcher
- type Route
- func (r *Route) Delete(h http.Handler) *Route
- func (r *Route) Get(h http.Handler) *Route
- func (r *Route) Handle(h http.Handler, methods ...string) *Route
- func (r *Route) Head(h http.Handler) *Route
- func (r *Route) Name(name string) *Route
- func (r *Route) Options(h http.Handler) *Route
- func (r *Route) Patch(h http.Handler) *Route
- func (r *Route) Post(h http.Handler) *Route
- func (r *Route) Put(h http.Handler) *Route
- type Router
- func (r *Router) Group(pattern string) *Router
- func (r *Router) Mount(src *Router) *Router
- func (r *Router) Name(name string) *Router
- func (r *Router) Route(pattern string) *Route
- func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (r *Router) URL(name string, vars ...string) string
- func (r *Router) Use(middleware ...func(http.Handler) http.Handler) *Router
- type Variable
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Matcher ¶
type Matcher interface { // Route returns a Route for the given pattern. Route(pattern string) (*Route, error) // Match matches registered routes against the incoming request and // stores URL variables in the request context. Match(r *http.Request) (http.Handler, *http.Request) // Build returns a URL string for the given route and variables. Build(r *Route, vars ...string) (string, error) }
Matcher registers patterns as routes and matches requests.
type Route ¶
type Route struct { // Router holds the router that registered this route. Router *Router // Pattern holds the route pattern. Pattern string // Noun holds the route name. Noun string // Handlers maps request methods to the handlers that will handle them. Handlers map[string]http.Handler }
Route stores a URL pattern to be matched and the handler to be served in case of a match, optionally mapping HTTP methods to different handlers.
type Router ¶
type Router struct { // Router holds the main router referenced by subrouters. Router *Router // Pattern holds the pattern prefix used to create new routes. Pattern string // Noun holds the name prefix used to create new routes. Noun string // Middleware holds the middleware to apply in new routes. Middleware []func(http.Handler) http.Handler // Routes maps all routes to their correspondent patterns. Routes map[*Route]string // NamedRoutes maps route names to their correspondent routes. NamedRoutes map[string]*Route // contains filtered or unexported fields }
Router matches the URL of incoming requests against registered routes and calls the appropriate handler.
func (*Router) Group ¶
Group creates a group for the given pattern prefix. All routes registered in the resulting router will prepend the prefix to its pattern. For example:
// Create a new router. r := muxy.New(matcher) // Create a group for the routes that share pattern prefix "/admin". g := r.Group("/admin") // Register a route in the admin group, and add handlers for two HTTP // methods. These handlers will be served for the path "/admin/products". g.Route("/products").Get(listProducts).Post(updateProducts)
func (*Router) Mount ¶
Mount imports all routes from the given router into this one.
Combined with Group() and Name(), it is possible to submount a router defined in a different package using pattern and name prefixes. For example:
// Create a new router. r := muxy.New(matcher) // Create a group for the routes starting with the pattern "/admin", // set the name prefix as "admin:" and register all routes from the // external router. g := r.Group("/admin").Name("admin:").Mount(admin.Router)
func (*Router) Name ¶
Name sets the name prefix used for new routes. All routes registered in the resulting router will prepend the prefix to its name.
func (*Router) ServeHTTP ¶
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP dispatches to the handler whose pattern matches the request.