forked from go-goyave/goyave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
235 lines (208 loc) · 7.31 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package goyave
import (
"net/http"
"strings"
"github.com/System-Glitch/goyave/v2/config"
"github.com/System-Glitch/goyave/v2/cors"
"github.com/System-Glitch/goyave/v2/helper/filesystem"
"github.com/System-Glitch/goyave/v2/validation"
"github.com/gorilla/mux"
)
// Router registers routes to be matched and dispatches a handler.
type Router struct {
muxRouter *mux.Router
corsOptions *cors.Options
hasCORSMiddleware bool
middleware []Middleware
statusHandlers map[int]Handler
}
// Handler is a controller or middleware function
type Handler func(*Response, *Request)
func panicStatusHandler(response *Response, request *Request) {
response.Error(response.GetError())
if response.empty {
message := map[string]string{
"error": http.StatusText(response.GetStatus()),
}
response.JSON(response.GetStatus(), message)
}
}
func errorStatusHandler(response *Response, request *Request) {
message := map[string]string{
"error": http.StatusText(response.GetStatus()),
}
response.JSON(response.GetStatus(), message)
}
func (r *Router) muxStatusHandler(status int) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, rawRequest *http.Request) {
r.requestHandler(w, rawRequest, func(response *Response, r *Request) {
response.Status(status)
}, nil)
})
}
func newRouter() *Router {
muxRouter := mux.NewRouter()
muxRouter.Schemes(config.GetString("protocol"))
router := &Router{
muxRouter: muxRouter,
statusHandlers: map[int]Handler{},
}
muxRouter.NotFoundHandler = router.muxStatusHandler(http.StatusNotFound)
muxRouter.MethodNotAllowedHandler = router.muxStatusHandler(http.StatusMethodNotAllowed)
router.StatusHandler(panicStatusHandler, http.StatusInternalServerError)
router.StatusHandler(errorStatusHandler, 404, 405, 501, 502, 503, 504, 505, 506, 507, 508, 510, 511)
router.Middleware(recoveryMiddleware, parseRequestMiddleware, languageMiddleware)
return router
}
func (r *Router) copyStatusHandlers() map[int]Handler {
cpy := make(map[int]Handler)
for key, value := range r.statusHandlers {
cpy[key] = value
}
return cpy
}
// Subrouter create a new sub-router from this router.
// Use subrouters to create route groups and to apply middleware to multiple routes.
// CORS options are also inherited.
func (r *Router) Subrouter(prefix string) *Router {
router := &Router{
muxRouter: r.muxRouter.PathPrefix(prefix).Subrouter(),
corsOptions: r.corsOptions,
hasCORSMiddleware: r.hasCORSMiddleware,
statusHandlers: r.copyStatusHandlers(),
}
router.muxRouter.NotFoundHandler = router.muxStatusHandler(http.StatusNotFound)
router.muxRouter.MethodNotAllowedHandler = router.muxStatusHandler(http.StatusMethodNotAllowed)
// Apply parent middleware to subrouter
router.Middleware(r.middleware...)
return router
}
// Middleware apply one or more middleware to the route group.
func (r *Router) Middleware(middleware ...Middleware) {
r.middleware = append(r.middleware, middleware...)
}
// Route register a new route.
//
// Multiple methods can be passed using a pipe-separated string.
// "PUT|PATCH"
//
// The validation rules set is optional. If you don't want your route
// to be validated, pass "nil".
//
// If the router has CORS options set, the "OPTIONS" method is automatically added
// to the matcher if it's missing, so it allows preflight requests.
func (r *Router) Route(methods string, uri string, handler Handler, validationRules validation.RuleSet) {
r.route(methods, uri, handler, validationRules)
}
func (r *Router) route(methods string, uri string, handler Handler, validationRules validation.RuleSet) *mux.Route {
if r.corsOptions != nil && !strings.Contains(methods, "OPTIONS") {
methods += "|OPTIONS"
}
return r.muxRouter.HandleFunc(uri, func(w http.ResponseWriter, rawRequest *http.Request) {
r.requestHandler(w, rawRequest, handler, validationRules)
}).Methods(strings.Split(methods, "|")...)
}
// Static serve a directory and its subdirectories of static resources.
// Set the "download" parameter to true if you want the files to be sent as an attachment
// instead of an inline element.
//
// If no file is given in the url, or if the given file is a directory, the handler will
// send the "index.html" file if it exists.
func (r *Router) Static(uri string, directory string, download bool) {
r.Route("GET", uri+"{resource:.*}", staticHandler(directory, download), nil)
}
// CORS set the CORS options for this route group.
// If the options are not nil, the CORS middleware is automatically added.
func (r *Router) CORS(options *cors.Options) {
r.corsOptions = options
if options != nil && !r.hasCORSMiddleware {
r.Middleware(corsMiddleware)
r.hasCORSMiddleware = true
}
}
// StatusHandler set a handler for responses with an empty body.
// The handler will be automatically executed if the request's life-cycle reaches its end
// and nothing has been written in the response body.
//
// Multiple status codes can be given. The handler will be executed if one of them matches.
//
// This method can be used to define custom error handlers for example.
//
// Status handlers are inherited as a copy in sub-routers. Modifying a child's status handler
// will not modify its parent's.
//
// Codes in the 500 range and codes 404 and 405 have a default status handler.
func (r *Router) StatusHandler(handler Handler, status int, additionalStatuses ...int) {
r.statusHandlers[status] = handler
for _, s := range additionalStatuses {
r.statusHandlers[s] = handler
}
}
func staticHandler(directory string, download bool) Handler {
return func(response *Response, r *Request) {
file := r.Params["resource"]
path := cleanStaticPath(directory, file)
if filesystem.FileExists(path) {
if download {
response.Download(path, file[strings.LastIndex(file, "/")+1:])
} else {
response.File(path)
}
} else {
response.Status(http.StatusNotFound)
}
}
}
func cleanStaticPath(directory string, file string) string {
if strings.HasPrefix(file, "/") {
file = file[1:]
}
path := directory + "/" + file
if filesystem.IsDirectory(path) {
if !strings.HasSuffix(path, "/") {
path += "/"
}
path += "index.html"
}
return path
}
func (r *Router) requestHandler(w http.ResponseWriter, rawRequest *http.Request, handler Handler, rules validation.RuleSet) {
request := &Request{
httpRequest: rawRequest,
corsOptions: r.corsOptions,
Rules: rules,
Params: mux.Vars(rawRequest),
}
response := &Response{
httpRequest: rawRequest,
ResponseWriter: w,
empty: true,
status: 0,
}
// Validate last.
// Allows custom middleware to be executed after core
// middleware and before validation.
handler = validateRequestMiddleware(handler)
for i := len(r.middleware) - 1; i >= 0; i-- {
handler = r.middleware[i](handler)
}
handler(response, request)
r.finalize(response, request)
}
// finalize the request's life-cycle.
func (r *Router) finalize(response *Response, request *Request) {
if response.empty {
if response.status == 0 {
// If the response is empty, return status 204 to
// comply with RFC 7231, 6.3.5
response.Status(http.StatusNoContent)
} else if statusHandler, ok := r.statusHandlers[response.status]; ok {
// Status has been set but body is empty.
// Execute status handler if exists.
statusHandler(response, request)
}
}
if !response.wroteHeader {
response.WriteHeader(response.status)
}
}