forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
41 lines (33 loc) · 904 Bytes
/
router.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package baseapp
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
)
type Router struct {
routes map[string]sdk.Handler
}
var _ sdk.Router = NewRouter()
// NewRouter returns a reference to a new router.
func NewRouter() *Router {
return &Router{
routes: make(map[string]sdk.Handler),
}
}
// AddRoute adds a route path to the router with a given handler. The route must
// be alphanumeric.
func (rtr *Router) AddRoute(path string, h sdk.Handler) sdk.Router {
if !sdk.IsAlphaNumeric(path) {
panic("route expressions can only contain alphanumeric characters")
}
if rtr.routes[path] != nil {
panic(fmt.Sprintf("route %s has already been initialized", path))
}
rtr.routes[path] = h
return rtr
}
// Route returns a handler for a given route path.
//
// TODO: Handle expressive matches.
func (rtr *Router) Route(_ sdk.Context, path string) sdk.Handler {
return rtr.routes[path]
}