forked from revel/revel
-
Notifications
You must be signed in to change notification settings - Fork 2
/
controller.go
496 lines (427 loc) · 15.6 KB
/
controller.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
// Revel Framework source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package revel
import (
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
"time"
)
// Controller Revel's controller structure that gets embedded in user defined
// controllers
type Controller struct {
Name string // The controller name, e.g. "Application"
Type *ControllerType // A description of the controller type.
MethodName string // The method name, e.g. "Index"
MethodType *MethodType // A description of the invoked action type.
AppController interface{} // The controller that was instantiated.
Action string // The fully qualified action name, e.g. "App.Index"
ClientIP string // holds IP address of request came from
Request *Request
Response *Response
Result Result
Flash Flash // User cookie, cleared after 1 request.
Session Session // Session, stored in cookie, signed.
Params *Params // Parameters from URL and form (including multipart).
Args map[string]interface{} // Per-request scratch space.
ViewArgs map[string]interface{} // Variables passed to the template.
Validation *Validation // Data validation helpers
}
// The map of controllers, controllers are mapped by using the namespace|controller_name as the key
var controllers = make(map[string]*ControllerType)
// NewController returns new controller instance for Request and Response
func NewController(req *Request, resp *Response) *Controller {
return &Controller{
Request: req,
Response: resp,
Params: new(Params),
Args: map[string]interface{}{},
ViewArgs: map[string]interface{}{
"RunMode": RunMode,
"DevMode": DevMode,
},
}
}
// FlashParams serializes the contents of Controller.Params to the Flash
// cookie.
func (c *Controller) FlashParams() {
for key, vals := range c.Params.Values {
c.Flash.Out[key] = strings.Join(vals, ",")
}
}
func (c *Controller) SetCookie(cookie *http.Cookie) {
http.SetCookie(c.Response.Out, cookie)
}
func (c *Controller) RenderError(err error) Result {
c.setStatusIfNil(http.StatusInternalServerError)
return ErrorResult{c.ViewArgs, err}
}
func (c *Controller) setStatusIfNil(status int) {
if c.Response.Status == 0 {
c.Response.Status = status
}
}
// Render a template corresponding to the calling Controller method.
// Arguments will be added to c.ViewArgs prior to rendering the template.
// They are keyed on their local identifier.
//
// For example:
//
// func (c Users) ShowUser(id int) revel.Result {
// user := loadUser(id)
// return c.Render(user)
// }
//
// This action will render views/Users/ShowUser.html, passing in an extra
// key-value "user": (User).
//
// This is the slower magical version which uses the runtime
// to determine
// 1) Set c.ViewArgs to the arguments passed into this function
// 2) How to call the RenderTemplate by building the following line
// c.RenderTemplate(c.Name + "/" + c.MethodType.Name + "." + c.Request.Format)
//
// If you want your code to run faster it is recommended you add the template values directly
// to the c.ViewArgs and call c.RenderTemplate directly
func (c *Controller) Render(extraViewArgs ...interface{}) Result {
c.setStatusIfNil(http.StatusOK)
// Get the calling function line number.
_, _, line, ok := runtime.Caller(1)
if !ok {
ERROR.Println("Failed to get Caller information")
}
// Get the extra ViewArgs passed in.
if renderArgNames, ok := c.MethodType.RenderArgNames[line]; ok {
if len(renderArgNames) == len(extraViewArgs) {
for i, extraRenderArg := range extraViewArgs {
c.ViewArgs[renderArgNames[i]] = extraRenderArg
}
} else {
ERROR.Println(len(renderArgNames), "RenderArg names found for",
len(extraViewArgs), "extra ViewArgs")
}
} else {
ERROR.Println("No RenderArg names found for Render call on line", line,
"(Action", c.Action, ")")
}
return c.RenderTemplate(c.Name + "/" + c.MethodType.Name + "." + c.Request.Format)
}
// RenderTemplate method does less magical way to render a template.
// Renders the given template, using the current ViewArgs.
func (c *Controller) RenderTemplate(templatePath string) Result {
c.setStatusIfNil(http.StatusOK)
// Get the Template.
lang, _ := c.ViewArgs[CurrentLocaleViewArg].(string)
template, err := MainTemplateLoader.TemplateLang(templatePath, lang)
if err != nil {
return c.RenderError(err)
}
return &RenderTemplateResult{
Template: template,
ViewArgs: c.ViewArgs,
}
}
// RenderJSON uses encoding/json.Marshal to return JSON to the client.
func (c *Controller) RenderJSON(o interface{}) Result {
c.setStatusIfNil(http.StatusOK)
return RenderJSONResult{o, ""}
}
// RenderJSONP renders JSONP result using encoding/json.Marshal
func (c *Controller) RenderJSONP(callback string, o interface{}) Result {
c.setStatusIfNil(http.StatusOK)
return RenderJSONResult{o, callback}
}
// RenderXML uses encoding/xml.Marshal to return XML to the client.
func (c *Controller) RenderXML(o interface{}) Result {
c.setStatusIfNil(http.StatusOK)
return RenderXMLResult{o}
}
// RenderText renders plaintext in response, printf style.
func (c *Controller) RenderText(text string, objs ...interface{}) Result {
c.setStatusIfNil(http.StatusOK)
finalText := text
if len(objs) > 0 {
finalText = fmt.Sprintf(text, objs...)
}
return &RenderTextResult{finalText}
}
// RenderHTML renders html in response
func (c *Controller) RenderHTML(html string) Result {
c.setStatusIfNil(http.StatusOK)
return &RenderHTMLResult{html}
}
// Todo returns an HTTP 501 Not Implemented "todo" indicating that the
// action isn't done yet.
func (c *Controller) Todo() Result {
c.Response.Status = http.StatusNotImplemented
return c.RenderError(&Error{
Title: "TODO",
Description: "This action is not implemented",
})
}
// NotFound returns an HTTP 404 Not Found response whose body is the
// formatted string of msg and objs.
func (c *Controller) NotFound(msg string, objs ...interface{}) Result {
finalText := msg
if len(objs) > 0 {
finalText = fmt.Sprintf(msg, objs...)
}
c.Response.Status = http.StatusNotFound
return c.RenderError(&Error{
Title: "Not Found",
Description: finalText,
})
}
// Forbidden returns an HTTP 403 Forbidden response whose body is the
// formatted string of msg and objs.
func (c *Controller) Forbidden(msg string, objs ...interface{}) Result {
finalText := msg
if len(objs) > 0 {
finalText = fmt.Sprintf(msg, objs...)
}
c.Response.Status = http.StatusForbidden
return c.RenderError(&Error{
Title: "Forbidden",
Description: finalText,
})
}
// RenderFile returns a file, either displayed inline or downloaded
// as an attachment. The name and size are taken from the file info.
func (c *Controller) RenderFile(file *os.File, delivery ContentDisposition) Result {
c.setStatusIfNil(http.StatusOK)
var (
modtime = time.Now()
fileInfo, err = file.Stat()
)
if err != nil {
WARN.Println("RenderFile error:", err)
}
if fileInfo != nil {
modtime = fileInfo.ModTime()
}
return c.RenderBinary(file, filepath.Base(file.Name()), delivery, modtime)
}
// RenderBinary is like RenderFile() except that it instead of a file on disk,
// it renders data from memory (which could be a file that has not been written,
// the output from some function, or bytes streamed from somewhere else, as long
// it implements io.Reader). When called directly on something generated or
// streamed, modtime should mostly likely be time.Now().
func (c *Controller) RenderBinary(memfile io.Reader, filename string, delivery ContentDisposition, modtime time.Time) Result {
c.setStatusIfNil(http.StatusOK)
return &BinaryResult{
Reader: memfile,
Name: filename,
Delivery: delivery,
Length: -1, // http.ServeContent gets the length itself unless memfile is a stream.
ModTime: modtime,
}
}
// Redirect to an action or to a URL.
// c.Redirect(Controller.Action)
// c.Redirect("/controller/action")
// c.Redirect("/controller/%d/action", id)
func (c *Controller) Redirect(val interface{}, args ...interface{}) Result {
c.setStatusIfNil(http.StatusFound)
if url, ok := val.(string); ok {
if len(args) == 0 {
return &RedirectToURLResult{url}
}
return &RedirectToURLResult{fmt.Sprintf(url, args...)}
}
return &RedirectToActionResult{val}
}
// Message performs a lookup for the given message name using the given
// arguments using the current language defined for this controller.
//
// The current language is set by the i18n plugin.
func (c *Controller) Message(message string, args ...interface{}) (value string) {
return MessageFunc(c.Request.Locale, message, args...)
}
// SetAction sets the action that is being invoked in the current request.
// It sets the following properties: Name, Action, Type, MethodType
func (c *Controller) SetAction(controllerName, methodName string) error {
return c.SetTypeAction(controllerName, methodName, nil)
}
// SetAction sets the assigns the Controller type, sets the action and initializes the controller
func (c *Controller) SetTypeAction(controllerName, methodName string, typeOfController *ControllerType) error {
// Look up the controller and method types.
if typeOfController== nil {
if c.Type = ControllerTypeByName(controllerName, anyModule); c.Type == nil {
return errors.New("revel/controller: failed to find controller " + controllerName)
}
} else {
c.Type = typeOfController
}
// Note method name is case insensitive search
if c.MethodType = c.Type.Method(methodName); c.MethodType == nil {
return errors.New("revel/controller: failed to find action " + methodName)
}
c.Name, c.MethodName = c.Type.Type.Name(), c.MethodType.Name
c.Action = c.Name + "." + c.MethodName
// Instantiate the controller.
c.AppController = initNewAppController(c.Type, c).Interface()
return nil
}
func ControllerTypeByName(controllerName string, moduleSource *Module) (c *ControllerType) {
var found bool
if c, found = controllers[controllerName]; !found {
// Backup, passed in controllerName should be in lower case, but may not be
if c, found = controllers[strings.ToLower(controllerName)]; !found {
INFO.Printf("Cannot find controller name '%s' in controllers map ", controllerName)
// Search for the controller by name
for _, cType := range controllers {
testControllerName := strings.ToLower(cType.Type.Name())
if testControllerName == strings.ToLower(controllerName) && (cType.ModuleSource == moduleSource || moduleSource == anyModule) {
WARN.Printf("Matched empty namespace controller for %s to this %s", controllerName, cType.ModuleSource.Name)
c = cType
found = true
break
}
}
}
}
return
}
// This is a helper that initializes (zeros) a new app controller value.
// Specifically, it sets all *revel.Controller embedded types to the provided controller.
// Returns a value representing a pointer to the new app controller.
func initNewAppController(appControllerType *ControllerType, c *Controller) reflect.Value {
var (
appControllerPtr = reflect.New(appControllerType.Type)
appController = appControllerPtr.Elem()
cValue = reflect.ValueOf(c)
)
for _, index := range appControllerType.ControllerIndexes {
appController.FieldByIndex(index).Set(cValue)
}
return appControllerPtr
}
func findControllers(appControllerType reflect.Type) (indexes [][]int) {
// It might be a multi-level embedding. To find the controllers, we follow
// every anonymous field, using breadth-first search.
type nodeType struct {
val reflect.Value
index []int
}
appControllerPtr := reflect.New(appControllerType)
queue := []nodeType{{appControllerPtr, []int{}}}
for len(queue) > 0 {
// Get the next value and de-reference it if necessary.
var (
node = queue[0]
elem = node.val
elemType = elem.Type()
)
if elemType.Kind() == reflect.Ptr {
elem = elem.Elem()
elemType = elem.Type()
}
queue = queue[1:]
// #944 if the type's Kind is not `Struct` move on,
// otherwise `elem.NumField()` will panic
if elemType.Kind() != reflect.Struct {
continue
}
// Look at all the struct fields.
for i := 0; i < elem.NumField(); i++ {
// If this is not an anonymous field, skip it.
structField := elemType.Field(i)
if !structField.Anonymous {
continue
}
fieldValue := elem.Field(i)
fieldType := structField.Type
// If it's a Controller, record the field indexes to get here.
if fieldType == controllerPtrType {
indexes = append(indexes, append(node.index, i))
continue
}
queue = append(queue,
nodeType{fieldValue, append(append([]int{}, node.index...), i)})
}
}
return
}
// Controller registry and types.
type ControllerType struct {
Namespace string // The namespace of the controller
ModuleSource *Module // The module for the controller
Type reflect.Type
Methods []*MethodType
ControllerIndexes [][]int // FieldByIndex to all embedded *Controllers
}
type MethodType struct {
Name string
Args []*MethodArg
RenderArgNames map[int][]string
lowerName string
}
type MethodArg struct {
Name string
Type reflect.Type
}
// Adds the controller to the controllers map using its namespace, also adds it to the module list of controllers.
// If the controller is in the main application it is added without its namespace as well.
func AddControllerType(moduleSource *Module,controllerType reflect.Type,methods []*MethodType) (newControllerType *ControllerType) {
if moduleSource==nil {
moduleSource = appModule
}
newControllerType = &ControllerType{ModuleSource:moduleSource,Type:controllerType,Methods:methods,ControllerIndexes:findControllers(controllerType)}
newControllerType.Namespace = moduleSource.Namespace()
controllerName := newControllerType.Name()
// Store the first controller only in the controllers map with the unmapped namespace.
if _, found := controllers[controllerName]; !found {
controllers[controllerName] = newControllerType
newControllerType.ModuleSource.AddController(newControllerType)
if newControllerType.ModuleSource == appModule {
// Add the controller mapping into the global namespace
controllers[newControllerType.ShortName()] = newControllerType
}
} else {
ERROR.Printf("Error, attempt to register duplicate controller as %s",controllerName)
}
TRACE.Printf("Registered controller: %s", controllerName)
return
}
// Method searches for a given exported method (case insensitive)
func (ct *ControllerType) Method(name string) *MethodType {
lowerName := strings.ToLower(name)
for _, method := range ct.Methods {
if method.lowerName == lowerName {
return method
}
}
return nil
}
// The controller name with the namespace
func (ct *ControllerType) Name() (string) {
return ct.Namespace + ct.ShortName()
}
// The controller name without the namespace
func (ct *ControllerType) ShortName() (string) {
return strings.ToLower(ct.Type.Name())
}
// RegisterController registers a Controller and its Methods with Revel.
func RegisterController(c interface{}, methods []*MethodType) {
// De-star the controller type
// (e.g. given TypeOf((*Application)(nil)), want TypeOf(Application))
elem := reflect.TypeOf(c).Elem()
// De-star all of the method arg types too.
for _, m := range methods {
m.lowerName = strings.ToLower(m.Name)
for _, arg := range m.Args {
arg.Type = arg.Type.Elem()
}
}
// Fetch module for controller, if none found controller must be part of the app
controllerModule := ModuleFromPath(elem.PkgPath(), true)
controllerType := AddControllerType(controllerModule,elem,methods)
TRACE.Printf("Registered controller: %s", controllerType.Name())
}