mux

package
v4.5.1 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2020 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).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

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

func NewNode

func NewNode(pathPart string, maxParamsSize uint8) Node

NewNode provides new mux Node

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 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 finds Node inside a tree by name

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

func (t Tree) MatchRoute(path string) (Route, context.Params)

MatchRoute path to first Node

func (Tree) PrettyPrint added in v4.2.0

func (t Tree) PrettyPrint() string

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

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, if 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