mux: pathprefix and match rest of url?

5,788 views
Skip to first unread message

Jorge Peña

unread,
Dec 31, 2013, 4:17:27 AM12/31/13
to goril...@googlegroups.com
I'm wondering if there's a way to do path globbing, similar to rails [ http://guides.rubyonrails.org/routing.html#route-globbing-and-wildcard-segments ] so that "some/path/*someglob" can match anything, even "some/one/two/three" and the "one/two/three" is available as in params[:someglob].

I'm currently doing this manually, but I don't know if it's the correct or best way. I get the CurrentRoute(req), take the length of it and then use it to slice the request.URL and get everything after the prefix.

It works, I'm just wondering if there's a better way to do it, perhaps built into mux?

Srinath

unread,
Dec 31, 2013, 4:24:22 AM12/31/13
to goril...@googlegroups.com
Yes you can do it using mux. 

it is given in the gorilla toolkit mux page [1]


r := mux.NewRouter()
s := r.PathPrefix("/products").Subrouter()
// "/products/"
s.HandleFunc("/", ProductsHandler)
// "/products/{key}/"
s.HandleFunc("/{key}/", ProductHandler)
// "/products/{key}/details"
s.HandleFunc("/{key}/details", ProductDetailsHandler)
You can then access the keys in the URL using mux.Vars [2]






http://about.me/srinathgs
I write code @ Alamut
Srinath G S



--
You received this message because you are subscribed to the Google Groups "Gorilla web toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gorilla-web...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Jorge Peña

unread,
Dec 31, 2013, 7:27:13 PM12/31/13
to goril...@googlegroups.com
Thanks, I appreciate the response, but I think you misunderstood me.

I know how to access path parameters with mux.Vars. What I'd like to do is set everything after the prefix to a specific mux.Vars key.

For example, given a pattern like "/prefix/{catch}", I want "catch" to match _everything_ until the end of the path, so that "/prefix/one/two/three.txt" makes the mux.Vars key "catch" equal to "one/two/three.txt".

When I tried creating a pattern like this, the above situation made "catch" equal to _only_ "one", that is, everything up until the next slash.

For example, in martini it would be "/prefix/**" accessible as params["_1"] (ref: https://github.com/codegangsta/martini/blob/master/router_test.go#L61 )

So I'm wondering if this is expected behavior, if perhaps I did it wrong, or if this isn't possible in mux and must be done manually, as I'm doing right now.

Thanks!

Kamil Kisiel

unread,
Dec 31, 2013, 9:37:56 PM12/31/13
to goril...@googlegroups.com
This should work:

package main

import (
"fmt"
"net/http"
"net/http/httptest"

)

func main() {
r := mux.NewRouter()
r.HandleFunc("/foo/{rest:.*}", func(w http.ResponseWriter, req *http.Request) {
fmt.Println(mux.Vars(req)["rest"])
fmt.Println(req.URL.Path)
})
req, _ := http.NewRequest("GET", "/foo/some/more/things", nil)
r.ServeHTTP(&httptest.ResponseRecorder{}, req)

Jorge Peña

unread,
Dec 31, 2013, 10:31:40 PM12/31/13
to goril...@googlegroups.com
Hah, thanks, it was that simple. I did think of trying that at first, but since every other system seems to have a unique syntax for it I was wondering if maybe mux did as well. I looked at the unit tests and everything and didn't see anything though, so I guess I should've just went with this.

Thanks :)
Reply all
Reply to author
Forward
0 new messages