routing

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2017 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MethodMatcher

func MethodMatcher(route *Route, request *http.Request) bool

MethodMatcher will check to see if the request method matches on of the routes allowed methods.

Types

type Matcher

type Matcher interface {
	// Match should run conditionals to check if the route and request match based on requirements.
	Match(route *Route, request *http.Request) bool
}

Matcher is used to check if the given route matches a request based on the conditions set out in the Match function.

type MatcherFunc

type MatcherFunc func(route *Route, request *http.Request) bool

MatcherFunc is an adapter to allow the use of ordinary functions as a Matcher.

func (MatcherFunc) Match

func (matcher MatcherFunc) Match(route *Route, request *http.Request) bool

Match calls matcher(route, request)

type Route

type Route struct {
	// contains filtered or unexported fields
}

Route should contain all information needed to match a url path to a handler bound on the route.

func NewRoute

func NewRoute(path string, methods []string, handler http.Handler) *Route

NewRoute will create a new Route and set a default MethodMatcher.

func (*Route) AddMatcher

func (r *Route) AddMatcher(matcher Matcher)

AddMatcher will add a Matcher to the list. These are used to check if the route matches a specific request criteria.

func (*Route) Matches

func (r *Route) Matches(request *http.Request) bool

Matches will run all matchers to check if this route matches the passed in request.

func (*Route) Methods

func (r *Route) Methods() []string

Methods will return a list of request methods that this route will respond to.

func (*Route) Name

func (r *Route) Name() string

Name can be used to lookup a route by its name.

func (*Route) Path

func (r *Route) Path() string

Path will return the required pattern used to match the route.

func (*Route) SetHandler

func (r *Route) SetHandler(handler http.Handler)

SetHandler will set the handler that will be called if the route is matched.

func (*Route) SetMethods

func (r *Route) SetMethods(methods ...string)

SetMethods will normalise and set the request methods for this route based on http.Method*.

func (*Route) SetName

func (r *Route) SetName(name string)

SetName will normalise and set the name of the route.

func (*Route) SetPath

func (r *Route) SetPath(path string)

SetPath will normalise and set the pattern used to match the route.

type RouteCollection

type RouteCollection struct {
	// contains filtered or unexported fields
}

RouteCollection provides helpful ways of dealing with collections of Routes.

func NewRouteCollection

func NewRouteCollection() *RouteCollection

NewRouteCollection creates an empty RouteCollection.

func (*RouteCollection) Add

func (collection *RouteCollection) Add(route *Route)

Add a route to the collection. If the route has a name assigned it will be added to the list of named routes.

func (*RouteCollection) Count

func (collection *RouteCollection) Count() int

Count will return the total number of routes in the collection.

func (*RouteCollection) RefreshNamedRoutes

func (collection *RouteCollection) RefreshNamedRoutes()

RefreshNamedRoutes will clear the named routes list and add all named routes back from the all routes list. This is useful if a name is assigned to a route after it has been added to the collection.

func (*RouteCollection) RouteByName

func (collection *RouteCollection) RouteByName(name string) *Route

RouteByName can be used to lookup a route by its name.

func (*RouteCollection) RoutesByPath

func (collection *RouteCollection) RoutesByPath(path string) *RouteMatchGroup

RoutesByPath will lookup routes in the route trie and return a RoutMatchGroup containing any matched routes and the parsed params.

type RouteMatchGroup

type RouteMatchGroup struct {
	Routes    []*Route
	UrlParams map[string]string
}

RouteMatchGroup is used to encapsulate found routes, when looked up by path, with the parsed named params from the url.

type Router

type Router interface {
	http.Handler

	Dispatch(response http.ResponseWriter, request *http.Request)
	SetNotFoundHandler(handler http.Handler)

	Get(path string, handler http.Handler)
	Post(path string, handler http.Handler)
	Put(path string, handler http.Handler)
	Patch(path string, handler http.Handler)
	Delete(path string, handler http.Handler)
	Any(path string, handler http.Handler)
	Match(path string, handler http.Handler, methods ...string)
}

Router manages dispatching of requests to route handlers.

func NewRouter

func NewRouter() Router

NewRouter will create a new router instance with an empty collection and a default NotFoundHandler.

func NewRouterFromCollection

func NewRouterFromCollection(collection *RouteCollection) Router

NewRouterFromCollection will create a new router instance with a default NotFoundHandler and set the collection.

Jump to

Keyboard shortcuts

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