Skip to content
This repository has been archived by the owner on Jul 29, 2023. It is now read-only.

Discussion: Subrouting #12

Open
moraes opened this issue Dec 1, 2015 · 1 comment
Open

Discussion: Subrouting #12

moraes opened this issue Dec 1, 2015 · 1 comment
Labels

Comments

@moraes
Copy link
Contributor

moraes commented Dec 1, 2015

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 Feature proposal: Better router/handler composition (*Router.Mount()) 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.

@obad2015
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
Status: No status
Development

No branches or pull requests

3 participants