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

Subrouting with httprouter #141

Closed
omgnuts opened this issue May 15, 2016 · 1 comment
Closed

Subrouting with httprouter #141

omgnuts opened this issue May 15, 2016 · 1 comment
Milestone

Comments

@omgnuts
Copy link

omgnuts commented May 15, 2016

Hi I'm implementing subgroups with httprouter, and applying middleware to specific route groups. Currently, my subware wrapper works simply like this below. Full source is here httprouter/subware.

    router := httprouter.New()
    router.GET("/", index)

    subrouter := subware.Path(router, "GET", "/protected/*path").
        UseFunc(middlewareA).
        UseHandle(middlewareB).
        UseMWFunc(middlewareC).
        SubRouter()
    {
        subrouter.GET("/protected/user/:id", appHandler("viewing: /protected/user/:id"))
        subrouter.GET("/protected/users", appHandler("viewing: /protected/users"))
    }

Httprouter works perfectly in the above example, but a minor issue is that when I'm trying to craft a route with a wildcard, ie router.GET("/protected/*", handle) vs router.GET("/protected/*path", handle), I get the following warning in httprouter/tree.go (~ lines 235)

// check if the wildcard has a name
if end-i < 2 {
    panic("wildcards must be named with a non-empty name in path '" + fullPath + "'")
}

May I know the side-effects of ignoring this check, beyond losing path as a parameter value in httprouters.Params?

edit: That is, router loses the path parameter in /protected/*path, but I don't really need it. The subrouter would have properly parsed a new set of parameters relevant to the subroutes. For example /protected/user/:id.

Many thanks!

@julienschmidt julienschmidt added this to the v2 milestone Oct 20, 2016
@julienschmidt
Copy link
Owner

I don't think there is a nice way to implement Subrouters without changing the router itself. Exactly that is planned for the next version: #89

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