router

package
v0.0.0-...-206adc0 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2014 License: MIT, Apache-2.0 Imports: 7 Imported by: 0

README

Router

A router with Pat-style path patterns.

API Docs: https://godoc.org/github.com/tedsuo/router

A key advantage is the ability to creates Routes as structs, indepentent of the router and the corresponding route handlers. This allows you to reuse the routes in multiple contexts. For example, a proxy server and a backend server can be created by having one set of Routes, but two sets of Handlers (one handler that proxies, another that serves the request). By sharing the same route structure, the type system ensures the two servers stay in sync. Likewise, your client code can use the routes to construct requests to the server, ensuring the client and server don't drift apart.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRouter

func NewRouter(routes Routes, handlers Handlers) (http.Handler, error)

NewRouter combines a set of Routes with their corresponding Handlers to produce a http request multiplexer (AKA a "router").

Types

type Handlers

type Handlers map[string]http.Handler

Handlers map Handler keys to http.Handler objects. The Handler key is used to then match the Handler to the appropriate Route in the Router.

type Params

type Params map[string]string

Params map path keys to values. For example, if your route has the path pattern:

/person/:person_id/pets/:pet_type

Then a correct Params map would lool like:

router.Params{
  "person_id": "123",
  "pet_type": "cats",
}

type RequestGenerator

type RequestGenerator struct {
	Header http.Header
	// contains filtered or unexported fields
}

func NewRequestGenerator

func NewRequestGenerator(host string, routes Routes) *RequestGenerator

func (*RequestGenerator) RequestForHandler

func (r *RequestGenerator) RequestForHandler(
	handler string,
	params Params,
	body io.Reader,
) (*http.Request, error)

type Route

type Route struct {
	// Handler is a key specifying which HTTP handler the router
	// should associate with the endpoint at runtime.
	Handler string
	// Method is one of the following: GET,PUT,POST,DELETE
	Method string
	// Path contains a path pattern
	Path string
}

A Route defines properties of an HTTP endpoint. At runtime, the router will associate each Route with a http.Handler object, and use the Route properties to determine which Handler should be invoked.

Currently, properties used in match are such as Method and Path.

Method is one of the following:

GET PUT POST DELETE

Path conforms to Pat-style pattern matching. The following docs are taken from http://godoc.org/github.com/bmizerany/pat#PatternServeMux

Path 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

func (Route) PathWithParams

func (r Route) PathWithParams(params Params) (string, error)

PathWithParams combines the route's path pattern with a Params map to produce a valid path.

type Routes

type Routes []Route

Routes is a Route collection.

func (Routes) PathForHandler

func (r Routes) PathForHandler(handler string, params Params) (string, error)

PathForHandler looks up a Route by it's Handler key and computes it's path with a given Params map.

func (Routes) RouteForHandler

func (r Routes) RouteForHandler(handler string) (Route, bool)

RouteForHandler looks up a Route by it's Handler key.

func (Routes) Router

func (r Routes) Router(handlers Handlers) (http.Handler, error)

Router is deprecated, please use router.NewRouter() instead

Jump to

Keyboard shortcuts

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