简体   繁体   中英

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"

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

Per the documentation for 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. See Route.PathPrefix().

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

PathPrefix adds a matcher for the URL path prefix. This matches if the given template is a prefix of the full URL path. See Route.Path() for details on the tpl argument.

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.

Also note that the setting of Router.StrictSlash() has no effect on routes with a PathPrefix matcher.


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/*".

So what you're looking for is:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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