Documentation ¶
Overview ¶
Package trie implements a minimal and powerful trie based url path router (or mux) for Go.
Index ¶
Constants ¶
const Version = "1.5.2"
Version is trie-mux version
Variables ¶
This section is empty.
Functions ¶
This section is empty.
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 // If FixedPathRedirect enabled, it may returns a redirect path, // otherwise a empty string. FPR string // If TrailingSlashRedirect enabled, it may returns a redirect path, // otherwise a empty string. TSR string }
Matched is a result returned by Trie.Match.
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node represents a node on defined patterns that can be matched.
func (*Node) GetAllow ¶ added in v1.1.0
GetAllow returns allow methods defined on the node
trie := New() trie.Define("/").Handle("GET", handler1) trie.Define("/").Handle("PUT", handler2) // trie.Match("/").Node.GetAllow() == "GET, PUT"
func (*Node) GetDescendants ¶ added in v1.5.0
GetDescendants returns all descendants nodes.
func (*Node) GetHandler ¶ added in v1.1.0
GetHandler ... GetHandler returns handler by method that defined on the node
trie := New() trie.Define("/api").Handle("GET", func handler1() {}) trie.Define("/api").Handle("PUT", func handler2() {}) trie.Match("/api").Node.GetHandler("GET").(func()) == handler1 trie.Match("/api").Node.GetHandler("PUT").(func()) == handler2
func (*Node) GetMethods ¶ added in v1.5.0
GetMethods returns methods defined on the node
func (*Node) GetPattern ¶ added in v1.4.2
GetPattern returns pattern defined on the node
type Options ¶
type Options struct { // Ignore case when matching URL path. IgnoreCase bool // If enabled, the trie will detect if the current path can't be matched but // a handler for the fixed path exists. // Matched.FPR will returns either a fixed redirect path or an empty string. // For example when "/api/foo" defined and matching "/api//foo", // The result Matched.FPR is "/api/foo". FixedPathRedirect bool // If enabled, the trie will detect if the current path can't be matched but // a handler for the path with (without) the trailing slash exists. // Matched.TSR will returns either a redirect path or an empty string. // For example if /foo/ is requested but a route only exists for /foo, the // client is redirected to /foo // For example when "/api/foo" defined and matching "/api/foo/", // The result Matched.TSR is "/api/foo". TrailingSlashRedirect bool }
Options is options for Trie.
type Trie ¶
type Trie struct {
// contains filtered or unexported fields
}
Trie represents a trie that defining patterns and matching URL.
func New ¶
New returns a trie
trie := New() // disable IgnoreCase, TrailingSlashRedirect and FixedPathRedirect trie := New(Options{})
func (*Trie) Define ¶
Define define a pattern on the trie and returns the endpoint node for the pattern.
trie := New() node1 := trie.Define("/a") node2 := trie.Define("/a/b") node3 := trie.Define("/a/b") // node2.parent == node1 // node2 == node3
The defined pattern can contain three types of parameters:
| Syntax | Description | |--------|------| | `:name` | named parameter | | `:name*` | named with catch-all parameter | | `:name(regexp)` | named with regexp parameter | | `::name` | not named parameter, it is literal `:name` |
func (*Trie) GetEndpoints ¶ added in v1.5.0
GetEndpoints returns all endpoint nodes.