-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.go
186 lines (160 loc) · 3.23 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
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
package stgin
import (
"net/http"
"regexp"
"strings"
)
type API = func(c RequestContext) Status
type Route struct {
Path string
Method string
Action API
correspondingRegex *regexp.Regexp
controller *Controller
dir string
}
func (route Route) isStaticDir() bool { return route.dir != "" }
func (route Route) withPrefixPrepended(controllerPrefix string) Route {
route.Path = normalizePath(controllerPrefix + route.Path)
return route
}
func (route Route) acceptsAndPathParams(request *http.Request) (ok bool, params Params) {
if request.Method == route.Method {
params, ok = MatchAndExtractPathParams(&route, request.URL.Path)
}
return
}
func getRoutePatternRegexOrPanic(pattern string) *regexp.Regexp {
regex, err := getPatternCorrespondingRegex(pattern)
if err != nil {
panic(err)
}
return regex
}
func GET(path string, api API) Route {
return Route{
Path: path,
Method: "GET",
Action: api,
}
}
func PUT(path string, api API) Route {
return Route{
Path: path,
Method: "PUT",
Action: api,
}
}
func POST(path string, api API) Route {
return Route{
Path: path,
Method: "POST",
Action: api,
}
}
func DELETE(path string, api API) Route {
return Route{
Path: path,
Method: "DELETE",
Action: api,
}
}
func Prefix(path string) string {
endsWithSlash := endsWithSlashRegex.MatchString(path)
if endsWithSlash {
return path + ".*"
} else {
return path + "/" + ".*"
}
}
func PATCH(path string, api API) Route {
return Route{
Path: path,
Method: "PATCH",
Action: api,
}
}
func OPTIONS(path string, api API) Route {
return Route{
Path: path,
Method: "OPTIONS",
Action: api,
}
}
func StaticDir(pattern string, dir string) Route {
return Route{
Path: pattern,
Method: "GET",
dir: dir,
}
}
func Handle(method string, path string, api API) Route {
return Route{
Path: path,
Method: method,
Action: api,
}
}
type RouteCreationStage struct {
method string
path string
}
func (stage RouteCreationStage) Do(api API) Route {
switch strings.ToUpper(stage.method) {
case "GET":
return GET(stage.path, api)
case "PUT":
return PUT(stage.path, api)
case "POST":
return POST(stage.path, api)
case "DELETE":
return DELETE(stage.path, api)
case "PATCH":
return PATCH(stage.path, api)
default:
return GET(stage.path, api)
}
}
func OnGET(path string) RouteCreationStage {
return RouteCreationStage{
method: "GET",
path: path,
}
}
func OnPUT(path string) RouteCreationStage {
return RouteCreationStage{
method: "PUT",
path: path,
}
}
func OnPOST(path string) RouteCreationStage {
return RouteCreationStage{
method: "POST",
path: path,
}
}
func OnDelete(path string) RouteCreationStage {
return RouteCreationStage{
method: "DELETE",
path: path,
}
}
func OnPatch(path string) RouteCreationStage {
return RouteCreationStage{
method: "PATCH",
path: path,
}
}
func OnOptions(path string) RouteCreationStage {
return RouteCreationStage{
method: "OPTIONS",
path: path,
}
}
func OnPath(path string) RouteCreationStage {
return RouteCreationStage{path: path}
}
func (stage RouteCreationStage) WithMethod(method string) RouteCreationStage {
stage.method = method
return stage
}