Documentation
¶
Overview ¶
Package router 包含路由相关工具
Index ¶
- func Register(kind string, creator RouterCreator)
- type BaseContext
- func (this *BaseContext) AllSegments() []string
- func (this *BaseContext) Match(count int)
- func (this *BaseContext) Matched() int
- func (this *BaseContext) Pure() bool
- func (this *BaseContext) Segments() []string
- func (this *BaseContext) SetValue(name string, value string)
- func (this *BaseContext) Unmatch(count int)
- func (this *BaseContext) Value(name string) (string, bool)
- type BaseRouter
- func (this *BaseRouter) AddChild(router Router)
- func (this *BaseRouter) AddChildren(routers []Router)
- func (this *BaseRouter) Child(name string) (Router, bool)
- func (this *BaseRouter) Children() []Router
- func (this *BaseRouter) Find(context RouterContext) (Router, bool)
- func (this *BaseRouter) MatchString() string
- func (this *BaseRouter) Normal() bool
- func (this *BaseRouter) RemoveChild(name string) (Router, bool)
- func (this *BaseRouter) SetParent(router Router) error
- type BaseRouterExecutor
- func (this *BaseRouterExecutor) Execute() (interface{}, error)
- func (this *BaseRouterExecutor) ExecutePostFilters(result interface{}) bool
- func (this *BaseRouterExecutor) ExecutePreFilters() bool
- func (this *BaseRouterExecutor) FilterExecute(f func() (interface{}, error)) (interface{}, error)
- func (this *BaseRouterExecutor) Router() Router
- func (this *BaseRouterExecutor) RouterContext() RouterContext
- func (this *BaseRouterExecutor) SetRouter(router Router)
- func (this *BaseRouterExecutor) SetRouterContext(context RouterContext)
- type Error
- type PostFilter
- type PreFilter
- type RegSegment
- type Router
- type RouterContext
- type RouterCreator
- type RouterExcutor
- type RouterExcutorGenerator
- type UnlimitedRouter
- func (this *UnlimitedRouter) AddChild(router Router)
- func (this *UnlimitedRouter) AddChildren(routers []Router)
- func (this *UnlimitedRouter) AddPostFilter(filter PostFilter) Router
- func (this *UnlimitedRouter) AddPreFilter(filter PreFilter) Router
- func (this *UnlimitedRouter) Child(name string) (Router, bool)
- func (this *UnlimitedRouter) Children() []Router
- func (this *UnlimitedRouter) ExecPostFilter(context RouterContext, result interface{}) bool
- func (this *UnlimitedRouter) ExecPreFilter(context RouterContext) bool
- func (this *UnlimitedRouter) Find(context RouterContext) (Router, bool)
- func (this *UnlimitedRouter) Match(context RouterContext) (RouterExcutor, bool)
- func (this *UnlimitedRouter) MatchString() string
- func (this *UnlimitedRouter) Name() string
- func (this *UnlimitedRouter) Normal() bool
- func (this *UnlimitedRouter) Parent() Router
- func (this *UnlimitedRouter) RemoveChild(name string) (Router, bool)
- func (this *UnlimitedRouter) RemovePostFilter(filter PostFilter) bool
- func (this *UnlimitedRouter) RemovePreFilter(filter PreFilter) bool
- func (this *UnlimitedRouter) RouterExcutor() (RouterExcutor, bool)
- func (this *UnlimitedRouter) SetParent(router Router) error
- func (this *UnlimitedRouter) SetRouterExcutorGenerator(reg RouterExcutorGenerator)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BaseContext ¶
基础路由上下文
func NewBaseContext ¶
func NewBaseContext(path string) *BaseContext
NewBaseContext 使用path创建基础路由上下文,path可以用\或/分割
func (*BaseContext) AllSegments ¶
func (this *BaseContext) AllSegments() []string
AllSegments 返回所有路由段
func (*BaseContext) Matched ¶
func (this *BaseContext) Matched() int
Matched 返回当前已匹配的路由段数量(即经过的路由数量)
func (*BaseContext) SetValue ¶
func (this *BaseContext) SetValue(name string, value string)
SetValue 设置路由值
type BaseRouter ¶
type BaseRouter struct { UnlimitedRouter // contains filtered or unexported fields }
基础路由
func (*BaseRouter) AddChildren ¶
func (this *BaseRouter) AddChildren(routers []Router)
AddChildren 批量添加子路由
func (*BaseRouter) Child ¶
func (this *BaseRouter) Child(name string) (Router, bool)
Child 返回指定名称的子路由
func (*BaseRouter) Find ¶
func (this *BaseRouter) Find(context RouterContext) (Router, bool)
Find 查找路由,该路由不一定能够生成RouterExcutor
func (*BaseRouter) MatchString ¶
func (this *BaseRouter) MatchString() string
MatchString 返回当前路由用于进行匹配的字符串
func (*BaseRouter) Normal ¶
func (this *BaseRouter) Normal() bool
Normal 返回当前路由是否为通常路由,通常路由可以使用MatchString()返回的字符串进行相等匹配
func (*BaseRouter) RemoveChild ¶
func (this *BaseRouter) RemoveChild(name string) (Router, bool)
RemoveChild 移除指定名称的路由,并返回该路由
func (*BaseRouter) SetParent ¶
func (this *BaseRouter) SetParent(router Router) error
SetParent 设置当前路由父路由,当前路由必须是父路由的子路由
type BaseRouterExecutor ¶
type BaseRouterExecutor struct { End Router Context RouterContext }
基础路由执行器
func (*BaseRouterExecutor) Execute ¶
func (this *BaseRouterExecutor) Execute() (interface{}, error)
Excute 执行
func (*BaseRouterExecutor) ExecutePostFilters ¶
func (this *BaseRouterExecutor) ExecutePostFilters(result interface{}) bool
ExecutePostFilters 执行全部后置过滤器,返回结果决定了处理方法的结果是否被Execute()返回
func (*BaseRouterExecutor) ExecutePreFilters ¶
func (this *BaseRouterExecutor) ExecutePreFilters() bool
ExecutePreFilters 执行全部前置过滤器,返回结果决定了处理方法是否会被执行
func (*BaseRouterExecutor) FilterExecute ¶
func (this *BaseRouterExecutor) FilterExecute(f func() (interface{}, error)) (interface{}, error)
FilterExecute 执行f方法并使用过滤器,未通过过滤器时返回相应错误
func (*BaseRouterExecutor) Router ¶
func (this *BaseRouterExecutor) Router() Router
Router 返回生成RouterExcutor的路由
func (*BaseRouterExecutor) RouterContext ¶
func (this *BaseRouterExecutor) RouterContext() RouterContext
RouterContext 返回路由上下文
func (*BaseRouterExecutor) SetRouter ¶
func (this *BaseRouterExecutor) SetRouter(router Router)
SetRouter 设置生成RouterExcutor的路由
func (*BaseRouterExecutor) SetRouterContext ¶
func (this *BaseRouterExecutor) SetRouterContext(context RouterContext)
SetRouterContext 设置路由上下文
type Error ¶
type Error string
错误信息
const ( ErrorInvalidParentRouter Error = "ErrorInvalidParentRouter(R10010):指定路由不是当前路由的父路由,设置父路由失败" ErrorNamedRouterNoChecker Error = "ErrorNamedRouterNoChecker(R10020):名称路由不能设置检查器" ErrorRegexpParseError Error = "ErrorRegexpParseError(R10030),无效的正则表达式(%s)" ErrorRegexpNoneError Error = "ErrorRegexpNoneError(R10031),字符串(%s)不包含正则表达式" ErrorRegexpFormatError Error = "ErrorRegexpFormatError(R10032),字符串(%s)格式错误" ErrorRegexpNotMatchError Error = "ErrorRegexpNotMatchError(R10033),正则表达式匹配失败" ErrorExecutorDoNothing Error = "ErrorExecutorDoNothing(R10050),空执行器错误,该执行器没有执行任何内容" ErrorPreFilterNotPass Error = "ErrorPreFilterNotPass(R10051),前置过滤未通过" ErrorPostFilterNotPass Error = "ErrorPostFilterNotPass(R10052),后置过滤未通过" ErrorInvalidKind Error = "ErrorInvalidKind(R10060),无效的路由类型(%s)" ErrorInvalidRouterCreator Error = "ErrorInvalidRouterCreator(R10070),无效的路由创建器" ErrorInvalidMatchParam Error = "ErrorInvalidMatchParam(R10080),无效的match参数(%s),期望参数类型为%s" )
错误码
type PostFilter ¶
type PostFilter interface { // Filter 过滤该请求 // return:返回true表示继续处理,否则终止路由过程,后续的过滤器也不会执行 Filter(context RouterContext, result interface{}) bool }
后置过滤器
type PreFilter ¶
type PreFilter interface { // Filter 过滤该请求 // return:返回true表示继续处理,否则终止路由过程,后续的过滤器也不会执行 Filter(context RouterContext) bool }
前置过滤器
type RegSegment ¶
type RegSegment struct { Exp string //正则表达式 Regexp *regexp.Regexp //编译后的正则表达式 Keys []string //可提取keys }
正则段
func ParseReg ¶
func ParseReg(exp string) (*RegSegment, error)
ParseReg 解析正则路由段字符串
return:如果可以解析出正则内容,则返回RegSegment,否则返回nil
func ParseRegs ¶
func ParseRegs(exps []string) ([]*RegSegment, error)
type Router ¶
type Router interface { // Name 返回当前路由名称 Name() string // MatchString 返回当前路由用于进行匹配的字符串 MatchString() string // Parent 返回当前父路由,每个Router只能有一个Parent Parent() Router // SetParent 设置当前路由父路由,当前路由必须是父路由的子路由 SetParent(router Router) error // Normal 返回当前路由是否为通常路由,通常路由可以使用MatchString()返回的字符串进行直接匹配 Normal() bool // AddChild 添加子路由,Name相同的路由自动合并 AddChild(router Router) // AddChildren 批量添加子路由,Name相同的路由自动合并 AddChildren(routers []Router) // Child 返回指定名称的子路由 Child(name string) (Router, bool) // Children 返回所有子路由 Children() []Router // RemoveChild 移除指定名称的路由,并返回该路由 RemoveChild(name string) (Router, bool) // AddPreFilter 添加前置过滤器 AddPreFilter(filter PreFilter) Router // RemovePreFilter 移除前置过滤器 RemovePreFilter(filter PreFilter) bool // ExecPreFilter 执行前置过滤器 ExecPreFilter(context RouterContext) bool // AddPostFilter 添加后置过滤器 AddPostFilter(filter PostFilter) Router // RemovePostFilter 移除后置过滤器 RemovePostFilter(filter PostFilter) bool // ExecPostFilter 执行后置过滤器 ExecPostFilter(context RouterContext, result interface{}) bool // SetRouterExcutorGenerator 设置路由执行器生成方法 SetRouterExcutorGenerator(RouterExcutorGenerator) // RouterExcutor 获得路由执行器 RouterExcutor() (RouterExcutor, bool) // Find 查找路由,该路由不一定能够生成RouterExcutor Find(context RouterContext) (Router, bool) // Match 匹配指定路由上下文,匹配成功则返回RouterExcutor Match(context RouterContext) (RouterExcutor, bool) }
func NewBaseRouter ¶
NewBaseRouter 创建基本路由,非正则路由不区分大小写,正则路由是否区分大小写由正则表达式确定
name:路由名称,如果match为nil,则使用name进行路由匹配 match:用于进行匹配的值,可以包含指定规则的正则字符串,格式可以为p{id=\d+}.html 解析为 ^p(\d+).html$
type RouterContext ¶
type RouterContext interface { // Segments 返回可匹配路由段(不包含已匹配的路由段) Segments() []string // AllSegments 返回所有路由段 AllSegments() []string // Match 匹配路由段数量 Match(count int) // Unmatch 失配路由段数量 Unmatch(count int) // Matched 返回当前已匹配的路由段数量 Matched() int // Value 返回路由值 Value(name string) (string, bool) // SetValue 设置路由值 SetValue(name string, value string) }
路由上下文
type RouterExcutor ¶
type RouterExcutor interface { // Router 返回生成RouterExcutor的路由 Router() Router // SetRouter 设置生成RouterExcutor的路由 SetRouter(router Router) // RouterContext 返回路由上下文 RouterContext() RouterContext // SetRouterContext 设置路由上下文 SetRouterContext(context RouterContext) // Execute 执行,并返回相应结果 Execute() (interface{}, error) }
路由执行器
type RouterExcutorGenerator ¶
type RouterExcutorGenerator func() RouterExcutor
路由执行器生成器,每次应当返回一个全新的RouterExcutor实例
type UnlimitedRouter ¶
type UnlimitedRouter struct {
// contains filtered or unexported fields
}
无限路由
func (*UnlimitedRouter) AddChild ¶
func (this *UnlimitedRouter) AddChild(router Router)
AddChild 无限路由不能添加子路由
func (*UnlimitedRouter) AddChildren ¶
func (this *UnlimitedRouter) AddChildren(routers []Router)
AddChildren 无限路由不能添加子路由
func (*UnlimitedRouter) AddPostFilter ¶
func (this *UnlimitedRouter) AddPostFilter(filter PostFilter) Router
AddPostFilter 添加后置过滤器
func (*UnlimitedRouter) AddPreFilter ¶
func (this *UnlimitedRouter) AddPreFilter(filter PreFilter) Router
AddPreFilter 添加前置过滤器
func (*UnlimitedRouter) Child ¶
func (this *UnlimitedRouter) Child(name string) (Router, bool)
Child 无限路由没有子路由
func (*UnlimitedRouter) Children ¶
func (this *UnlimitedRouter) Children() []Router
Children 无限路由没有子路由
func (*UnlimitedRouter) ExecPostFilter ¶
func (this *UnlimitedRouter) ExecPostFilter(context RouterContext, result interface{}) bool
ExecPostFilter 执行后置过滤器
func (*UnlimitedRouter) ExecPreFilter ¶
func (this *UnlimitedRouter) ExecPreFilter(context RouterContext) bool
ExecPreFilter 执行前置过滤器
func (*UnlimitedRouter) Find ¶
func (this *UnlimitedRouter) Find(context RouterContext) (Router, bool)
Find 查找路由,该路由不一定能够生成RouterExcutor
func (*UnlimitedRouter) Match ¶
func (this *UnlimitedRouter) Match(context RouterContext) (RouterExcutor, bool)
Match 匹配指定路由上下文,匹配成功则返回RouterExcutor
func (*UnlimitedRouter) MatchString ¶
func (this *UnlimitedRouter) MatchString() string
MatchString 返回当前路由用于进行匹配的字符串
func (*UnlimitedRouter) Normal ¶
func (this *UnlimitedRouter) Normal() bool
Normal 返回当前路由是否为通常路由,通常路由可以使用MatchString()返回的字符串进行直接匹配
func (*UnlimitedRouter) Parent ¶
func (this *UnlimitedRouter) Parent() Router
Parent 返回当前父路由,每个Router只能有一个Parent
func (*UnlimitedRouter) RemoveChild ¶
func (this *UnlimitedRouter) RemoveChild(name string) (Router, bool)
RemoveChild 无限路由没有子路由
func (*UnlimitedRouter) RemovePostFilter ¶
func (this *UnlimitedRouter) RemovePostFilter(filter PostFilter) bool
RemovePostFilter 移除后置过滤器
func (*UnlimitedRouter) RemovePreFilter ¶
func (this *UnlimitedRouter) RemovePreFilter(filter PreFilter) bool
RemovePreFilter 移除前置过滤器
func (*UnlimitedRouter) RouterExcutor ¶
func (this *UnlimitedRouter) RouterExcutor() (RouterExcutor, bool)
RouterExcutor 获得路由执行器
func (*UnlimitedRouter) SetParent ¶
func (this *UnlimitedRouter) SetParent(router Router) error
SetParent 设置当前路由父路由,当前路由必须是父路由的子路由
func (*UnlimitedRouter) SetRouterExcutorGenerator ¶
func (this *UnlimitedRouter) SetRouterExcutorGenerator(reg RouterExcutorGenerator)
SetRouterExcutor 设置路由执行器生成方法