This repository has been archived by the owner on Feb 24, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 579
/
route_mappings.go
358 lines (297 loc) · 10.3 KB
/
route_mappings.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package buffalo
import (
"fmt"
"net/http"
"net/url"
"os"
"path"
"reflect"
"strings"
"github.com/gobuffalo/envy"
"github.com/gobuffalo/flect/name"
"github.com/gorilla/handlers"
)
const (
// AssetsAgeVarName is the ENV variable used to specify max age when ServeFiles is used.
AssetsAgeVarName = "ASSETS_MAX_AGE"
)
// These method functions will be moved to Home structure.
// GET maps an HTTP "GET" request to the path and the specified handler.
func (a *App) GET(p string, h Handler) *RouteInfo {
return a.addRoute("GET", p, h)
}
// POST maps an HTTP "POST" request to the path and the specified handler.
func (a *App) POST(p string, h Handler) *RouteInfo {
return a.addRoute("POST", p, h)
}
// PUT maps an HTTP "PUT" request to the path and the specified handler.
func (a *App) PUT(p string, h Handler) *RouteInfo {
return a.addRoute("PUT", p, h)
}
// DELETE maps an HTTP "DELETE" request to the path and the specified handler.
func (a *App) DELETE(p string, h Handler) *RouteInfo {
return a.addRoute("DELETE", p, h)
}
// HEAD maps an HTTP "HEAD" request to the path and the specified handler.
func (a *App) HEAD(p string, h Handler) *RouteInfo {
return a.addRoute("HEAD", p, h)
}
// OPTIONS maps an HTTP "OPTIONS" request to the path and the specified handler.
func (a *App) OPTIONS(p string, h Handler) *RouteInfo {
return a.addRoute("OPTIONS", p, h)
}
// PATCH maps an HTTP "PATCH" request to the path and the specified handler.
func (a *App) PATCH(p string, h Handler) *RouteInfo {
return a.addRoute("PATCH", p, h)
}
// Redirect from one URL to another URL. Only works for "GET" requests.
func (a *App) Redirect(status int, from, to string) *RouteInfo {
return a.GET(from, func(c Context) error {
return c.Redirect(status, to)
})
}
// Mount mounts a http.Handler (or Buffalo app) and passes through all requests to it.
//
// func muxer() http.Handler {
// f := func(res http.ResponseWriter, req *http.Request) {
// fmt.Fprintf(res, "%s - %s", req.Method, req.URL.String())
// }
// mux := mux.NewRouter()
// mux.HandleFunc("/foo", f).Methods("GET")
// mux.HandleFunc("/bar", f).Methods("POST")
// mux.HandleFunc("/baz/baz", f).Methods("DELETE")
// return mux
// }
//
// a.Mount("/admin", muxer())
//
// $ curl -X DELETE http://localhost:3000/admin/baz/baz
func (a *App) Mount(p string, h http.Handler) {
prefix := path.Join(a.Prefix, p)
path := path.Join(p, "{path:.+}")
a.ANY(path, WrapHandler(http.StripPrefix(prefix, h)))
}
// ServeFiles maps an path to a directory on disk to serve static files.
// Useful for JavaScript, images, CSS, etc...
/*
a.ServeFiles("/assets", http.Dir("path/to/assets"))
*/
func (a *App) ServeFiles(p string, root http.FileSystem) {
path := path.Join(a.Prefix, p)
a.filepaths = append(a.filepaths, path)
h := stripAsset(path, a.fileServer(root), a)
a.router.PathPrefix(path).Handler(h)
}
func (a *App) fileServer(fs http.FileSystem) http.Handler {
fsh := http.FileServer(fs)
baseHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
f, err := fs.Open(path.Clean(r.URL.Path))
if os.IsNotExist(err) {
eh := a.ErrorHandlers.Get(http.StatusNotFound)
eh(http.StatusNotFound, fmt.Errorf("could not find %s", r.URL.Path), a.newContext(RouteInfo{}, w, r))
return
}
stat, _ := f.Stat()
maxAge := envy.Get(AssetsAgeVarName, "31536000")
w.Header().Add("ETag", fmt.Sprintf("%x", stat.ModTime().UnixNano()))
w.Header().Add("Cache-Control", fmt.Sprintf("max-age=%s", maxAge))
fsh.ServeHTTP(w, r)
})
if a.CompressFiles {
return handlers.CompressHandler(baseHandler)
}
return baseHandler
}
type newable interface {
New(Context) error
}
type editable interface {
Edit(Context) error
}
// Resource maps an implementation of the Resource interface
// to the appropriate RESTful mappings. Resource returns the *App
// associated with this group of mappings so you can set middleware, etc...
// on that group, just as if you had used the a.Group functionality.
//
// Resource automatically creates a URL `/resources/new` if the resource
// has a function `New()`. So it could act as a restriction for the value
// of `resource_id`. URL `/resources/new` will always show the resource
// creation page instead of showing the resource called `new`.
/*
a.Resource("/users", &UsersResource{})
// Is equal to this:
ur := &UsersResource{}
g := a.Group("/users")
g.GET("/", ur.List) // GET /users => ur.List
g.POST("/", ur.Create) // POST /users => ur.Create
g.GET("/new", ur.New) // GET /users/new => ur.New
g.GET("/{user_id}", ur.Show) // GET /users/{user_id} => ur.Show
g.PUT("/{user_id}", ur.Update) // PUT /users/{user_id} => ur.Update
g.DELETE("/{user_id}", ur.Destroy) // DELETE /users/{user_id} => ur.Destroy
g.GET("/{user_id}/edit", ur.Edit) // GET /users/{user_id}/edit => ur.Edit
*/
func (a *App) Resource(p string, r Resource) *App {
g := a.Group(p)
if mw, ok := r.(Middler); ok {
g.Use(mw.Use()...)
}
p = "/"
rv := reflect.ValueOf(r)
if rv.Kind() == reflect.Ptr {
rv = rv.Elem()
}
rt := rv.Type()
resourceName := rt.Name()
handlerName := fmt.Sprintf("%s.%s", rt.PkgPath(), resourceName) + ".%s"
n := strings.TrimSuffix(rt.Name(), "Resource")
paramName := name.New(n).ParamID().String()
type paramKeyable interface {
ParamKey() string
}
if pk, ok := r.(paramKeyable); ok {
paramName = pk.ParamKey()
}
spath := path.Join(p, "{"+paramName+"}")
// This order will become the order of route evaluation too.
setFuncKey(r.List, fmt.Sprintf(handlerName, "List"))
g.GET(p, r.List).ResourceName = resourceName
setFuncKey(r.Create, fmt.Sprintf(handlerName, "Create"))
g.POST(p, r.Create).ResourceName = resourceName
// NOTE: it makes restriction that resource id cannot be 'new'.
if n, ok := r.(newable); ok {
setFuncKey(n.New, fmt.Sprintf(handlerName, "New"))
g.GET(path.Join(p, "new"), n.New).ResourceName = resourceName
}
setFuncKey(r.Show, fmt.Sprintf(handlerName, "Show"))
g.GET(path.Join(spath), r.Show).ResourceName = resourceName
setFuncKey(r.Update, fmt.Sprintf(handlerName, "Update"))
g.PUT(path.Join(spath), r.Update).ResourceName = resourceName
setFuncKey(r.Destroy, fmt.Sprintf(handlerName, "Destroy"))
g.DELETE(path.Join(spath), r.Destroy).ResourceName = resourceName
if n, ok := r.(editable); ok {
setFuncKey(n.Edit, fmt.Sprintf(handlerName, "Edit"))
g.GET(path.Join(spath, "edit"), n.Edit).ResourceName = resourceName
}
g.Prefix = path.Join(g.Prefix, spath)
g.prefix = g.Prefix
return g
}
// ANY accepts a request across any HTTP method for the specified path
// and routes it to the specified Handler.
func (a *App) ANY(p string, h Handler) {
a.GET(p, h)
a.POST(p, h)
a.PUT(p, h)
a.PATCH(p, h)
a.HEAD(p, h)
a.OPTIONS(p, h)
a.DELETE(p, h)
}
// Group creates a new `*App` that inherits from it's parent `*App`.
// This is useful for creating groups of end-points that need to share
// common functionality, like middleware.
/*
g := a.Group("/api/v1")
g.Use(AuthorizeAPIMiddleware)
g.GET("/users, APIUsersHandler)
g.GET("/users/:user_id, APIUserShowHandler)
*/
func (a *App) Group(groupPath string) *App {
// TODO: move this function to app.go or home.go eventually.
g := New(a.Options)
// keep them for v0 compatibility
g.Prefix = path.Join(a.Prefix, groupPath)
g.Name = g.Prefix
// for Home structure
g.prefix = path.Join(a.prefix, groupPath)
g.host = a.host
g.name = g.prefix
g.router = a.router
g.RouteNamer = a.RouteNamer
g.Middleware = a.Middleware.clone()
g.ErrorHandlers = a.ErrorHandlers
g.app = a.app // will replace g.root
g.root = g.app // will be deprecated
// to be replaced with child Homes. currently, only used in grifts.
a.children = append(a.children, g)
return g
}
// VirtualHost creates a new `*App` that inherits from it's parent `*App`.
// All pre-configured things on the parent App such as middlewares will be
// applied, and can be modified only for this child App.
//
// This is a multi-homing feature similar to the `VirtualHost` in Apache
// or multiple `server`s in nginx. One important different behavior is that
// there is no concept of the `default` host in buffalo (at least for now)
// and the routing decision will be made with the "first match" manner.
// (e.g. if you have already set the route for '/' for the root App before
// setting up a virualhost, the route of the root App will be picked up
// even if the client makes a request to the specified domain.)
/*
a.VirtualHost("www.example.com")
a.VirtualHost("{subdomain}.example.com")
a.VirtualHost("{subdomain:[a-z]+}.example.com")
*/
func (a *App) VirtualHost(h string) *App {
g := a.Group("/")
g.host = h
g.router = a.router.Host(h).Subrouter()
return g
}
// RouteHelpers returns a map of BuildPathHelper() for each route available in the app.
func (a *App) RouteHelpers() map[string]RouteHelperFunc {
rh := map[string]RouteHelperFunc{}
for _, route := range a.Routes() {
cRoute := route
rh[cRoute.PathName] = cRoute.BuildPathHelper()
}
return rh
}
func (e *Home) addRoute(method string, url string, h Handler) *RouteInfo {
// NOTE: lock the root app (not this app). only the root has the affective
// routes list.
e.app.moot.Lock()
defer e.app.moot.Unlock()
url = path.Join(e.prefix, url)
url = e.app.normalizePath(url)
name := e.app.RouteNamer.NameRoute(url)
hs := funcKey(h)
r := &RouteInfo{
Method: method,
Path: url,
HandlerName: hs,
Handler: h,
App: e.appSelf, // CHKME: to be replaced with Home
Aliases: []string{},
}
r.MuxRoute = e.router.Handle(url, r).Methods(method)
r.Name(name)
routes := e.app.Routes()
routes = append(routes, r)
// NOTE: sorting is fancy but we lose the evaluation order information
// of routing decision. Let's keep the routes as registered order so
// developers can easily evaluate the order with `buffalo routes` and
// can debug any routing priority issue. (just keep the original line
// as history reference)
//sort.Sort(routes)
e.app.routes = routes
return r
}
func stripAsset(path string, h http.Handler, a *App) http.Handler {
if path == "" {
return h
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
up := r.URL.Path
up = strings.TrimPrefix(up, path)
up = strings.TrimSuffix(up, "/")
u, err := url.Parse(up)
if err != nil {
eh := a.ErrorHandlers.Get(http.StatusBadRequest)
eh(http.StatusBadRequest, err, a.newContext(RouteInfo{}, w, r))
return
}
r.URL = u
h.ServeHTTP(w, r)
})
}