-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.go
75 lines (59 loc) · 1.18 KB
/
route.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
74
75
package httpserve
func newRoute(url string, h Handler, method string) (rp *route, err error) {
if url[0] != '/' {
err = ErrMissingLeadSlash
return
}
var r route
if r.s, err = getParts(url); err != nil {
return
}
r.method = method
r.h = h
rp = &r
return
}
type route struct {
s []string
h Handler
method string
}
func (r *route) numParams() (n int) {
for _, part := range r.s {
if part[0] != colon {
continue
}
n++
}
return
}
// check will check a url for a match, it will also return any associated parameters
func (r *route) check(p Params, url string) (out Params, ok bool) {
out = p
for _, part := range r.s {
switch {
case len(url) == 0:
return
case part[0] == colon:
// Skip forward to avoid slash
param, n := newParam(part, url[1:])
// Increment N to account for skipping
n++
out = append(out, param)
url = shiftStr(url, n)
case part[0] == '*':
ok = true
return
case isPartMatch(url, part):
// Part matches, increment and move on
url = shiftStr(url, len(part))
default:
// We do not have a match, return
return
}
}
ok = len(url) == 0
return
}
type routes []*route
type routesMap map[string]routes