muxy

package module
v0.0.0-...-65b4fd5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2017 License: BSD-3-Clause Imports: 1 Imported by: 1

README

muxy

GoDoc Build Status

Package gorilla/muxy takes gorilla/mux to the next level.

This is a work in progress: not stable, not usable etc.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Var

func Var(r *http.Request, name string) string

Var returns the route variable with the given name from the request context.

The returned value may be empty if the variable wasn't set.

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.

func (*Route) Delete

func (r *Route) Delete(h http.Handler) *Route

Delete sets the given handler to be served for the request method DELETE.

func (*Route) Get

func (r *Route) Get(h http.Handler) *Route

Get sets the given handler to be served for the request method GET.

func (*Route) Handle

func (r *Route) Handle(h http.Handler, methods ...string) *Route

Handle sets the given handler to be served for the optional request methods.

func (*Route) Head

func (r *Route) Head(h http.Handler) *Route

Head sets the given handler to be served for the request method HEAD.

func (*Route) Name

func (r *Route) Name(name string) *Route

Name defines the route name used for URL building.

func (*Route) Options

func (r *Route) Options(h http.Handler) *Route

Options sets the given handler to be served for the request method OPTIONS.

func (*Route) Patch

func (r *Route) Patch(h http.Handler) *Route

Patch sets the given handler to be served for the request method PATCH.

func (*Route) Post

func (r *Route) Post(h http.Handler) *Route

Post sets the given handler to be served for the request method POST.

func (*Route) Put

func (r *Route) Put(h http.Handler) *Route

Put sets the given handler to be served for the request method PUT.

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 New

func New(m Matcher) *Router

New creates a new Router for the given matcher.

func (*Router) Group

func (r *Router) Group(pattern string) *Router

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

func (r *Router) Mount(src *Router) *Router

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

func (r *Router) Name(name string) *Router

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) Route

func (r *Router) Route(pattern string) *Route

Route creates a new Route for the given pattern.

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP dispatches to the handler whose pattern matches the request.

func (*Router) URL

func (r *Router) URL(name string, vars ...string) string

URL returns a URL string for the given route name and variables.

func (*Router) Use

func (r *Router) Use(middleware ...func(http.Handler) http.Handler) *Router

Use appends the given middleware to this router.

type Variable

type Variable string

Variable is a type used to set and retrieve route variables from the request context.

Directories

Path Synopsis
matchers

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL