简体   繁体   English

如何在Go中以某个网址开头的任何网址进行处理?

[英]How Can I handle any url beginning with some url in Go?

Hi I am using gorilla/mux in go, and I want to handle any url that begins with : "/a/b/c" 嗨,我在go中使用了大猩猩/ mux,我想处理任何以“ / a / b / c”开头的网址

I tried: 我试过了:

router := mux.NewRouter().StrictSlash(true)
router.HandleFunc(`/a/b/{_dummy:c(\/)?.*}`, Func1)

that is the url can be /a/b/c/d or /a/b/c/d/e 该网址可以是/ a / b / c / d或/ a / b / c / d / e

Per the documentation for gorilla/mux: http://www.gorillatoolkit.org/pkg/mux#Route.PathPrefix 根据gorilla / mux的文档: http : //www.gorillatoolkit.org/pkg/mux#Route.PathPrefix

func (r *Router) PathPrefix(tpl string) *Route

PathPrefix registers a new route with a matcher for the URL path prefix. PathPrefix使用匹配器为URL路径前缀注册新路由。 See Route.PathPrefix(). 请参见Route.PathPrefix()。

func (r *Route) PathPrefix(tpl string) *Route

PathPrefix adds a matcher for the URL path prefix. PathPrefix为URL路径前缀添加一个匹配器。 This matches if the given template is a prefix of the full URL path. 如果给定的模板是完整URL路径的前缀,则此匹配。 See Route.Path() for details on the tpl argument. 有关tpl参数的详细信息,请参见Route.Path()。

Note that it does not treat slashes specially ("/foobar/" will be matched by the prefix "/foo") so you may want to use a trailing slash here. 请注意,它不对斜杠进行特殊处理(“ / foobar /”将与前缀“ / foo”匹配),因此您可能要在此处使用斜杠。

Also note that the setting of Router.StrictSlash() has no effect on routes with a PathPrefix matcher. 还要注意,Router.StrictSlash()的设置对具有PathPrefix匹配器的路由没有影响。


Note that the path provided to PathPrefix() represents a "wildcard": calling PathPrefix("/static/").Handler(...) means that the handler will be passed any request that matches "/static/*". 请注意,提供给PathPrefix()的路径表示一个“通配符”:调用PathPrefix(“ / static /”)。Handler(...)意味着将向处理程序传递与“ / static / *”匹配的任何请求。

So what you're looking for is: 因此,您正在寻找的是:

router := mux.NewRouter()
router.PathPrefix("/a/b/c/").HandleFunc(proxy.GrafanaHandler) // matches /a/b/c/*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM