2018-01-20 20:08:00 -08:00
|
|
|
package baseapp
|
2018-01-12 11:49:53 -08:00
|
|
|
|
2018-01-12 13:59:19 -08:00
|
|
|
import (
|
2019-02-05 19:11:57 -08:00
|
|
|
"fmt"
|
2018-01-12 13:59:19 -08:00
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
2019-10-14 15:05:10 -07:00
|
|
|
type Router struct {
|
2019-02-05 19:11:57 -08:00
|
|
|
routes map[string]sdk.Handler
|
2018-01-12 11:49:53 -08:00
|
|
|
}
|
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
var _ sdk.Router = NewRouter()
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// NewRouter returns a reference to a new router.
|
2019-10-14 15:05:10 -07:00
|
|
|
func NewRouter() *Router {
|
|
|
|
return &Router{
|
2019-02-05 19:11:57 -08:00
|
|
|
routes: make(map[string]sdk.Handler),
|
2018-01-12 11:49:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// AddRoute adds a route path to the router with a given handler. The route must
|
|
|
|
// be alphanumeric.
|
2019-10-14 15:05:10 -07:00
|
|
|
func (rtr *Router) AddRoute(path string, h sdk.Handler) sdk.Router {
|
2019-02-05 19:11:57 -08:00
|
|
|
if !isAlphaNumeric(path) {
|
2018-07-17 14:11:34 -07:00
|
|
|
panic("route expressions can only contain alphanumeric characters")
|
2018-01-12 11:49:53 -08:00
|
|
|
}
|
2019-02-05 19:11:57 -08:00
|
|
|
if rtr.routes[path] != nil {
|
|
|
|
panic(fmt.Sprintf("route %s has already been initialized", path))
|
|
|
|
}
|
2018-02-26 05:02:03 -08:00
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
rtr.routes[path] = h
|
2018-02-26 05:02:03 -08:00
|
|
|
return rtr
|
2018-01-12 11:49:53 -08:00
|
|
|
}
|
|
|
|
|
2019-02-05 19:11:57 -08:00
|
|
|
// Route returns a handler for a given route path.
|
|
|
|
//
|
|
|
|
// TODO: Handle expressive matches.
|
2020-01-04 11:50:38 -08:00
|
|
|
func (rtr *Router) Route(_ sdk.Context, path string) sdk.Handler {
|
2019-02-05 19:11:57 -08:00
|
|
|
return rtr.routes[path]
|
2018-01-12 11:49:53 -08:00
|
|
|
}
|