mux

package
v4.2.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2019 License: MIT Imports: 8 Imported by: 0

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

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 provides Node name
	Name() string
	// Tree provides next level Node Tree
	Tree() Tree
	// Route provides Node's Route if assigned
	Route() Route

	// Name provides maximum number of parameters Route can have for given Node
	MaxParamsSize() uint8

	// WithRoute assigns Route to given Node
	WithRoute(r Route)
	// WithChildren sets Node's Tree
	WithChildren(t Tree)

	// 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()
}

Node represents mux Node Can match path and provide routes

func NewNode

func NewNode(pathPart string, maxParamsSize uint8) Node

NewNode provides new mux Node

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 NewTree

func NewTree() Tree

NewTree provides new empty Tree

func (Tree) Compile added in v4.2.0

func (t Tree) Compile() Tree

Compile optimizes Tree nodes reducing static nodes depth when possible

func (Tree) Find added in v4.2.0

func (t Tree) Find(name string) Node

Find Node inside a tree by name

func (Tree) Match added in v4.2.0

func (t Tree) Match(path string) (Node, context.Params, string)

Match path to Node

func (Tree) PrettyPrint added in v4.2.0

func (t Tree) PrettyPrint() string

PrettyPrint prints the tree text representation to console

func (Tree) WithRoute added in v4.2.0

func (t Tree) WithRoute(path string, route Route, maxParamsSize uint8) Tree

WithRoute returns new Tree with Route set to Node Route is set to Node under the give path, ff Node does not exist it is created

func (Tree) WithSubrouter added in v4.2.0

func (t Tree) WithSubrouter(path string, route Route, maxParamsSize uint8) Tree

WithSubrouter returns new Tree with new Route set to Subrouter Node Route is set to Node under the give path, ff Node does not exist it is created

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL