router.go
3.63 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package beego
import (
"regexp"
"strings"
)
/*
Route
*/
// Represents a single route mapping
type Route struct {
pattern string
parameterKeys ParameterKeyMap
extension string
Path string
Controller Controller
MatcherFuncs []RouteMatcherFunc
}
func (r *Route) String() string {
return "{Route:'" + r.Path + "'}"
}
// Makes a new route from the given path
func makeRouteFromPath(path string) *Route {
// get the path segments
segments := getPathSegments(path)
regexSegments := make([]string, len(segments))
// prepare the parameter key map
var paramKeys ParameterKeyMap = make(ParameterKeyMap)
var extension string
// pull out any dynamic segments
for index, _ := range segments {
if isDynamicSegment(segments[index]) {
// e.g. {id}
paramKeys[strings.Trim(segments[index], "{}")] = index
regexSegments[index] = ROUTE_REGEX_PLACEHOLDER
} else if isExtensionSegment(segments[index]) {
// e.g. .json
extension = segments[index]
// trim off the last space (we don't need it)
regexSegments = regexSegments[0 : len(regexSegments)-1]
} else {
// e.g. "groups"
regexSegments[index] = segments[index]
}
}
patternString := "/" + strings.Join(regexSegments, "/")
// return a new route
var route *Route = new(Route)
route.pattern = patternString
route.extension = extension
route.parameterKeys = paramKeys
route.Path = path
route.Controller = nil
return route
}
// Gets the parameter values for the route from the specified path
func (route *Route) getParameterValueMap(path string) ParameterValueMap {
return getParameterValueMap(route.parameterKeys, path)
}
// Checks whether a path matches a route or not
func (route *Route) DoesMatchPath(path string) bool {
match, error := regexp.MatchString(route.pattern, path)
if error == nil {
if match {
if len(route.extension) > 0 {
// make sure the extensions match too
return strings.HasSuffix(strings.ToLower(path), strings.ToLower(route.extension))
} else {
return match
}
} else {
return false
}
}
// error :-(
return false
}
// Checks whether the context for this request matches the route
func (route *Route) DoesMatchContext(c *Context) bool {
// by default, we match
var match bool = true
if len(route.MatcherFuncs) > 0 {
// there are some matcher functions, so don't automatically
// match by default - let the matchers decide
match = false
// loop through the matcher functions
for _, f := range route.MatcherFuncs {
// modify 'match' based on the result of the matcher function
switch f(c) {
case NoMatch:
match = false
case Match:
match = true
}
}
}
// return the result
return match
}