Skip to content

Routing Group like interface not exposed in router #22

Description

@someone1

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions