Documentation ¶
Overview ¶
Package route provides http package-compatible routing library. It can route http requests by hostname, method, path and headers.
Route defines simple language for matching requests based on Go syntax. Route provides series of matchers that follow the syntax:
Matcher("value") // matches value using trie Matcher("<string>.value") // uses trie-based matching for a.value and b.value MatcherRegexp(".*value") // uses regexp-based matching
Host matcher:
Host("<subdomain>.localhost") // trie-based matcher for a.localhost, b.localhost, etc. HostRegexp(".*localhost") // regexp based matcher
Path matcher:
Path("/hello/<value>") // trie-based matcher for raw request path PathRegexp("/hello/.*") // regexp-based matcher for raw request path
Method matcher:
Method("GET") // trie-based matcher for request method MethodRegexp("POST|PUT") // regexp based matcher for request method
Header matcher:
Header("Content-Type", "application/<subtype>") // trie-based matcher for headers HeaderRegexp("Content-Type", "application/.*") // regexp based matcher for headers
Matchers can be combined using && operator:
Host("localhost") && Method("POST") && Path("/v1")
Route library will join the trie-based matchers into one trie matcher when possible, for example:
Host("localhost") && Method("POST") && Path("/v1") Host("localhost") && Method("GET") && Path("/v2")
Will be combined into one trie for performance. If you add a third route:
Host("localhost") && Method("GET") && PathRegexp("/v2/.*")
It wont be joined ito the trie, and would be matched separately instead.
Index ¶
- func IsValid(expr string) bool
- type Mux
- func (m *Mux) AddAlias(match, replace string)
- func (m *Mux) GetNotFound() http.Handler
- func (m *Mux) Handle(expr string, handler http.Handler) error
- func (m *Mux) HandleFunc(expr string, handler func(http.ResponseWriter, *http.Request)) error
- func (m *Mux) InitHandlers(handlers map[string]interface{}) error
- func (m *Mux) IsValid(expr string) bool
- func (m *Mux) Remove(expr string) error
- func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (m *Mux) SetNotFound(n http.Handler) error
- type Router
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Mux ¶
type Mux struct {
// contains filtered or unexported fields
}
Mux implements router compatible with http.Handler
func (*Mux) AddAlias ¶
AddAlias adds an alias for matchers in an expression. If the string in `match` matches any part of an expression added via `Mux.Handle()` then the match is replaced with the value of `alias`.
func (*Mux) GetNotFound ¶
func (*Mux) HandleFunc ¶
HandleFunc adds http handler function for route expression
func (*Mux) InitHandlers ¶
InitHandlers This adds a map of handlers and expressions in a single call. This allows init to load many rules on first startup, thus reducing the time it takes to create the initial mux.
type Router ¶
type Router interface { // GetRoute returns a route by a given expression, returns nil if expression is not found GetRoute(string) interface{} // AddRoute adds a route to match by expression, // returns error if the expression already defined, or route expression is incorrect AddRoute(string, interface{}) error // RemoveRoute removes a route for a given expression RemoveRoute(string) error // UpsertRoute updates an existing route or adds a new route by given expression UpsertRoute(string, interface{}) error // InitRoutes Initializes the routes, // this method clobbers all existing routes and should only be called during init InitRoutes(map[string]interface{}) error // Route takes a request and matches it against requests, returns matched route in case if found, // nil if there's no matching route or error in case of internal error. Route(*http.Request) (interface{}, error) }
Router implements http request routing and operations. It is a generic router not conforming to http.Handler interface, to get a handler conforming to http.Handler interface, use Mux router instead.