/
router.go
209 lines (181 loc) · 4.13 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package prago
import (
"context"
"fmt"
"net/url"
"strings"
)
type router struct {
priorityRoutes []*route
routes []*route
}
func newRouter() *router {
return &router{
[]*route{},
[]*route{},
}
}
type routerConstraint func(context.Context, url.Values) bool
func (r *router) addRoute(route *route) {
if route.controller.priorityRouter {
r.priorityRoutes = append(r.priorityRoutes, route)
} else {
r.routes = append(r.routes, route)
}
}
func (r *router) process(request *Request) bool {
for _, routes := range [][]*route{
r.priorityRoutes,
r.routes,
} {
for _, route := range routes {
params, match := route.match(request.r.Context(), request.Request().Method, request.Request().URL.Path)
if match {
for k, values := range params {
for _, value := range values {
request.Params().Add(k, value)
}
}
route.controller.callArounds(request, 0, func() {
route.fn(request)
}, true)
return true
}
}
}
return false
}
func (r *router) export() (ret [][2]string) {
ret = append(ret, [2]string{"PRIORITY ROUTES", ""})
for _, v := range r.priorityRoutes {
ret = append(ret, [2]string{v.method, v.path})
}
ret = append(ret, [2]string{"NORMAL ROUTES", ""})
for _, v := range r.routes {
ret = append(ret, [2]string{v.method, v.path})
}
return
}
type route struct {
method string
path string
constraints []routerConstraint
pathMatcher pathMatcherFn
controller *controller
fn func(p *Request)
}
type pathMatcherFn func(string) (url.Values, bool)
func matcherBasic(route string) pathMatcherFn {
routeItems := strings.Split(route, "/")
return func(path string) (values url.Values, ok bool) {
items := strings.Split(path, "/")
values = map[string][]string{}
if len(items) != len(routeItems) {
return
}
for i := 0; i < len(items); i++ {
expect := routeItems[i]
if len(expect) > 1 && strings.HasPrefix(expect, ":") {
values.Add(expect[1:], items[i])
} else {
if expect != items[i] {
return
}
}
}
return values, true
}
}
func matcherStar(route string) pathMatcherFn {
if !strings.HasPrefix(route, "*") {
return nil
}
routeName := route[1:]
return func(path string) (values url.Values, ok bool) {
values = map[string][]string{}
if len(routeName) > 0 {
values.Add(routeName, path)
}
return values, true
}
}
func matcherStarMiddle(route string) pathMatcherFn {
starIndex := strings.Index(route, "/*")
if starIndex <= 0 {
return nil
}
prefix := route[0 : starIndex+1]
routeName := route[starIndex+2:]
return func(path string) (values url.Values, ok bool) {
if !strings.HasPrefix(path, prefix) {
return nil, false
}
values = map[string][]string{}
if len(routeName) > 0 {
values.Add(routeName, path[starIndex+1:])
}
return values, true
}
}
func isHTTPMethodValid(method string) bool {
if method == "ANY" ||
method == "GET" ||
method == "HEAD" ||
method == "POST" ||
method == "PUT" ||
method == "DELETE" ||
method == "CONNECT" ||
method == "OPTIONS" ||
method == "TRACE" ||
method == "PATCH" {
return true
}
return false
}
func newRoute(method string, path string, controller *controller, fn func(p *Request), constraints []routerConstraint) (ret *route) {
if !isHTTPMethodValid(method) {
panic(fmt.Sprintf("unknown method: %s", method))
}
ret = &route{
method: method,
path: path,
constraints: constraints,
controller: controller,
fn: fn,
}
for _, v := range []func(string) pathMatcherFn{matcherStar, matcherStarMiddle, matcherBasic} {
if ret.pathMatcher != nil {
break
}
ret.pathMatcher = v(path)
}
return
}
func (r *route) match(ctx context.Context, method, path string) (url.Values, bool) {
if !methodMatch(r.method, method) {
return nil, false
}
if !strings.HasPrefix(path, "/") {
return nil, false
}
m, ok := r.pathMatcher(path)
if !ok {
return nil, false
}
for _, constraint := range r.constraints {
ok = constraint(ctx, m)
if !ok {
return nil, false
}
}
return m, true
}
func methodMatch(m1, m2 string) bool {
if m1 == "ANY" {
return true
}
if len(m1) > 0 && m1 != m2 {
return false
}
return true
}