Documentation
¶
Overview ¶
Package mux implements a high performance and powerful trie based url path router for Go.
Index ¶
- Constants
- func Param(r *http.Request, key string) string
- func Params(r *http.Request) map[string]string
- type Matched
- type Mux
- func (m *Mux) DefaultHandler(handler http.HandlerFunc)
- func (m *Mux) Delete(pattern string, handler http.HandlerFunc)
- func (m *Mux) Get(pattern string, handler http.HandlerFunc)
- func (m *Mux) Handle(method, pattern string, handler http.HandlerFunc)
- func (m *Mux) Handler(method, path string, handler http.Handler)
- func (m *Mux) Head(pattern string, handler http.HandlerFunc)
- func (m *Mux) Options(pattern string, handler http.HandlerFunc)
- func (m *Mux) Patch(pattern string, handler http.HandlerFunc)
- func (m *Mux) Post(pattern string, handler http.HandlerFunc)
- func (m *Mux) Put(pattern string, handler http.HandlerFunc)
- func (m *Mux) ServeHTTP(w http.ResponseWriter, req *http.Request)
- type Node
- type Options
- type Trie
Constants ¶
const Version = "0.0.1"
Version holds the current mux version
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Matched ¶
type Matched struct {
// Either a Node pointer when matched or nil
Node *Node
// Either a map contained matched values or empty map.
Params map[string]string
// Matched path to access
// If Node is nil then redirect to this PATH
Path string
}
Matched is a result returned by Trie.Match.
type Mux ¶
type Mux struct {
// contains filtered or unexported fields
}
Mux is a tire base HTTP request router which can be used to dispatch requests to different handler functions.
func (*Mux) DefaultHandler ¶
func (m *Mux) DefaultHandler(handler http.HandlerFunc)
DefaultHandler registers a new handler in the Mux that will run if there is no other handler matching.
func (*Mux) Delete ¶
func (m *Mux) Delete(pattern string, handler http.HandlerFunc)
Delete registers a new DELETE route for a path with matching handler in the Mux.
func (*Mux) Get ¶
func (m *Mux) Get(pattern string, handler http.HandlerFunc)
Get registers a new GET route for a path with matching handler in the Mux.
func (*Mux) Handle ¶
func (m *Mux) Handle(method, pattern string, handler http.HandlerFunc)
Handle registers a new handler with method and path in the Mux. For GET, POST, PUT, PATCH and DELETE requests the respective shortcut functions can be used.
func (*Mux) Handler ¶
Handler is an adapter which allows the usage of an http.Handler as a request handle.
func (*Mux) Head ¶
func (m *Mux) Head(pattern string, handler http.HandlerFunc)
Head registers a new HEAD route for a path with matching handler in the Mux.
func (*Mux) Options ¶
func (m *Mux) Options(pattern string, handler http.HandlerFunc)
Options registers a new OPTIONS route for a path with matching handler in the Mux.
func (*Mux) Patch ¶
func (m *Mux) Patch(pattern string, handler http.HandlerFunc)
Patch registers a new PATCH route for a path with matching handler in the Mux.
func (*Mux) Post ¶
func (m *Mux) Post(pattern string, handler http.HandlerFunc)
Post registers a new POST route for a path with matching handler in the Mux.
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node represents a node on defined patterns that can be matched.
func (*Node) GetAllow ¶
GetAllow returns allow methods defined on the node
trie := New()
trie.Parse("/").Handle("GET", handler1)
trie.Parse("/").Handle("PUT", handler2)
// trie.Match("/").Node.GetAllow() == []string{"GET", "PUT"}
func (*Node) GetHandler ¶
GetHandler ... GetHandler returns handler by method that defined on the node
trie := New()
trie.Parse("/api").Handle("GET", func handler1() {})
trie.Parse("/api").Handle("PUT", func handler2() {})
trie.Match("/api").Node.GetHandler("GET").(func()) == handler1
trie.Match("/api").Node.GetHandler("PUT").(func()) == handler2
type Options ¶
type Options struct {
// CaseSensitive when matching URL path.
CaseSensitive bool
// PathClean defines the path cleaning behavior for new routes. The default value is false.
// Users should be careful about which routes are not cleaned
// When true, the path will be cleaned, if the route path is "/path//to", it will return "/path/to"
// When false, if the route path is "/path//to", it will remain with the double slash
PathClean bool
// StrictSlash defines the trailing slash behavior for new routes.
// The initial value is false.
// When true, if the route path is "/path/", accessing "/path" will
// redirect to the former and vice versa. In other words,
// your application will always see the path as specified in the route.
// When false, if the route path is "/path", accessing "/path/" will
// not match this route and vice versa.
StrictSlash bool
// UseEncodedPath tells the router to match the encoded original path to the routes.
// For eg. "/path/foo%2Fbar/to" will match the path "/path/{var}/to".
// This behavior has the drawback of needing to match routes against r.RequestURI instead of r.URL.Path.
// Any modifications (such as http.StripPrefix) to r.URL.Path will not affect routing when this flag is
// on and thus may induce unintended behavior.
// If not called, the router will match the unencoded path to the routes.
// For eg. "/path/foo%2Fbar/to" will match the path "/path/foo/bar/to"
UseEncodedPath bool
}
Options describes options for Trie.
type Trie ¶
type Trie struct {
// contains filtered or unexported fields
}
Trie represents a trie that defining patterns and matching URL.
func NewTrie ¶
NewTrie returns a trie
trie := New()
// disable CaseSensitive, PathClean and StrictSlash
trie := New(Options{})