Documentation ¶
Index ¶
- Constants
- Variables
- func SetRouterCheckFunc(name string, fn RouterCheckFunc)
- func SetRouterNewCheckFunc(name string, fn RouterNewCheckFunc)
- type Handler
- type Middleware
- type Params
- type ParamsArray
- type Router
- type RouterCheckFunc
- type RouterCore
- type RouterFull
- func (r *RouterFull) Match(method, path string, params Params) Handler
- func (r *RouterFull) RegisterHandler(method string, path string, handler Handler)
- func (r *RouterFull) RegisterMiddleware(method, path string, hs []Middleware)
- func (r *RouterFull) ServeHTTP(w http.ResponseWriter, req *http.Request)
- type RouterHost
- type RouterMethod
- type RouterMethodStd
- func (m *RouterMethodStd) AddHandler(method, path string, hs Handler) RouterMethod
- func (m *RouterMethodStd) AddMiddleware(method, path string, hs ...Middleware) RouterMethod
- func (m *RouterMethodStd) Any(path string, h Handler)
- func (m *RouterMethodStd) Delete(path string, h Handler)
- func (m *RouterMethodStd) Get(path string, h Handler)
- func (m *RouterMethodStd) Group(path string) RouterMethod
- func (m *RouterMethodStd) Head(path string, h Handler)
- func (m *RouterMethodStd) MethodNotAllowed(h Handler)
- func (m *RouterMethodStd) NotFound(h Handler)
- func (m *RouterMethodStd) Options(path string, h Handler)
- func (m *RouterMethodStd) Patch(path string, h Handler)
- func (m *RouterMethodStd) Post(path string, h Handler)
- func (m *RouterMethodStd) Put(path string, h Handler)
- type RouterNewCheckFunc
- type RouterRadix
- func (r *RouterRadix) Match(method, path string, params Params) Handler
- func (r *RouterRadix) RegisterHandler(method string, path string, handler Handler)
- func (r *RouterRadix) RegisterMiddleware(method, path string, hs []Middleware)
- func (r *RouterRadix) ServeHTTP(w http.ResponseWriter, req *http.Request)
Constants ¶
const ( MethodAny = "ANY" MethodGet = "GET" MethodPost = "POST" MethodPut = "PUT" MethodDelete = "DELETE" MethodHead = "HEAD" MethodPatch = "PATCH" MethodOptions = "OPTIONS" MethodConnect = "CONNECT" MethodTrace = "TRACE" )
默认http请求方法
Variables ¶
var ( // ParamRoute 是路由参数键值 ParamRoute = "route" // Page404 是404返回的body Page404 = []byte("404 page not found\n") // Page405 是405返回的body Page405 = []byte("405 method not allowed\n") // RouterAllMethod 是默认Any的全部方法 RouterAllMethod = []string{MethodGet, MethodPost, MethodPut, MethodDelete, MethodHead, MethodPatch, MethodOptions} )
Functions ¶
func SetRouterCheckFunc ¶
func SetRouterCheckFunc(name string, fn RouterCheckFunc)
SetRouterCheckFunc 保存一个RouterCheckFunc函数,用于参数校验使用。
func SetRouterNewCheckFunc ¶
func SetRouterNewCheckFunc(name string, fn RouterNewCheckFunc)
SetRouterNewCheckFunc 保存一个RouterNewCheckFunc函数,用于参数动态校验使用。
Types ¶
type Handler ¶
type Handler func(http.ResponseWriter, *http.Request, Params)
Handler 是Erouter处理一个请求的方法,在http.HandlerFunc基础上增加了Parmas。
func CombineHandler ¶
func CombineHandler(handler Handler, m []Middleware) Handler
CombineHandler 合并全部中间件处理函数。
func NewHandler ¶
func NewHandler(i interface{}) Handler
NewHandler 根据http.Handler和http.HandlerFunc返回erouter.Handler
type Middleware ¶
Middleware 定义请求处理中间件函数,通过传入处理然后返回一个处理,使用装饰器组装处理请求。
type ParamsArray ¶
ParamsArray 默认参数实现,使用数组保存键值对。
func (*ParamsArray) AddParam ¶
func (p *ParamsArray) AddParam(key string, val string)
AddParam 追加一个参数的值。
func (*ParamsArray) GetParam ¶
func (p *ParamsArray) GetParam(key string) string
GetParam 读取参数的值,如果不存在返回空字符串。
func (*ParamsArray) SetParam ¶
func (p *ParamsArray) SetParam(key string, val string)
SetParam 设置一个参数的值。
type Router ¶
type Router interface { RouterCore RouterMethod }
Router interface needs to implement two methods: the router method and the router core.
路由器接口,需要实现路由器方法、路由器核心两个接口。
func NewRouterFull ¶
func NewRouterFull() Router
NewRouterFull 创建一个Full路由器,基于基数数实现,使用Radix路由器扩展,新增参数校验功能。
func NewRouterHost ¶
func NewRouterHost() Router
NewRouterHost 创建一个Host路由器,默认子路由器为Radix,其他Host匹配需要将Router类型转换成*RouterHost,然后使用RegisterHost方法注册。
type RouterCheckFunc ¶
RouterCheckFunc Route data validation function to check if a string parameter is valid.
RouterCheckFunc路由数据校验函数,检查一个字符串参数是否有效。
func GetRouterCheckFunc ¶
func GetRouterCheckFunc(name string) RouterCheckFunc
GetRouterCheckFunc 获得一个RouterCheckFunc函数
type RouterCore ¶
type RouterCore interface { RegisterMiddleware(string, string, []Middleware) RegisterHandler(string, string, Handler) ServeHTTP(http.ResponseWriter, *http.Request) }
RouterCore interface, performing routing, middleware registration, and processing http requests.
路由器核心接口,执行路由、中间件的注册和处理http请求。
type RouterFull ¶
type RouterFull struct { RouterMethod // contains filtered or unexported fields }
RouterFull is implemented based on the radix tree to implement all router related features.
Based on the RouterRadix extension, RouterFull implements variable check matching and wildcard check matching.
RouterFull基于基数树实现,实现全部路由器相关特性。
RouterFull基于RouterRadix扩展,实现变量校验匹配、通配符校验匹配功能。
func (*RouterFull) Match ¶
func (r *RouterFull) Match(method, path string, params Params) Handler
Match a request, if the method does not allow direct return to node405, no match returns node404.
匹配一个请求,如果方法不不允许直接返回node405,未匹配返回node404。
func (*RouterFull) RegisterHandler ¶
func (r *RouterFull) RegisterHandler(method string, path string, handler Handler)
RegisterHandler Register a new method request path to the router
The router matches the handlers available to the current path from the middleware tree and adds them to the front of the handler.
RegisterHandler给路由器注册一个新的方法请求路径 ¶
路由器会从中间件树中匹配当前路径可使用的处理者,并添加到处理者前方。
func (*RouterFull) RegisterMiddleware ¶
func (r *RouterFull) RegisterMiddleware(method, path string, hs []Middleware)
RegisterMiddleware Register the middleware into the middleware tree and append the handler if it exists.
If the method is not empty, the path is empty and the modified path is '/'.
RegisterMiddleware注册中间件到中间件树中,如果存在则追加处理者。
如果方法非空,路径为空,修改路径为'/'。
func (*RouterFull) ServeHTTP ¶
func (r *RouterFull) ServeHTTP(w http.ResponseWriter, req *http.Request)
实现http.Handler接口,进行路由匹配并处理http请求。
type RouterHost ¶
type RouterHost struct { RouterMethod Default Router Hosts []string Routers []Router }
RouterHost 基于Host匹配进行路由。
func (*RouterHost) RegisterHandler ¶
func (r *RouterHost) RegisterHandler(method string, path string, handler Handler)
RegisterHandler 从路径参数中获得host参数,选择对应子路由器注册新路由。
func (*RouterHost) RegisterHost ¶
func (r *RouterHost) RegisterHost(host string, router Router)
RegisterHost 给Host路由器注册域名的子路由器。
如果host为空字符串,设置为默认子路由器。
func (*RouterHost) RegisterMiddleware ¶
func (r *RouterHost) RegisterMiddleware(method, path string, hs []Middleware)
RegisterMiddleware 从路径参数中获得host参数,选择对应子路由器注册中间件函数。
func (*RouterHost) ServeHTTP ¶
func (r *RouterHost) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP 获取请求的Host匹配对应子路由器处理http请求。
type RouterMethod ¶
type RouterMethod interface { Group(string) RouterMethod AddHandler(string, string, Handler) RouterMethod AddMiddleware(string, string, ...Middleware) RouterMethod NotFound(Handler) MethodNotAllowed(Handler) Any(string, Handler) Delete(string, Handler) Get(string, Handler) Head(string, Handler) Options(string, Handler) Patch(string, Handler) Post(string, Handler) Put(string, Handler) }
RouterMethod route is directly registered by default. Other methods can be directly registered using the RouterRegister interface.
路由默认直接注册的方法,其他方法可以使用RouterRegister接口直接注册。
type RouterMethodStd ¶
type RouterMethodStd struct { RouterCore // contains filtered or unexported fields }
RouterMethodStd 默认路由器方法添加一个实现
func (*RouterMethodStd) AddHandler ¶
func (m *RouterMethodStd) AddHandler(method, path string, hs Handler) RouterMethod
AddHandler 添加一个新路由。
方法和RegisterHandler方法的区别在于AddHandler方法不会继承Group的路径和参数信息,AddMiddleware相同。
func (*RouterMethodStd) AddMiddleware ¶
func (m *RouterMethodStd) AddMiddleware(method, path string, hs ...Middleware) RouterMethod
AddMiddleware 给路由器添加一个中间件函数。
func (*RouterMethodStd) Any ¶
func (m *RouterMethodStd) Any(path string, h Handler)
Any Router Register handler。
func (*RouterMethodStd) Delete ¶
func (m *RouterMethodStd) Delete(path string, h Handler)
Delete 添加一个DELETE方法请求处理。
func (*RouterMethodStd) Get ¶
func (m *RouterMethodStd) Get(path string, h Handler)
Get 添加一个GET方法请求处理。
func (*RouterMethodStd) Group ¶
func (m *RouterMethodStd) Group(path string) RouterMethod
Group 返回一个组路由方法。
func (*RouterMethodStd) Head ¶
func (m *RouterMethodStd) Head(path string, h Handler)
Head 添加一个HEAD方法请求处理。
func (*RouterMethodStd) MethodNotAllowed ¶
func (m *RouterMethodStd) MethodNotAllowed(h Handler)
MethodNotAllowed 设置405处理。
func (*RouterMethodStd) Options ¶
func (m *RouterMethodStd) Options(path string, h Handler)
Options 添加一个OPTIONS方法请求处理。
func (*RouterMethodStd) Patch ¶
func (m *RouterMethodStd) Patch(path string, h Handler)
Patch 添加一个PATCH方法请求处理。
func (*RouterMethodStd) Post ¶
func (m *RouterMethodStd) Post(path string, h Handler)
Post 添加一个POST方法请求处理。
func (*RouterMethodStd) Put ¶
func (m *RouterMethodStd) Put(path string, h Handler)
Put 添加一个PUT方法请求处理。
type RouterNewCheckFunc ¶
type RouterNewCheckFunc func(string) RouterCheckFunc
RouterNewCheckFunc Routing data validation function creation function
Construct a new validation function by specifying a string.
RouterNewCheckFunc路由数据校验函数的创建函数 ¶
通过指定字符串构造出一个新的校验函数。
func GetRouterNewCheckFunc ¶
func GetRouterNewCheckFunc(name string) RouterNewCheckFunc
GetRouterNewCheckFunc 获得一个RouterNewCheckFunc函数
type RouterRadix ¶
type RouterRadix struct { RouterMethod // contains filtered or unexported fields }
RouterRadix Basic function router based on radix tree implementation.
Features such as zero memory copy, strict route matching order, group routing, middleware function, default parameters, constant matching, variable matching, wildcard matching, variable check matching, wildcard check matching, and host routing. // 基于基数树实现的基本功能路由器。
RouterRadix基于基数树实现基本的路由器功能。
具有零内存复制、严格路由匹配顺序、组路由、中间件功能、默认参数、常量匹配、变量匹配、通配符匹配、变量校验匹配、通配符校验匹配、基于Host路由这些特点功能。
func (*RouterRadix) Match ¶
func (r *RouterRadix) Match(method, path string, params Params) Handler
Match a request, if the method does not allow direct return to node405, no match returns node404.
匹配一个请求,如果方法不不允许直接返回node405,未匹配返回node404。
func (*RouterRadix) RegisterHandler ¶
func (r *RouterRadix) RegisterHandler(method string, path string, handler Handler)
RegisterHandler method register a new method request path to the router
The router matches the handlers available to the current path from the middleware tree and adds them to the front of the handler.
RegisterHandler给路由器注册一个新的方法请求路径 ¶
路由器会从中间件树中匹配当前路径可使用的处理者,并添加到处理者前方。
func (*RouterRadix) RegisterMiddleware ¶
func (r *RouterRadix) RegisterMiddleware(method, path string, hs []Middleware)
RegisterMiddleware method register the middleware into the middleware tree and append the handler if it exists.
If the method is not empty, the path is empty and the modified path is '/'.
RegisterMiddleware注册中间件到中间件树中,如果存在则追加处理者。
如果方法非空,路径为空,修改路径为'/'。
func (*RouterRadix) ServeHTTP ¶
func (r *RouterRadix) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP 实现http.Handler接口,进行路由匹配并处理http请求。