This repository was archived by the owner on Feb 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathreturn.go
251 lines (226 loc) · 5.17 KB
/
return.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
// Copyright 2015 The Tango Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tango
import (
"encoding/json"
"encoding/xml"
"net/http"
"reflect"
)
// StatusResult describes http response
type StatusResult struct {
Code int
Result interface{}
}
// enumerate all the return response types
const (
autoResponse = iota
jsonResponse
xmlResponse
)
// ResponseTyper describes reponse type
type ResponseTyper interface {
ResponseType() int
}
// Json describes return JSON type
// Deprecated: use JSON instead
type Json struct{}
// ResponseType implementes ResponseTyper
func (Json) ResponseType() int {
return jsonResponse
}
// JSON describes return JSON type
type JSON struct{}
// ResponseType implementes ResponseTyper
func (JSON) ResponseType() int {
return jsonResponse
}
// Xml descirbes return XML type
// Deprecated: use XML instead
type Xml struct{}
// ResponseType implementes ResponseTyper
func (Xml) ResponseType() int {
return xmlResponse
}
// XML descirbes return XML type
type XML struct{}
// ResponseType implementes ResponseTyper
func (XML) ResponseType() int {
return xmlResponse
}
func isNil(a interface{}) bool {
if a == nil {
return true
}
aa := reflect.ValueOf(a)
return !aa.IsValid() || (aa.Type().Kind() == reflect.Ptr && aa.IsNil())
}
// XMLError describes return xml error
type XMLError struct {
XMLName xml.Name `xml:"err"`
Content string `xml:"content"`
}
// XMLString describes return xml string
type XMLString struct {
XMLName xml.Name `xml:"string"`
Content string `xml:"content"`
}
// Return returns a tango middleware to handler return values
func Return() HandlerFunc {
return func(ctx *Context) {
var rt int
action := ctx.Action()
if action != nil {
if i, ok := action.(ResponseTyper); ok {
rt = i.ResponseType()
}
}
ctx.Next()
// if no route match or has been write, then return
if action == nil || ctx.Written() {
return
}
// if there is no return value or return nil
if isNil(ctx.Result) {
// then we return blank page
ctx.Result = ""
}
var result = ctx.Result
var statusCode = 0
if res, ok := ctx.Result.(*StatusResult); ok {
statusCode = res.Code
result = res.Result
}
if rt == jsonResponse {
encoder := json.NewEncoder(ctx)
if len(ctx.Header().Get("Content-Type")) <= 0 {
ctx.Header().Set("Content-Type", "application/json; charset=UTF-8")
}
switch res := result.(type) {
case AbortError:
if statusCode == 0 {
statusCode = res.Code()
}
ctx.WriteHeader(statusCode)
encoder.Encode(map[string]string{
"err": res.Error(),
})
case ErrorWithCode:
if statusCode == 0 {
statusCode = http.StatusOK
}
ctx.WriteHeader(statusCode)
encoder.Encode(map[string]interface{}{
"err": res.Error(),
"err_code": res.ErrorCode(),
})
case error:
if statusCode == 0 {
statusCode = http.StatusOK
}
ctx.WriteHeader(statusCode)
encoder.Encode(map[string]string{
"err": res.Error(),
})
case string:
if statusCode == 0 {
statusCode = http.StatusOK
}
ctx.WriteHeader(statusCode)
encoder.Encode(map[string]string{
"content": res,
})
case []byte:
if statusCode == 0 {
statusCode = http.StatusOK
}
ctx.WriteHeader(statusCode)
encoder.Encode(map[string]string{
"content": string(res),
})
default:
if statusCode == 0 {
statusCode = http.StatusOK
}
ctx.WriteHeader(statusCode)
err := encoder.Encode(result)
if err != nil {
ctx.Result = err
encoder.Encode(map[string]string{
"err": err.Error(),
})
}
}
return
} else if rt == xmlResponse {
encoder := xml.NewEncoder(ctx)
if len(ctx.Header().Get("Content-Type")) <= 0 {
ctx.Header().Set("Content-Type", "application/xml; charset=UTF-8")
}
switch res := result.(type) {
case AbortError:
if statusCode == 0 {
statusCode = res.Code()
}
ctx.WriteHeader(statusCode)
encoder.Encode(XMLError{
Content: res.Error(),
})
case error:
if statusCode == 0 {
statusCode = http.StatusOK
}
ctx.WriteHeader(statusCode)
encoder.Encode(XMLError{
Content: res.Error(),
})
case string:
if statusCode == 0 {
statusCode = http.StatusOK
}
ctx.WriteHeader(statusCode)
encoder.Encode(XMLString{
Content: res,
})
case []byte:
if statusCode == 0 {
statusCode = http.StatusOK
}
ctx.WriteHeader(statusCode)
encoder.Encode(XMLString{
Content: string(res),
})
default:
if statusCode == 0 {
statusCode = http.StatusOK
}
ctx.WriteHeader(statusCode)
err := encoder.Encode(result)
if err != nil {
ctx.Result = err
encoder.Encode(XMLError{
Content: err.Error(),
})
}
}
return
}
switch res := result.(type) {
case AbortError, error:
ctx.HandleError()
case []byte:
if statusCode == 0 {
statusCode = http.StatusOK
}
ctx.WriteHeader(statusCode)
ctx.Write(res)
case string:
if statusCode == 0 {
statusCode = http.StatusOK
}
ctx.WriteHeader(statusCode)
ctx.WriteString(res)
}
}
}