Documentation
¶
Index ¶
- Variables
- func Vars(r *http.Request) map[string]string
- type BuildVarsFunc
- type MatcherFunc
- type MiddlewareFunc
- type Route
- func (r *Route) BuildOnly() *Route
- func (r *Route) BuildVarsFunc(f BuildVarsFunc) *Route
- func (r *Route) GetError() error
- func (r *Route) GetHandler() http.Handler
- func (r *Route) GetHostTemplate() (string, error)
- func (r *Route) GetMethods() ([]string, error)
- func (r *Route) GetName() string
- func (r *Route) GetPathRegexp() (string, error)
- func (r *Route) GetPathTemplate() (string, error)
- func (r *Route) GetQueriesRegexp() ([]string, error)
- func (r *Route) GetQueriesTemplates() ([]string, error)
- func (r *Route) Handler(handler http.Handler) *Route
- func (r *Route) HandlerFunc(f func(http.ResponseWriter, *http.Request)) *Route
- func (r *Route) Headers(pairs ...string) *Route
- func (r *Route) HeadersRegexp(pairs ...string) *Route
- func (r *Route) Host(tpl string) *Route
- func (r *Route) Match(req *http.Request, match *RouteMatch) bool
- func (r *Route) MatcherFunc(f MatcherFunc) *Route
- func (r *Route) Methods(methods ...string) *Route
- func (r *Route) Name(name string) *Route
- func (r *Route) Path(tpl string) *Route
- func (r *Route) PathPrefix(tpl string) *Route
- func (r *Route) Queries(pairs ...string) *Route
- func (r *Route) Schemes(schemes ...string) *Route
- func (r *Route) SkipClean() bool
- func (r *Route) Subrouter() *Router
- func (r *Route) URL(pairs ...string) (*url.URL, error)
- func (r *Route) URLHost(pairs ...string) (*url.URL, error)
- func (r *Route) URLPath(pairs ...string) (*url.URL, error)
- type RouteMatch
- type Router
- func (r *Router) BuildVarsFunc(f BuildVarsFunc) *Route
- func (r *Router) Get(name string) *Route
- func (r *Router) Handle(path string, handler http.Handler) *Route
- func (r *Router) HandleFunc(path string, f func(http.ResponseWriter, *http.Request)) *Route
- func (r *Router) Headers(pairs ...string) *Route
- func (r *Router) Host(tpl string) *Route
- func (r *Router) Match(req *http.Request, match *RouteMatch) bool
- func (r *Router) MatcherFunc(f MatcherFunc) *Route
- func (r *Router) Methods(methods ...string) *Route
- func (r *Router) Name(name string) *Route
- func (r *Router) NewRoute() *Route
- func (r *Router) Path(tpl string) *Route
- func (r *Router) PathPrefix(tpl string) *Route
- func (r *Router) Queries(pairs ...string) *Route
- func (r *Router) Schemes(schemes ...string) *Route
- func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (r *Router) SkipClean(value bool) *Router
- func (r *Router) StrictSlash(value bool) *Router
- func (r *Router) Use(mwf ...MiddlewareFunc)
- func (r *Router) UseEncodedPath() *Router
- func (r *Router) Walk(walkFn WalkFunc) error
- type WalkFunc
Constants ¶
This section is empty.
Variables ¶
var ( // ErrMethodMismatch 当请求中的方法不匹配时返回,针对路由定义的方法。 ErrMethodMismatch = errors.New("method is not allowed") // ErrNotFound 当没有找到匹配的路由时返回 ErrNotFound = errors.New("no matching route was found") )
var SkipRouter = errors.New("skip this router")
SkipRouter 从WalkFuncs返回一个SkipRouter值,walk将要下行到的路由器应该被跳过
Functions ¶
Types ¶
type BuildVarsFunc ¶
BuildVarsFunc 自定义构建变量是否使用函数签名(可以在构建路由URL之前修改路由变量)
type MatcherFunc ¶
type MatcherFunc func(*http.Request, *RouteMatch) bool
MatcherFunc 是自定义匹配器使用的函数签名
func (MatcherFunc) Match ¶
func (m MatcherFunc) Match(r *http.Request, match *RouteMatch) bool
Match 返回给定请求的匹配项
type MiddlewareFunc ¶
func CORSMethodMiddleware ¶
func CORSMethodMiddleware(r *Router) MiddlewareFunc
CORSMethodMiddleware 自动设置Access-Control-Allow-Methods响应头
func (MiddlewareFunc) Middleware ¶
func (mw MiddlewareFunc) Middleware(handler http.Handler) http.Handler
type Route ¶
type Route struct {
// contains filtered or unexported fields
}
Route 存储匹配请求和构建url的信息
func (*Route) BuildVarsFunc ¶
func (r *Route) BuildVarsFunc(f BuildVarsFunc) *Route
BuildVarsFunc 添加要用于修改生成变量的自定义函数(在构建路由的URL之前)
func (*Route) GetHostTemplate ¶
GetHostTemplate returns 返回主机匹配规则
func (*Route) GetPathRegexp ¶
GetPathRegexp 返回用于匹配路由路径的扩展正则表达式
func (*Route) GetPathTemplate ¶
GetPathTemplate 返回用于构建的模板
func (*Route) GetQueriesRegexp ¶
GetQueriesRegexp 对象匹配的扩展正则表达式
func (*Route) GetQueriesTemplates ¶
GetQueriesTemplates 返回查询的模板
func (*Route) HandlerFunc ¶
HandlerFunc 为路由设置处理函数
func (*Route) Headers ¶
Headers 为请求标头值添加匹配器 示例:
r := mux.NewRouter() r.Headers("Content-Type", "application/json", "X-Requested-With", "XMLHttpRequest")
面的路由只有在两个请求报头值匹配的情况下才会匹配,如果值是一个空字符串,它将匹配设置了键的任何值
func (*Route) HeadersRegexp ¶
HeadersRegexp 受一个键/值对序列,其中值具有正则表达式 示例:
r := mux.NewRouter() r.HeadersRegexp("Content-Type", "application/(text|json)", "X-Requested-With", "XMLHttpRequest")
只有当两个请求头都匹配两个正则表达式时,上面的路由才会匹配 如果值是一个空字符串,它将匹配设置了键的任何值 使用字符串锚的开始和结束符(^和$)来匹配精确的值
func (*Route) Host ¶
Host 为URL主机添加匹配器,它接受包含0个或多个URL变量的模板,这些变量用{}括起来 变量可以定义一个可选的regexp模式来匹配:
- {name} 匹配任何东西直到下一个点.
- {name:pattern} 匹配给定的regexp模式.
示例:
r := mux.NewRouter() r.Host("www.example.com") r.Host("{subdomain}.domain.com") r.Host("{subdomain:[a-z]+}.domain.com")
在给定路由中,变量名必须是唯一的。它们可以被检索到 调用 mux.Vars(request).
func (*Route) Match ¶
func (r *Route) Match(req *http.Request, match *RouteMatch) bool
Match 根据请求匹配路由
func (*Route) MatcherFunc ¶
func (r *Route) MatcherFunc(f MatcherFunc) *Route
MatcherFunc 添加要用作请求匹配器的自定义函数
func (*Route) Path ¶
Path 为URL路径添加匹配器 它接受包含0个或多个URL变量的模板,这些变量用{}括起来 模板必须以"/"开头 变量可以定义一个可选的regexp模式来匹配:
- {name} 匹配下一个斜杠之前的任何内容 - {name:pattern} 匹配给定的regexp模式
示例:
r := mux.NewRouter() r.Path("/products/").Handler(ProductsHandler) r.Path("/products/{key}").Handler(ProductsHandler) r.Path("/articles/{category}/{id:[0-9]+}"). Handler(ArticleHandler)
在给定路由中,变量名必须是唯一的 可通过 mux.Vars(request)调用
func (*Route) PathPrefix ¶
PathPrefix 为URL路径前缀添加一个匹配器见Route.Path()
func (*Route) Queries ¶
Queries 为URL查询值添加匹配器 接受一个键/值对序列。值可以定义变量 示例:
r := mux.NewRouter() r.Queries("foo", "bar", "id", "{id:[0-9]+}")
只有当URL包含定义的查询时,上面的路由才会匹配,如:?foo=bar&id=42 如果值是一个空字符串,它将匹配设置了键的任何值 变量可以定义一个可选的regexp模式来匹配 - {name} 匹配下一个斜杠之前的任何内容 - {name:pattern} 匹配给定的regexp模式
func (*Route) Subrouter ¶
Subrouter 为路由创建子路由器
只有当父路由匹配时,它才会走内部路由,如:
r := mux.NewRouter() s := r.Host("www.example.com").Subrouter() s.HandleFunc("/products/", ProductsHandler) s.HandleFunc("/products/{key}", ProductHandler) s.HandleFunc("/articles/{category}/{id:[0-9]+}"), ArticleHandler)
如果主机不匹配,也不会到子路由器
func (*Route) URL ¶
URL 为路由构建一个URL,示例:
r := mux.NewRouter() r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler). Name("article")
获取url示例:
url, err := r.Get("article").URL("category", "technology", "id", "42") "/articles/technology/42"
适用于主机变量:
r := mux.NewRouter() r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler). Host("{subdomain}.domain.com"). Name("article") // url.String() "http://news.domain.com/articles/technology/42" url, err := r.Get("article").URL("subdomain", "news", "category", "technology", "id", "42")
结果url的模式将是传递给Schemes的第一个参数:
// url.String() "https://example.com" r := mux.NewRouter() url, err := r.Host("example.com") .Schemes("https", "http").URL()
路由中定义的所有变量都是必需的,它们的值也必须是必需的
type RouteMatch ¶
type RouteMatch struct { Route *Route Handler http.Handler Vars map[string]string // MatchErr 设置为适当的匹配错误,如果存在不匹配,则设置为ErrMethodMismatch MatchErr error }
RouteMatch 保存匹配的路由信息
type Router ¶
type Router struct { // 404 NotFoundHandler http.Handler // 405不被允许 MethodNotAllowedHandler http.Handler // contains filtered or unexported fields }
Router 路由器
func (*Router) BuildVarsFunc ¶
func (r *Router) BuildVarsFunc(f BuildVarsFunc) *Route
BuildVarsFunc 用自定义函数注册一条新的路由
func (*Router) HandleFunc ¶
HandleFunc 用匹配器为URL路径注册一个新路由。
func (*Router) Match ¶
func (r *Router) Match(req *http.Request, match *RouteMatch) bool
Match 根据路由器注册的路由匹配给定的请求,match参数被填充
func (*Router) MatcherFunc ¶
func (r *Router) MatcherFunc(f MatcherFunc) *Route
MatcherFunc 用自定义匹配器函数注册一条新路由
func (*Router) PathPrefix ¶
PathPrefix 用URL路径前缀的匹配器注册一个新路由
func (*Router) ServeHTTP ¶
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP 分派匹配路由中注册的处理器,当有匹配时,可以调用mux.Vars(request)
func (*Router) StrictSlash ¶
StrictSlash 定义新路由的尾斜杠行为,默认值为false,子路由会继承此设置
func (*Router) Use ¶
func (r *Router) Use(mwf ...MiddlewareFunc)
func (*Router) UseEncodedPath ¶
UseEncodedPath 匹配经过编码的原始路径 如: "/path/foo%2Fbar/to" 会匹配到 "/path/{var}/to". 如果没被调用 "/path/foo%2Fbar/to" 匹配到 "/path/foo/bar/to"