Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Routing Group like interface not exposed in router #22

Open
someone1 opened this issue May 9, 2016 · 1 comment
Open

Routing Group like interface not exposed in router #22

someone1 opened this issue May 9, 2016 · 1 comment

Comments

@someone1
Copy link
Contributor

someone1 commented May 9, 2016

A Routing Group like interface from the dimfeld/httptreemux package used for kami's router is not exposed on the kami.Mux object. This would simplify larger projects when building URLs.

For example, if I wanted to setup my application with multiple API versions, I could have my code split between server/endpoint logic as follows:

package server - > holds a simple main.go that imports packages from various API imports, sets up a user auth middleware on the context
package api/foo/v1 -> has a LoadRoutes(ctx context.Context, prefix *kami.Mux) function that adds its own group of routes building on the prefix supplied by the prefix router passed in. (e.g. if the group router that was passed in is for '/api/', this adds the group of routes for '/v1/' which when added to the group translates to '/api/v1')
package api/foo/v2 -> has a LoadRoutes(ctx context.Context, prefix *kami.Mux) that is the same as v1 except adds the group of routes for v2

A quick (not thought out) example:
main.go:

package server

import (
    //...

    " api/foo/v1"
    " api/foo/v2"

    "github.com/guregu/kami"
)

func init() {
    // Setup Auth middleware and other things
    kami.Use('/', Login)
    // ... Other things...

    group := kami.NewGroup('/api')
    v1.LoadRoutes(kami.Context, group)
    v2.LoadRoutes(kami.Context, group)
}

api_v1.go:

package v1

import (
    //...
    "github.com/guregu/kami"
)

func LoadRoutes(ctx context.Context, prefix  *kami.Mux) {
    v1Prefix := prefix.NewGroup("/v1")
    v1Prefix.Context = ctx
    v1Prefix.Get('/list', ListHandler)
    // Other routes...
}

api_v2.go:

package v2

import (
    //...
    "github.com/guregu/kami"
)

func LoadRoutes(ctx context.Context, prefix  *kami.Mux) {
    v2Prefix := prefix.NewGroup("/v2")
    v2Prefix.Context = ctx
    v2Prefix.Get('/list', ListHandler)
    // Other routes...
}

I'm interested to hear thoughts on this and see whether or not this feature could be added to kami!

@guregu
Copy link
Owner

guregu commented May 20, 2016

Hello, could you be more specific about how groups would work? Unfortunately we can't merge arbitrary contexts, so it might be impossible to implement.
I can make it easier to use different contexts. I think I will add a way to copy URL params from one context to another. Right now the lack of such a function makes it difficult to use middleware that completely replaces the context.

For routes like:

/api/v1/...
/api/v2/...

You don't really need groups to separate them. You can add common middleware with kami.Use("/api/", mw). Then you can add v1 middleware like kami.Use("/api/v1/", mw) and v2 middleware like kami.Use("/api/v2/", mw). You can add multiple middleware functions to the same route.

I'll also get #21 taken care of so it's easier to run kami from a kami.Mux.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants