Documentation
¶
Overview ¶
Package mux provide route tree
Example ¶
root := NewNode("GET", 0)
lang := NewNode("{lang:en|pl}", root.MaxParamsSize())
blog := NewNode("blog", lang.MaxParamsSize())
search := NewNode("search", blog.MaxParamsSize())
searchAuthor := NewNode("author", search.MaxParamsSize())
page := NewNode("page", blog.MaxParamsSize())
pageID := NewNode(`{pageId:[^/]+}`, page.MaxParamsSize())
posts := NewNode("posts", blog.MaxParamsSize())
postsID := NewNode(`{postsId:[^/]+}`, posts.MaxParamsSize())
comments := NewNode("comments", blog.MaxParamsSize())
commentID := NewNode(`{commentId:\d+}`, comments.MaxParamsSize())
commentNew := NewNode("new", commentID.MaxParamsSize())
root.WithChildren(root.Tree().withNode(lang))
lang.WithChildren(lang.Tree().withNode(blog))
blog.WithChildren(blog.Tree().withNode(search))
blog.WithChildren(blog.Tree().withNode(page))
blog.WithChildren(blog.Tree().withNode(posts))
blog.WithChildren(blog.Tree().withNode(comments))
search.WithChildren(search.Tree().withNode(searchAuthor))
page.WithChildren(page.Tree().withNode(pageID))
posts.WithChildren(posts.Tree().withNode(postsID))
comments.WithChildren(comments.Tree().withNode(commentID))
commentID.WithChildren(commentID.Tree().withNode(commentNew))
fmt.Printf("Raw tree:\n")
fmt.Print(root.Tree().PrettyPrint())
root.WithChildren(root.Tree().Compile())
fmt.Printf("Compiled tree:\n")
fmt.Print(root.Tree().PrettyPrint())
Output: Raw tree: {lang:en|pl} blog page {pageId:[^/]+} posts {postsId:[^/]+} search author comments {commentId:\d+} new Compiled tree: {lang:en|pl} blog page {pageId:[^/]+} posts {postsId:[^/]+} search/author comments {commentId:\d+} new
Index ¶
- type Node
- type Route
- type Tree
- func (t Tree) Compile() Tree
- func (t Tree) Find(name string) Node
- func (t Tree) Match(path string) (Node, context.Params, string)
- func (t Tree) PrettyPrint() string
- func (t Tree) WithRoute(path string, route Route, maxParamsSize uint8) Tree
- func (t Tree) WithSubrouter(path string, route Route, maxParamsSize uint8) Tree
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Node ¶
type Node interface {
// Match matches given path to Node within Node and its Tree
Match(path string) (Node, context.Params, string)
// Name privides Node name
Name() string
// Name privides maximum number of parameters Route can have for given Node
MaxParamsSize() uint8
// Tree provides next level Node Tree
Tree() Tree
// Route provides Node's Route if assigned
Route() Route
// WithRoute assigns Route to given Node
WithRoute(r Route)
// WithChildren sets Node's Tree
WithChildren(t Tree)
}
Node represents mux Node Can match path and provide routes
type Route ¶
type Route interface {
Handler() interface{}
AppendMiddleware(m middleware.Middleware)
PrependMiddleware(m middleware.Middleware)
}
Route is an middleware aware route interface
type Tree ¶
type Tree []Node
Tree slice of mux Nodes
func (Tree) Compile ¶ added in v4.2.0
Compile optimizes Tree nodes reducing static nodes depth when possible
func (Tree) PrettyPrint ¶ added in v4.2.0
PrettyPrint prints the tree text representation to console
Click to show internal directories.
Click to hide internal directories.