Skip to content

Subrouting with httprouter #141

@omgnuts

Description

@omgnuts

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions