muxy icon indicating copy to clipboard operation
muxy copied to clipboard

Discussion: Subrouting

Open moraes opened this issue 10 years ago • 1 comments

I detected two kinds of subrouting that we may want to support:

  1. Convenience setup for path and name prefix: when defining several routes that share a path or name prefix, it is nice to have a way to define the shared prefix once, then define the remaining based on that "subroute". It's avoids repeating yourself and also makes a bit easier to refactor a route setup;
  2. Mount (or "import") routes defined in a different package: as requested in gorilla/mux#136, this allows packages to define their routes independently. Then a main package imports the defined routers or setup mounting points for them;

These are somewhat related. The first one is simply "nice to have"; the second is not a so common use case, but why not? In any case, the first can be used by the second when mounting a foreign router in a given path. The docs for Mount() illustrate it better:

// Mount imports all routes from the given router into this one.
//
// Combined with Sub() and Name(), it is possible to submount a router
// defined in a different package using pattern and name prefixes:
//
//     r := New()
//     s := r.Sub("/admin").Name("admin:").Mount(admin.Router)
func (r *Router) Mount(router *Router) *Router {

These two kinds of subrouting require the three router methods exemplified in the docs above: Sub() (which creates a subrouter for a pattern prefix), Name() (sets a name prefix for the routes), and Mount() (imports routes defined elsewhere).

Given this initial draft, I invite you to share your thoughts about the subject.

moraes avatar Dec 01 '15 20:12 moraes

I think number 2 is a much needed design, it is already supported by https://github.com/go-chi/chi

obad2015 avatar Oct 11 '18 08:10 obad2015