-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
73 lines (60 loc) · 1.8 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package types
import (
"regexp"
)
var (
// IsAlphaNumeric defines a regular expression for matching against alpha-numeric
// values.
IsAlphaNumeric = regexp.MustCompile(`^[a-zA-Z0-9]+$`).MatchString
// IsAlphaLower defines regular expression to check if the string has lowercase
// alphabetic characters only.
IsAlphaLower = regexp.MustCompile(`^[a-z]+$`).MatchString
// IsAlphaUpper defines regular expression to check if the string has uppercase
// alphabetic characters only.
IsAlphaUpper = regexp.MustCompile(`^[A-Z]+$`).MatchString
// IsAlpha defines regular expression to check if the string has alphabetic
// characters only.
IsAlpha = regexp.MustCompile(`^[a-zA-Z]+$`).MatchString
// IsNumeric defines regular expression to check if the string has numeric
// characters only.
IsNumeric = regexp.MustCompile(`^[0-9]+$`).MatchString
)
// Router provides handlers for each transfer types.
type Router interface {
AddRoute(r string, h Handler) (rtr Router)
Route(path string) (h Handler)
}
// map a transfer types to a handler and an initgenesis function
type route struct {
r string
h Handler
}
type router struct {
routes []route
}
// nolint
// NewRouter - create new router
// TODO either make Function unexported or make return types (router) Exported
func NewRouter() *router {
return &router{
routes: make([]route, 0),
}
}
// AddRoute - TODO add description
func (rtr *router) AddRoute(r string, h Handler) Router {
if !IsAlphaNumeric(r) {
panic("route expressions can only contain alphanumeric characters")
}
rtr.routes = append(rtr.routes, route{r, h})
return rtr
}
// Route - TODO add description
// TODO handle expressive matches.
func (rtr *router) Route(path string) (h Handler) {
for _, route := range rtr.routes {
if route.r == path {
return route.h
}
}
return nil
}