-
Notifications
You must be signed in to change notification settings - Fork 3
/
router.go
65 lines (57 loc) · 1.41 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
package goui
import (
"regexp"
)
type route struct {
handler func(*Context)
paras []string //named parameters
regex *regexp.Regexp //if there are parameters
}
var (
//optionalPart = regexp.MustCompile(`\((.*?)\)`)
namedPart = regexp.MustCompile(`:([^/]+)`)
//splatPart = regexp.MustCompile(`\*\w+`)
escapeRegExp = regexp.MustCompile(`([\-{}\[\]+?.,\\\^$|#\s])`)
routes = make(map[string]*route)
)
func parseRoute(pattern string, route *route) {
params := namedPart.FindAllStringSubmatch(pattern, -1)
if params != nil {
l := len(params)
route.paras = make([]string, l)
//fmt.Println(params)
for i, param := range params {
route.paras[i] = param[1]
//fmt.Println(param[1])
}
pattern = namedPart.ReplaceAllString(pattern, `([^/]+)`)
route.regex = regexp.MustCompile(pattern)
}
routes[pattern] = route
}
func dispatch(url string) (handler func(*Context), params map[string]string) {
for key, route := range routes {
if route.regex == nil {
if key == url {
handler = route.handler
return
}
} else {
matches := route.regex.FindAllStringSubmatch(url, -1)
if matches != nil && len(matches) == 1 {
params = make(map[string]string)
vals := matches[0][1:]
l, lkey := len(vals), len(route.paras)
if l > lkey {
l = lkey
}
for i := 0; i < l; i++ {
params[route.paras[i]] = vals[i]
}
handler = route.handler
return
}
}
}
return
}