Documentation
¶
Overview ¶
Package tree 提供了以树形结构保存路由项的相关操作
Index ¶
- Variables
- func ApplyMiddleware[T any](h T, method, pattern, router string, f ...types.Middleware[T]) T
- func BuildTestMiddleware(a *assert.Assertion, text string) types.Middleware[http.Handler]
- func BuildTestNodeHandlerFunc(status int) types.BuildNodeHandler[http.Handler]
- type Tree
- func (tree *Tree[T]) Add(pattern string, h T, ms []types.Middleware[T], methods ...string) error
- func (tree *Tree[T]) ApplyMiddleware(ms ...types.Middleware[T])
- func (tree *Tree[T]) Clean(prefix string)
- func (tree *Tree[T]) Find(pattern string) *node[T]
- func (tree *Tree[T]) Handler(ctx *types.Context, method string) (types.Node, T, bool)
- func (tree *Tree[T]) Name() string
- func (tree *Tree[T]) Print(w io.Writer)
- func (tree *Tree[T]) Remove(pattern string, methods ...string)
- func (tree *Tree[T]) Routes() map[string][]string
- func (tree *Tree[T]) URL(buf *errwrap.StringBuilder, pattern string, ps map[string]string) error
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // Methods 支持的请求方法 Methods = []string{ http.MethodGet, http.MethodPost, http.MethodDelete, http.MethodPut, http.MethodPatch, http.MethodConnect, http.MethodTrace, http.MethodHead, http.MethodOptions, } AnyMethods = Methods[:len(Methods)-3] // 添加请求方法时,所采用的默认值。 )
Functions ¶
func ApplyMiddleware ¶
func ApplyMiddleware[T any](h T, method, pattern, router string, f ...types.Middleware[T]) T
func BuildTestMiddleware ¶
func BuildTestMiddleware(a *assert.Assertion, text string) types.Middleware[http.Handler]
func BuildTestNodeHandlerFunc ¶
func BuildTestNodeHandlerFunc(status int) types.BuildNodeHandler[http.Handler]
Types ¶
type Tree ¶
type Tree[T any] struct { // contains filtered or unexported fields }
Tree 以树节点的形式保存的路由
多段路由项,会提取其中的相同的内容组成树状结构的节点。比如以下路由项:
/posts/{id}/author /posts/{id}/author/emails /posts/{id}/author/profile /posts/1/author
会被转换成以下结构
/posts | +---- 1/author | +---- {id}/author/ | +---- profile | +---- emails
func New ¶
func New[T any]( name string, lock bool, i *syntax.Interceptors, notFound T, trace any, methodNotAllowedBuilder, optionsBuilder types.BuildNodeHandler[T], ) *Tree[T]
func NewTestTree ¶
func NewTestTree(a *assert.Assertion, lock bool, trace http.Handler, i *syntax.Interceptors) *Tree[http.Handler]
NewTestTree 返回以 http.Handler 作为参数实例化的 Tree
func (*Tree[T]) ApplyMiddleware ¶
func (tree *Tree[T]) ApplyMiddleware(ms ...types.Middleware[T])
ApplyMiddleware 为已有的路由项添加中间件
func (*Tree[T]) Handler ¶
Handler 查找与参数匹配的处理对象
如果未找到,也会返回相应在的处理对象,比如 tree.notFound 或是相应的 methodNotAllowed 方法。
Click to show internal directories.
Click to hide internal directories.