Documentation ¶
Overview ¶
Package group 提供了按条件进行分组路由的功能
Index ¶
- func Any(r *http.Request) (*http.Request, bool)
- type Group
- func (g *Group) Add(matcher Matcher, r *mux.Router)
- func (g *Group) Middlewares() *mux.Middlewares
- func (g *Group) New(name string, matcher Matcher, o ...mux.Option) *mux.Router
- func (g *Group) Remove(name string)
- func (g *Group) Router(name string) *mux.Router
- func (g *Group) Routers() []*mux.Router
- func (g *Group) Routes() map[string]map[string][]string
- func (g *Group) ServeHTTP(w http.ResponseWriter, r *http.Request)
- type HeaderVersion
- type Hosts
- type Matcher
- type MatcherFunc
- type PathVersion
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group 一组路由
当路由关联的 Matcher 返回 true 时,就会进入该路由。 如果多条路由的 Matcher 都返回 true,则第一条路由获得权限, 即使该路由最终返回 404,也不会再在其它路由里查找相应的路由。 所以在有多条子路由的情况下,第一条子路由不应该永远返回 true, 否则其它子路由永远无法到达。
func New ¶
func New(o ...mux.Option) *Group
New 声明一个新的 Groups
o 用于设置由 New 添加的路由,有关 NotFound 与 Recovery 的设置同时会作用于 Group。
type HeaderVersion ¶
type HeaderVersion struct { // 将版本号作为参数保存到上下文中是的名称 // // 如果不需要,可以设置为空值。 Key string // 支持的版本号列表 // // 比如 accept=application/json;version=1.0,version= 之后的内容。 Versions []string // 错误日志输出通道 // // 如果为空,则不输出任何内容。 ErrLog *log.Logger }
HeaderVersion 匹配报头的版本号
匹配报头 Accept 中的报头信息。
type Hosts ¶
type Hosts struct {
// contains filtered or unexported fields
}
Hosts 限定域名的匹配工具
func (*Hosts) Add ¶
Add 添加新的域名
域名的格式和路由的语法格式是一样的,比如:
api.example.com {sub:[a-z]+}.example.com
如果存在命名参数,也可以通过 syntax.GetParams 获取。 当语法错误时,会触发 panic,可通过 CheckSyntax 检测语法的正确性。
type Matcher ¶
type Matcher interface { // Match 验证请求是否符合当前对象的要求 // // req 为 r 的副本,除非对 r 作了修改,否则可以直接返回 r。 // ok 表示是否匹配成功。 Match(r *http.Request) (req *http.Request, ok bool) }
Matcher 验证一个请求是否符合要求
Matcher 用于路由项的前置判断,用于对路由项进行归类, 符合同一个 Matcher 的路由项,再各自进行路由。比如按域名进行分组路由。
type MatcherFunc ¶
MatcherFunc 用于将 Match(*http.Request) (*http.Request, bool) 转换成 Matcher 接口
type PathVersion ¶
type PathVersion struct {
// contains filtered or unexported fields
}
PathVersion 匹配路径中的版本号
会修改 http.Request.URL.Path 的值,去掉匹配的版本号路径部分,比如:
/v1/path.html
如果匹配 v1 版本,会修改为:
/path.html
func NewPathVersion ¶
func NewPathVersion(param string, version ...string) *PathVersion
NewPathVersion 声明 PathVersion 实例
param 将版本号作为参数保存到上下文中是的名称,如果不需要,可以设置为空值。