-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactionResult.go
187 lines (151 loc) · 5.09 KB
/
actionResult.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
package trinity
import (
"errors"
"net/http"
"encoding/json"
)
// ActionResultInterface generates the http-response using a given mvc infrastructure,
// controller, action, and request and writes it to the given response writer
type ActionResultInterface interface {
Response(mvcI *MvcInfrastructure, c Controller, a Action, response http.ResponseWriter, request *http.Request)
}
// Error action result
type ErrorActionResult struct {
err interface{}
view bool // true, if the error occured during the error view generation.
// an internal state variable used to avoid infinite loops
}
// Generates an error result representing a specified error
func ErrorResult(err interface{}) ActionResultInterface {
logger.Trace("")
logger.Debugf("err: %v", err)
return &ErrorActionResult{err, false}
}
func viewErrorResult(err interface{}) ActionResultInterface {
logger.Trace("")
logger.Debugf("err: %v", err)
return &ErrorActionResult{err, true}
}
func (result *ErrorActionResult) Response(mvcI *MvcInfrastructure, c Controller, a Action, response http.ResponseWriter, request *http.Request) {
logger.Trace("")
response.WriteHeader(500)
if mvcI.internalErrorView == nil {
defaultInternalError(response, request, result.err)
return
}
if result.view && (c == mvcI.internalErrorView.C) && (a == mvcI.internalErrorView.A) {
defaultInternalError(response, request, result.err)
return
}
ShowView(mvcI.internalErrorView.C, mvcI.internalErrorView.A, result.err).
Response(mvcI, mvcI.internalErrorView.C, mvcI.internalErrorView.A, response, request)
}
// Resource-not-found action result
type NotFoundActionResult struct {
}
// Generates a resource-not-found action result
func NotFoundResult() ActionResultInterface {
logger.Trace("")
return &NotFoundActionResult{}
}
func (result *NotFoundActionResult) Response(mvcI *MvcInfrastructure, c Controller, a Action, response http.ResponseWriter, request *http.Request) {
logger.Trace("")
response.WriteHeader(404)
if mvcI.notFoundView == nil {
defaultNotFound(response, request)
return
}
ShowView(mvcI.notFoundView.C, mvcI.notFoundView.A, request.URL.String()).
Response(mvcI, mvcI.notFoundView.C, mvcI.notFoundView.A, response, request)
}
// Action result that performs a redirect to another controller/action
type RedirectToActionResult struct {
c Controller
a Action
params map[string]string
}
// Creates a redirect action result
func RedirectToAction(c Controller, a Action, params map[string]string) ActionResultInterface {
logger.Trace("")
//c = toLowerC(c)
//a = toLowerA(a)
logger.Debugf("c: %v, a: %v, p: %v", c, a, params)
return &RedirectToActionResult{c, a, params}
}
func (result *RedirectToActionResult) Response(mvcI *MvcInfrastructure, c Controller, a Action, response http.ResponseWriter, request *http.Request) {
logger.Trace("")
if len(result.c) > 0 {
c = result.c
}
if len(result.a) > 0 {
a = result.a
}
response.Header().Set("Location", createURL(c, a, result.params))
response.WriteHeader(302)
}
// ShowViewResult generates http-response using the view associated with the
// specified controller/action. Passes the specified vm as the view data.
type ShowViewResult struct {
c Controller
a Action
vm interface{}
}
// Creates a ShowViewResult using the specified controller, action, vm. See the description of
// ShowViewResult.
func ShowView(c Controller, a Action, vm interface{}) ActionResultInterface {
logger.Trace("")
//c = toLowerC(c)
//a = toLowerA(a)
logger.Debugf("c: %v, a: %v", c, a)
return &ShowViewResult{c, a, vm}
}
func (result *ShowViewResult) Response(mvcI *MvcInfrastructure, c Controller, a Action, response http.ResponseWriter, request *http.Request) {
logger.Tracef("View: res.c = %v, res.a = %v, c = %v, a = %v", result.c, result.a, c, a)
if len(result.c) > 0 {
c = result.c
}
if len(result.a) > 0 {
a = result.a
}
logger.Trace("get views")
actions, exists := mvcI.views[c]
if !exists {
logger.Errorf("Controller not found: %v", c)
viewErrorResult(errors.New("Controller not found")).Response(mvcI, c, a, response, request)
return
}
logger.Trace("get actions")
template, exists := actions[a]
if !exists {
logger.Errorf("Action not found: %v", a)
viewErrorResult(errors.New("Action not found")).Response(mvcI, c, a, response, request)
return
}
logger.Trace("render")
html, err := renderPage(result.vm, template)
if err != nil {
logger.Errorf("%v", err)
viewErrorResult(err).Response(mvcI, c, a, response, request)
return
}
response.Write(html)
}
// Action result that sends JSON-formatted content to the response.
type JsonActionResult struct {
Data interface{}
}
// Creates a json action result
func JsonResult(data interface{}) ActionResultInterface {
logger.Trace("")
return &JsonActionResult{data}
}
func (result *JsonActionResult) Response(mvcI *MvcInfrastructure, c Controller, a Action, response http.ResponseWriter, request *http.Request) {
logger.Trace("")
bytes, err := json.Marshal(result.Data)
if err != nil {
ErrorResult(err).Response(mvcI, c, a, response, request)
return
}
response.Header().Set("Content-Type", "application/json")
response.Write(bytes)
}