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).sort())
lang.WithChildren(lang.Tree().withNode(blog).sort())
blog.WithChildren(blog.Tree().withNode(search).sort())
blog.WithChildren(blog.Tree().withNode(page).sort())
blog.WithChildren(blog.Tree().withNode(posts).sort())
blog.WithChildren(blog.Tree().withNode(comments).sort())
search.WithChildren(search.Tree().withNode(searchAuthor).sort())
page.WithChildren(page.Tree().withNode(pageID).sort())
posts.WithChildren(posts.Tree().withNode(postsID).sort())
comments.WithChildren(comments.Tree().withNode(commentID).sort())
commentID.WithChildren(commentID.Tree().withNode(commentNew).sort())
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 MiddlewareAware
- type Node
- type Route
- type RouteAware
- type Tree
- func (t Tree) Compile() Tree
- func (t Tree) Find(name string) Node
- func (t Tree) MatchMiddleware(path string) middleware.Collection
- func (t Tree) MatchRoute(path string) (Route, context.Params)
- func (t Tree) PrettyPrint() string
- func (t Tree) WithMiddleware(path string, m middleware.Collection, maxParamsSize uint8) Tree
- 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 MiddlewareAware ¶ added in v4.4.0
type MiddlewareAware interface {
// MatchMiddleware collects middleware from all nodes within tree matching given path
// middleware is merged in order nodes where created, collecting from top to bottom
MatchMiddleware(path string) middleware.Collection
// Middleware provides Node's middleware collection
Middleware() middleware.Collection
// AppendMiddleware appends middleware collection to Node
AppendMiddleware(m middleware.Collection)
// PrependMiddleware prepends middleware collection to Node
PrependMiddleware(m middleware.Collection)
}
MiddlewareAware represents middleware aware node
type Node ¶
type Node interface {
RouteAware
MiddlewareAware
// Name provides Node name
Name() string
// Tree provides next level Node Tree
Tree() Tree
// 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{}
}
Route is an handler aware route interface
type RouteAware ¶ added in v4.4.0
type RouteAware interface {
// MatchRoute matches given path to Route within Node and its Tree
MatchRoute(path string) (Route, context.Params)
// Route provides Node's Route if assigned
Route() Route
// WithRoute assigns Route to given Node
WithRoute(r Route)
// Name provides maximum number of parameters Route can have for given Node
MaxParamsSize() uint8
// SkipSubPath sets skipSubPath node property to true
// will skip children match search and return current node directly
// this value is used when matching subrouter
SkipSubPath()
}
RouteAware represents route aware Node
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) MatchMiddleware ¶ added in v4.4.0
func (t Tree) MatchMiddleware(path string) middleware.Collection
MatchMiddleware collects middleware from all nodes that match path
func (Tree) MatchRoute ¶ added in v4.4.0
MatchRoute path to first Node
func (Tree) PrettyPrint ¶ added in v4.2.0
PrettyPrint prints the tree text representation to console
func (Tree) WithMiddleware ¶ added in v4.4.0
func (t Tree) WithMiddleware(path string, m middleware.Collection, maxParamsSize uint8) Tree
WithMiddleware returns new Tree with Collection appended to given Node Collection is appended to Node under the give path, if Node does not exist it will panic