-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathController.go
121 lines (101 loc) · 3.23 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
package pgo
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"reflect"
"github.com/pinguo/pgo/Util"
)
// Controller the base class of web and cmd controller
type Controller struct {
Object
}
// GetBindInfo get action map as extra binding info
func (c *Controller) GetBindInfo(v interface{}) interface{} {
if _, ok := v.(IController); !ok {
panic("param require a controller")
}
rt := reflect.ValueOf(v).Type()
num := rt.NumMethod()
actions := make(map[string]int)
for i := 0; i < num; i++ {
name := rt.Method(i).Name
if len(name) > ActionLength && name[:ActionLength] == ActionPrefix {
actions[name[ActionLength:]] = i
}
}
return actions
}
// BeforeAction before action hook
func (c *Controller) BeforeAction(action string) {
}
// AfterAction after action hook
func (c *Controller) AfterAction(action string) {
}
// HandlePanic process unhandled action panic
func (c *Controller) HandlePanic(v interface{}) {
status := http.StatusInternalServerError
switch e := v.(type) {
case *Exception:
status = e.GetStatus()
c.OutputJson(EmptyObject, status, e.GetMessage())
default:
c.OutputJson(EmptyObject, status)
}
c.GetContext().Error("%s, trace[%s]", Util.ToString(v), Util.PanicTrace(TraceMaxDepth, false))
}
// Redirect output redirect response
func (c *Controller) Redirect(location string, permanent bool) {
ctx := c.GetContext()
ctx.SetHeader("Location", location)
if permanent {
ctx.End(http.StatusMovedPermanently, nil)
} else {
ctx.End(http.StatusFound, nil)
}
}
// OutputJson output json response
func (c *Controller) OutputJson(data interface{}, status int, msg ...string) {
ctx := c.GetContext()
message := App.GetStatus().GetText(status, ctx, msg...)
output, e := json.Marshal(map[string]interface{}{
"status": status,
"message": message,
"data": data,
})
if e != nil {
panic(fmt.Sprintf("failed to marshal json, %s", e))
}
ctx.PushLog("status", status)
ctx.SetHeader("Content-Type", "application/json; charset=utf-8")
ctx.End(http.StatusOK, output)
}
// OutputJsonp output jsonp response
func (c *Controller) OutputJsonp(callback string, data interface{}, status int, msg ...string) {
ctx := c.GetContext()
message := App.GetStatus().GetText(status, ctx, msg...)
output, e := json.Marshal(map[string]interface{}{
"status": status,
"message": message,
"data": data,
})
if e != nil {
panic(fmt.Sprintf("failed to marshal json, %s", e))
}
buf := &bytes.Buffer{}
buf.WriteString(callback + "(")
buf.Write(output)
buf.WriteString(")")
ctx.PushLog("status", status)
ctx.SetHeader("Content-Type", "text/javascript; charset=utf-8")
ctx.End(http.StatusOK, buf.Bytes())
}
// OutputView output rendered view
func (c *Controller) OutputView(view string, data interface{}, contentType ...string) {
ctx := c.GetContext()
contentType = append(contentType, "text/html; charset=utf-8")
ctx.PushLog("status", http.StatusOK)
ctx.SetHeader("Content-Type", contentType[0])
ctx.End(http.StatusOK, App.GetView().Render(view, data))
}