How does 'PathPrefix' work in 'gorilla.mux' library for Go?
Asked Answered
C

1

7

I'm playing around with the gorilla.mux library for Go. I have the following configuration, but I cant figure out the URL to reach the HelloWorldXml method.

func main() {
    router := mux.NewRouter()
    router.HandleFunc("/{name}.xml", HelloWorldXml).
           PathPrefix("/products/")
    router.HandleFunc("/hello/{name}", HelloWorld)
    http.Handle("/", router)
    http.ListenAndServe(":8787",nil)
}

What would be the proper URL to use? http://localhost:8787/products/MyName.xml returns a 404.

Craal answered 10/9, 2013 at 13:38 Comment(0)
A
16
 func main() {
    router := mux.NewRouter()
    router.HandleFunc("/{name}.xml", HelloWorldXml)
    subrouter := router.PathPrefix("/products/").Subrouter()
    //localhost/products/item.xml
    subrouter.HandleFunc("/{name}.xml", HelloWorldXmlHandler)
    router.HandleFunc("/hello/{name}", HelloWorld)
    http.Handle("/", router)
    http.ListenAndServe(":8787",nil)
}
Arborization answered 10/9, 2013 at 16:29 Comment(2)
So, in order for the path prefix to work, you need to create a subrouter? I guess I suspected that, but I never tried it.Craal
Would appreciate some more context/info from the answer.Bogart

© 2022 - 2024 — McMap. All rights reserved.