Documentation ¶
Overview ¶
Package mux 是一个提供了路由匹配功能的中间件
m := mux.New(false, false, false, nil, nil). Get("/users/1", h). Post("/login", h). Get("/posts/{id:\\d+}", h). // 正则路由 Options("/users/1", "GET") // 手动指定 OPTIONS 请求的返回内容。 // 统一前缀路径的路由 p := m.Prefix("/api") p.Get("/logout", h) // 相当于 m.Get("/api/logout", h) p.Post("/login", h) // 相当于 m.Get("/api/login", h) // 对同一资源的不同操作 res := p.Resource("/users/{id\\d+}") res.Get(h) // 相当于 m.Get("/api/users/{id}", h) res.Post(h) // 相当于 m.Post("/api/users/{id}", h) res.URL(map[string]string{"id": "5"}) // 生成 /users/5 http.ListenAndServe(":8080", m)
正则表达式
路由中支持以正则表达式的方式进行匹配,表达式以大括号包含,内部以冒号分隔, 前半部分为变量的名称,后半部分为变量可匹配类型的正则表达式。比如:
/posts/{id:\\d+} // 将被转换成 /posts/(?P<id>\\d+) /posts/{:\\d+} // 将被转换成 /posts/\\d+
命名参数
若路由字符串中,所有的正则表达式冒号之后的内容是特定的内容,或是无内容, 则会被转换成命名参数,因为有专门的验证方法,性能会比较正则稍微好上一些。
/posts/{id}.html // 匹配 /posts/1.html /posts-{id}-{page}.html // 匹配 /posts-1-10.html /posts/{id:digit}.html // 匹配 /posts/1.html
目前支持以下作为命名参数的类型约束:
digit 限定为数字字符,相当于正则的 [0-9]; word 相当于正则的 [a-zA-Z0-9]; any 表示匹配任意非空内容; 为空表示表示任意内容,包括空;
如果需要自定义这些约束符,可以参考 interceptor 包的文档。
在路由字符串中若是以命名参数结尾的,则表示可以匹配之后的任意字符。
/blog/assets/{path} // 可以匹配 /blog/assets/2020/11/11/file.ext 等格式 /blog/{tags:\\w+}/{path} /blog/assets{path}
路径匹配规则
可能会出现多条记录与同一请求都匹配的情况,这种情况下, 系统会找到一条认为最匹配的路由来处理,判断规则如下:
- 普通路由优先于正则路由;
- 正则路由优先于命名路由;
比如:
/posts/{id}.html // 1 /posts/{id:\\d+}.html // 2 /posts/1.html // 3 /posts/1.html // 匹配 3 /posts/11.html // 匹配 2 /posts/index.html // 匹配 1
路由参数
通过正则表达式匹配的路由,其中带命名的参数可通过 Params() 获取:
params := Params(r) id, err := params.Int("id") // 或是 id := params.MustInt("id", 0) // 0 表示在无法获取 id 参数的默认值
OPTIONS ¶
默认情况下,用户无须显示地实现它,系统会自动实现。 当然用户也可以使用 *.Options() 函数指定特定的数据; 或是直接使用 *.Handle() 指定一个自定义的实现方式。
如果不需要的话,也可以在 New() 中将 disableOptions 设置为 true。 显示设定 OPTIONS,不受 disableOptions 的影响。
m := mux.New(...) m.Get("/posts/{id}", nil) // 默认情况下, OPTIONS 的报头为 GET, OPTIONS m.Options("/posts/{id}", "*") // 强制改成 * m.Delete("/posts/{id}", nil) // OPTIONS 依然为 * m.Remove("/posts/{id}", http.MethodOptions) // 在当前路由上禁用 OPTIONS m.Handle("/posts/{id}", h, http.MethodOptions) // 显示指定一个处理函数 h
HEAD
默认情况下,用户无须显示地实现 HEAD 请求, 系统会为每一个 GET 请求自动实现一个对应的 HEAD 请求, 当然也与 OPTIONS 一样,你也可以自通过 mux.Handle() 自己实现 HEAD 请求。
Index ¶
- Variables
- func IsWell(pattern string) error
- func Methods() []string
- func Params(r *http.Request) params.Params
- type Hosts
- type Matcher
- type MatcherFunc
- type Mux
- func (mux *Mux) All(ignoreHead, ignoreOptions bool) []*Router
- func (mux *Mux) Any(pattern string, h http.Handler) *Mux
- func (mux *Mux) AnyFunc(pattern string, fun http.HandlerFunc) *Mux
- func (mux *Mux) Clean() *Mux
- func (mux *Mux) Delete(pattern string, h http.Handler) *Mux
- func (mux *Mux) DeleteFunc(pattern string, fun http.HandlerFunc) *Mux
- func (mux *Mux) Get(pattern string, h http.Handler) *Mux
- func (mux *Mux) GetFunc(pattern string, fun http.HandlerFunc) *Mux
- func (mux *Mux) Handle(pattern string, h http.Handler, methods ...string) error
- func (mux *Mux) HandleFunc(pattern string, fun http.HandlerFunc, methods ...string) error
- func (mux *Mux) Name(name, pattern string) error
- func (mux *Mux) NewMux(name string, matcher Matcher) (*Mux, bool)
- func (mux *Mux) Options(pattern string, allow string) *Mux
- func (mux *Mux) Patch(pattern string, h http.Handler) *Mux
- func (mux *Mux) PatchFunc(pattern string, fun http.HandlerFunc) *Mux
- func (mux *Mux) Post(pattern string, h http.Handler) *Mux
- func (mux *Mux) PostFunc(pattern string, fun http.HandlerFunc) *Mux
- func (mux *Mux) Prefix(prefix string) *Prefix
- func (mux *Mux) Put(pattern string, h http.Handler) *Mux
- func (mux *Mux) PutFunc(pattern string, fun http.HandlerFunc) *Mux
- func (mux *Mux) Remove(pattern string, methods ...string) *Mux
- func (mux *Mux) Resource(pattern string) *Resource
- func (mux *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (mux *Mux) SetAllow(pattern string, allow string) error
- func (mux *Mux) URL(name string, params map[string]string) (string, error)
- type Prefix
- func (p *Prefix) Any(pattern string, h http.Handler) *Prefix
- func (p *Prefix) AnyFunc(pattern string, fun http.HandlerFunc) *Prefix
- func (p *Prefix) Clean() *Prefix
- func (p *Prefix) Delete(pattern string, h http.Handler) *Prefix
- func (p *Prefix) DeleteFunc(pattern string, fun http.HandlerFunc) *Prefix
- func (p *Prefix) Get(pattern string, h http.Handler) *Prefix
- func (p *Prefix) GetFunc(pattern string, fun http.HandlerFunc) *Prefix
- func (p *Prefix) Handle(pattern string, h http.Handler, methods ...string) error
- func (p *Prefix) HandleFunc(pattern string, fun http.HandlerFunc, methods ...string) error
- func (p *Prefix) Mux() *Mux
- func (p *Prefix) Name(name, pattern string) error
- func (p *Prefix) Options(pattern string, allow string) *Prefix
- func (p *Prefix) Patch(pattern string, h http.Handler) *Prefix
- func (p *Prefix) PatchFunc(pattern string, fun http.HandlerFunc) *Prefix
- func (p *Prefix) Post(pattern string, h http.Handler) *Prefix
- func (p *Prefix) PostFunc(pattern string, fun http.HandlerFunc) *Prefix
- func (p *Prefix) Prefix(prefix string) *Prefix
- func (p *Prefix) Put(pattern string, h http.Handler) *Prefix
- func (p *Prefix) PutFunc(pattern string, fun http.HandlerFunc) *Prefix
- func (p *Prefix) Remove(pattern string, methods ...string) *Prefix
- func (p *Prefix) Resource(pattern string) *Resource
- func (p *Prefix) SetAllow(pattern string, allow string) error
- func (p *Prefix) URL(name string, params map[string]string) (string, error)
- type Resource
- func (r *Resource) Any(h http.Handler) *Resource
- func (r *Resource) AnyFunc(fun http.HandlerFunc) *Resource
- func (r *Resource) Clean() *Resource
- func (r *Resource) Delete(h http.Handler) *Resource
- func (r *Resource) DeleteFunc(fun http.HandlerFunc) *Resource
- func (r *Resource) Get(h http.Handler) *Resource
- func (r *Resource) GetFunc(fun http.HandlerFunc) *Resource
- func (r *Resource) Handle(h http.Handler, methods ...string) error
- func (r *Resource) HandleFunc(fun http.HandlerFunc, methods ...string) error
- func (r *Resource) Mux() *Mux
- func (r *Resource) Name(name string) error
- func (r *Resource) Options(allow string) *Resource
- func (r *Resource) Patch(h http.Handler) *Resource
- func (r *Resource) PatchFunc(fun http.HandlerFunc) *Resource
- func (r *Resource) Post(h http.Handler) *Resource
- func (r *Resource) PostFunc(fun http.HandlerFunc) *Resource
- func (r *Resource) Put(h http.Handler) *Resource
- func (r *Resource) PutFunc(fun http.HandlerFunc) *Resource
- func (r *Resource) Remove(methods ...string) *Resource
- func (r *Resource) SetAllow(allow string) error
- func (r *Resource) URL(params map[string]string) (string, error)
- type Router
Constants ¶
This section is empty.
Variables ¶
var ErrNameExists = errors.New("存在相同名称的路由项")
ErrNameExists 存在相同名称
当为一个路由项命名时,若存在相同名称的,则返回此错误信息。
Functions ¶
Types ¶
type Hosts ¶
type Hosts struct {
// contains filtered or unexported fields
}
Hosts 限定域名的匹配工具
type MatcherFunc ¶ added in v3.2.0
MatcherFunc 用于转一个 Match(http.Request) bool 转换成 Matcher 接口
type Mux ¶
type Mux struct {
// contains filtered or unexported fields
}
Mux 提供了强大的路由匹配功能
可以对路径按正则或是请求方法进行匹配。用法如下:
m := mux.New() m.Get("/abc/h1", h1). Post("/abc/h2", h2). Handle("/api/{version:\\d+}",h3, http.MethodGet, http.MethodPost) // 只匹配 GET 和 POST http.ListenAndServe(m)
func New ¶
func New(disableOptions, disableHead, skipCleanPath bool, notFound, methodNotAllowed http.HandlerFunc) *Mux
New 声明一个新的 Mux
disableOptions 是否禁用自动生成 OPTIONS 功能; disableHead 是否禁用根据 Get 请求自动生成 HEAD 请求; skipCleanPath 是否不对访问路径作处理,比如 "//api" ==> "/api"; notFound 404 页面的处理方式,为 nil 时会调用默认的方式进行处理; methodNotAllowed 405 页面的处理方式,为 nil 时会调用默认的方式进行处理;
func (*Mux) AnyFunc ¶
func (mux *Mux) AnyFunc(pattern string, fun http.HandlerFunc) *Mux
AnyFunc 相当于 Mux.HandleFunc(pattern, func) 的简易写法
func (*Mux) DeleteFunc ¶
func (mux *Mux) DeleteFunc(pattern string, fun http.HandlerFunc) *Mux
DeleteFunc 相当于 Mux.HandleFunc(pattern, func, http.MethodDelete) 的简易写法
func (*Mux) GetFunc ¶
func (mux *Mux) GetFunc(pattern string, fun http.HandlerFunc) *Mux
GetFunc 相当于 Mux.HandleFunc(pattern, func, http.MethodGet) 的简易写法
func (*Mux) Handle ¶
Handle 添加一条路由数据
pattern 为路由匹配模式,可以是正则匹配也可以是字符串匹配, 若语法不正确,则直接 panic,可以通过 IsWell 检测语法的有效性,其它接口也相同; methods 该路由项对应的请求方法,如果未指定值,则表示所有支持的请求方法, 但不包含 OPTIONS 和 HEAD。
func (*Mux) HandleFunc ¶
HandleFunc 功能同 Mux.Handle(),但是将第二个参数从 http.Handler 换成了 http.HandlerFunc
func (*Mux) NewMux ¶
NewMux 添加子路由组
该路由只有符合 matcher 的要求才会进入,其它与 Mux 功能相同。
name 表示该路由组的名称,需要唯一,否则返回 false;
func (*Mux) Options ¶
Options 将 OPTIONS 请求方法的报头 allow 值固定为指定的值
若无特殊需求,不用调用此方法,系统会自动计算符合当前路由的请求方法列表。 如果想实现对处理方法的自定义,可以显示地调用 Handle 方法:
Mux.Handle("/api/1", handle, http.MethodOptions)
Options 与 SetAllow 功能上完全相同,只是对错误处理上有所有区别。 Options 在出错时 panic,而 SetAllow 会返回错误信息。
func (*Mux) PatchFunc ¶
func (mux *Mux) PatchFunc(pattern string, fun http.HandlerFunc) *Mux
PatchFunc 相当于 Mux.HandleFunc(pattern, func, http.MethodPatch) 的简易写法
func (*Mux) PostFunc ¶
func (mux *Mux) PostFunc(pattern string, fun http.HandlerFunc) *Mux
PostFunc 相当于 Mux.HandleFunc(pattern, func, "POST") 的简易写法
func (*Mux) PutFunc ¶
func (mux *Mux) PutFunc(pattern string, fun http.HandlerFunc) *Mux
PutFunc 相当于 Mux.HandleFunc(pattern, func, http.MethodPut) 的简易写法
type Prefix ¶
type Prefix struct {
// contains filtered or unexported fields
}
Prefix 可以将具有统一前缀的路由项集中在一起操作
example:
p := srv.Prefix("/api") p.Get("/users") // 相当于 srv.Get("/api/users") p.Get("/user/1") // 相当于 srv.Get("/api/user/1")
func (*Prefix) AnyFunc ¶
func (p *Prefix) AnyFunc(pattern string, fun http.HandlerFunc) *Prefix
AnyFunc 相当于 Mux.AnyFunc(prefix+pattern, func) 的简易写法
func (*Prefix) Clean ¶
Clean 清除所有以 Prefix.prefix 开头的路由项
当指定多个相同的 Prefix 时,调用其中的一个 Clean 也将会清除其它的:
p1 := mux.Prefix("prefix") p2 := mux.Prefix("prefix") p2.Clean() 将同时清除 p1 的内容,因为有相同的前缀。
func (*Prefix) DeleteFunc ¶
func (p *Prefix) DeleteFunc(pattern string, fun http.HandlerFunc) *Prefix
DeleteFunc 相当于 Mux.DeleteFunc(prefix+pattern, func) 的简易写法
func (*Prefix) GetFunc ¶
func (p *Prefix) GetFunc(pattern string, fun http.HandlerFunc) *Prefix
GetFunc 相当于 Mux.GetFunc(prefix+pattern, func) 的简易写法
func (*Prefix) HandleFunc ¶
HandleFunc 功能同 Mux.HandleFunc(prefix+pattern, fun, ...)
func (*Prefix) PatchFunc ¶
func (p *Prefix) PatchFunc(pattern string, fun http.HandlerFunc) *Prefix
PatchFunc 相当于 Mux.PatchFunc(prefix+pattern, func) 的简易写法
func (*Prefix) PostFunc ¶
func (p *Prefix) PostFunc(pattern string, fun http.HandlerFunc) *Prefix
PostFunc 相当 于Mux.PostFunc(prefix+pattern, func) 的简易写法
func (*Prefix) Prefix ¶
Prefix 在现有 Prefix 的基础上声明一个新的 Prefix 实例
example:
p := mux.Prefix("/api") v := p.Prefix("/v2") v.Get("/users") // 相当于 g.Get("/api/v2/users") v.Get("/users/1") // 相当于 g.Get("/api/v2/users/1") v.Get("example.com/users/1") // 相当于 g.Get("/api/v2/example.com/users/1")
func (*Prefix) PutFunc ¶
func (p *Prefix) PutFunc(pattern string, fun http.HandlerFunc) *Prefix
PutFunc 相当于 Mux.PutFunc(prefix+pattern, func) 的简易写法
type Resource ¶
type Resource struct {
// contains filtered or unexported fields
}
Resource 以资源地址为对象的路由配置
r, _ := srv.Resource("/api/users/{id}") r.Get(h) // 相当于 srv.Get("/api/users/{id}") r.Post(h) // 相当于 srv.Post("/api/users/{id}") url := r.URL(map[string]string{"id":5}) // 获得 /api/users/5
func (*Resource) AnyFunc ¶
func (r *Resource) AnyFunc(fun http.HandlerFunc) *Resource
AnyFunc 相当于 Mux.AnyFunc(pattern, func) 的简易写法
func (*Resource) DeleteFunc ¶
func (r *Resource) DeleteFunc(fun http.HandlerFunc) *Resource
DeleteFunc 相当于 Mux.DeleteFunc(pattern, func) 的简易写法
func (*Resource) GetFunc ¶
func (r *Resource) GetFunc(fun http.HandlerFunc) *Resource
GetFunc 相当于 Mux.GetFunc(pattern, func) 的简易写法
func (*Resource) HandleFunc ¶
func (r *Resource) HandleFunc(fun http.HandlerFunc, methods ...string) error
HandleFunc 功能同 Mux.HandleFunc(pattern, fun, ...)
func (*Resource) PatchFunc ¶
func (r *Resource) PatchFunc(fun http.HandlerFunc) *Resource
PatchFunc 相当于 Mux.PatchFunc(pattern, func) 的简易写法
func (*Resource) PostFunc ¶
func (r *Resource) PostFunc(fun http.HandlerFunc) *Resource
PostFunc 相当于 Mux.PostFunc(pattern, func) 的简易写法
func (*Resource) PutFunc ¶
func (r *Resource) PutFunc(fun http.HandlerFunc) *Resource
PutFunc 相当于 Mux.PutFunc(pattern, func) 的简易写法
func (*Resource) URL ¶
URL 根据参数构建一条 URL
params 匹配路由参数中的同名参数,或是不存在路由参数,比如普通的字符串路由项, 该参数不启作用;
res, := m.Resource("/posts/{id}") res.URL(map[string]string{"id": "1"}, "") // /posts/1 res, := m.Resource("/posts/{id}/{path}") res.URL(map[string]string{"id": "1","path":"author/profile"}) // /posts/1/author/profile
Directories ¶
Path | Synopsis |
---|---|
Package interceptor 针对带参数类型路由的拦截处理 在解析诸如 /authors/{id:\\d+} 带参数的路由项时, 用户可以通过拦截并自定义对参数部分 {id:\\d+} 的解析, 从而不需要走正则表达式的那一套解析流程,可以在一定程度上增强性能。
|
Package interceptor 针对带参数类型路由的拦截处理 在解析诸如 /authors/{id:\\d+} 带参数的路由项时, 用户可以通过拦截并自定义对参数部分 {id:\\d+} 的解析, 从而不需要走正则表达式的那一套解析流程,可以在一定程度上增强性能。 |
internal
|
|
handlers
Package handlers 用于处理节点下与处理函数相关的逻辑
|
Package handlers 用于处理节点下与处理函数相关的逻辑 |
syntax
Package syntax 负责处理路由语法
|
Package syntax 负责处理路由语法 |
tree
Package tree 提供了以树形结构保存路由项的相关操作。
|
Package tree 提供了以树形结构保存路由项的相关操作。 |
Package params 获取和转换路由中的参数信息
|
Package params 获取和转换路由中的参数信息 |