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!
A Routing Group like interface from the dimfeld/httptreemux package used for kami's router is not exposed on the
kami.Muxobject. 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 theprefixrouter 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 v2A quick (not thought out) example:
main.go:
api_v1.go:
api_v2.go:
I'm interested to hear thoughts on this and see whether or not this feature could be added to kami!