mux Subrouter problem

47 views
Skip to first unread message

OSubachev

unread,
Feb 5, 2019, 8:24:46 AM2/5/19
to Gorilla web toolkit
This code works OK:   rtr := mux.NewRouter()
  rtr.
    Path("/customer/{id}").
    Methods(http.MethodGet).
    HandlerFunc(customerGetByIDHandleFunc)

But this yields '404 page not found':
  rtr := mux.NewRouter()
  rtrCust := rtr.Path("/customer").Subrouter()
  rtrCust.
    Path("/{id}").
    Methods(http.MethodGet).
    HandlerFunc(customerGetByIDHandleFunc)

What's wrong ?

Ben Hartshorne

unread,
Feb 5, 2019, 12:49:36 PM2/5/19
to goril...@googlegroups.com
Hi,

Caveat - I’m not at a place I can test this.

I think you want PathPrefix when adding /customer rather than Path. That will allow the sub router to match and then go through to test against the route with the ID.

-ben

--
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/d/optout.
--
https://honeycomb.io - a power tool for engineers debugging and visualizing complex systems

OSubachev

unread,
Feb 5, 2019, 1:33:07 PM2/5/19
to Gorilla web toolkit
Hi Ben:

PathPrefix does not work either.
It does not yield '404 page not found', but instead it calls HandleFunc registered for the subrouter path itself:

rtr := mux.NewRouter()
  rtrCust := rtr.Path("/customer").Subrouter()
  rtrCust.
    Methods(http.MethodGet).
 HandlerFunc(customersGetHandleFunc)
rtrCust.
    Path("/{id}").
    Methods(http.MethodGet).
    HandlerFunc(customerGetByIDHandleFunc)

customersGetHandleFunc gets called instead of customerGetByIDHandleFunc.

Ben Hartshorne

unread,
Feb 5, 2019, 7:22:29 PM2/5/19
to goril...@googlegroups.com
Hi,

Sorry, I guess I didn't really understand the behavior you were going for. If you want to delegate handling all routes under a prefix to a subrouter, you should use PathPrefix. What the handler does once it gets there is a separate question.

I put up a demonstration of what I mean here: https://gist.github.com/maplebed/313c3208a80433cb60d1a07a995b6510

Hopefully that is enough to get you going in the right direction. Here is a transcript of the calls I'm expecting you to need to handle with that routing tree:

➜  curl localhost:8080/customer
handling customers
➜  curl localhost:8080/customer/
handling customers
➜  curl localhost:8080/customer/foo
handling ID foo

Cheers,

-ben

p.s. There is a more elegant way of handling paths that optionally end in a / involving some of the the mux settings, but I don't remember what they are. I encourage you to set up a simple example like the one in that gist and make all the manipulations there, then take what you've learned to your larger application. It yields a very fast turnaround cycle for testing out behavior.

--
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/d/optout.
Reply all
Reply to author
Forward
0 new messages