-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.go
167 lines (139 loc) · 3.28 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
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
expectedQueries queries
}
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) (bool, Params) {
var ok bool
var params Params
if request.Method == route.Method {
params, ok = MatchAndExtractPathParams(&route, request.URL.Path)
}
return ok, params
}
func getRoutePatternRegexOrPanic(pattern string) *regexp.Regexp {
regex, err := getPatternCorrespondingRegex(pattern)
if err != nil {
panic(err)
}
return regex
}
func mkRoute(pattern string, api API, method string) Route {
path, queryDefs := splitBy(pattern, "?")
return Route {
Path: path,
Method: method,
Action: api,
expectedQueries: getQueryDefinitionsFromPattern(queryDefs),
}
}
func GET(pattern string, api API) Route {
return mkRoute(pattern, api, "GET")
}
func PUT(pattern string, api API) Route {
return mkRoute(pattern, api, "PUT")
}
func POST(pattern string, api API) Route {
return mkRoute(pattern, api, "POST")
}
func DELETE(pattern string, api API) Route {
return mkRoute(pattern, api, "DELETE")
}
func Prefix(path string) string {
return normalizePath("/" + path + "/.*")
}
func PATCH(pattern string, api API) Route {
return mkRoute(pattern, api, "PATCH")
}
func OPTIONS(pattern string, api API) Route {
return mkRoute(pattern, api, "OPTIONS")
}
func StaticDir(pattern string, dir string) Route {
return Route{
Path: normalizePath("/" + pattern + "/"),
Method: "GET",
dir: dir,
}
}
func Handle(method string, pattern string, api API) Route {
return mkRoute(pattern, api, method)
}
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
}