-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresponse.go
256 lines (230 loc) · 4.98 KB
/
response.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
package fresh
import (
"encoding/json"
"encoding/xml"
"fmt"
"net/http"
"os"
)
type (
Response interface {
write()
get() reply
writeErr(error)
set(int, []byte)
Code(int) error
Type(content string)
Raw(int, string) error
Error(int, error) error
HTML(int, string) error
File(int, string) error
Get() http.ResponseWriter
Download(int, string) error
XML(int, interface{}) error
Text(int, interface{}) error
JSON(int, interface{}) error
JSONP(int, string, interface{}) error
XMLFormat(int, interface{}, string) error
JSONFormat(int, interface{}, string) error
JSONPFormat(int, string, interface{}, string) error
}
reply struct {
code int
response []byte
}
response struct {
*context
w http.ResponseWriter
r *http.Request
reply reply
}
)
// Return writer
func (r *response) write() {
r.w.WriteHeader(r.reply.code)
r.w.Write(r.reply.response)
}
// Get response values
func (r *response) get() reply {
return r.reply
}
// WriteErr response http error
func (r *response) writeErr(err error) {
if r.reply.code == 0 {
r.reply.code = http.StatusInternalServerError
}
http.Error(r.w, err.Error(), r.reply.code)
}
// Check content type
func (r *response) check(content string) {
if r.w.Header().Get(ContentType) != content {
r.w.Header().Set(ContentType, content)
}
}
// Set response values
func (r *response) set(code int, response []byte) {
r.reply = reply{code, response}
}
// Http code response
func (r *response) Code(c int) error {
r.set(c, nil)
return nil
}
// Type set content type
func (r *response) Type(content string) {
r.w.Header().Set(ContentType, content)
}
// Http code response
func (r *response) Get() http.ResponseWriter {
return r.w
}
// Raw response
func (r *response) Raw(c int, i string) error {
r.set(c, []byte(i))
return nil
}
// HTML response
func (r *response) HTML(c int, i string) error {
r.check(MIMETextHTML)
r.set(c, []byte(i))
return nil
}
// HTTP error
func (r *response) Error(c int, err error) error {
r.set(c, nil)
return err
}
// File response, may be used to display a file
func (r *response) File(c int, path string) error {
// check if exist
if _, err := os.Stat(path); err != nil {
r.set(http.StatusNotFound, nil)
return err
}
// check if is a dir or not
fi, err := os.Stat(path)
if err != nil || fi.IsDir() {
r.set(http.StatusNotFound, nil)
return err
}
f, err := os.Open(path)
defer f.Close()
if err != nil {
return err
}
http.ServeContent(r.w, r.r, fi.Name(), fi.ModTime(), f)
return nil
}
// XML response
func (r *response) XML(c int, i interface{}) error {
r.check(MIMEAppXML)
d, err := xml.Marshal(i)
if err != nil {
r.set(c, nil)
return err
}
r.set(c, d)
return nil
}
// JSON response
func (r *response) JSON(c int, i interface{}) error {
r.check(MIMEAppJSON)
d, err := json.Marshal(i)
if err != nil {
r.set(c, nil)
return err
}
r.set(c, d)
return nil
}
// Text response
func (r *response) Text(c int, i interface{}) error {
r.check(MIMEText)
d, err := xml.Marshal(i)
if err != nil {
r.set(c, nil)
return err
}
r.set(c, d)
return nil
}
// Custom Header
func (r *response) Header(key, value string) Response {
r.w.Header().Add(key, value)
return r
}
// Download response, force the browser to download a file
func (r *response) Download(c int, path string) error {
// check if exist
if _, err := os.Stat(path); err != nil {
r.set(http.StatusNotFound, nil)
return err
}
// check if is a dir or not
fi, err := os.Stat(path)
if err != nil || fi.IsDir() {
r.set(http.StatusNotFound, nil)
return err
}
f, err := os.Open(path)
defer f.Close()
if err != nil {
return err
}
r.w.Header().Set("Content-Disposition", "attachment; filename="+fi.Name())
http.ServeContent(r.w, r.r, fi.Name(), fi.ModTime(), f)
return nil
}
// Redirect
func (r *response) Redirect(c int, link string) error {
if c < http.StatusMultipleChoices || c > http.StatusTemporaryRedirect {
return nil
}
r.w.Header().Set(Location, link)
r.w.WriteHeader(c)
return nil
}
// JSON response
func (r *response) JSONP(c int, callback string, i interface{}) error {
r.check(MIMEAppJS)
d, err := json.Marshal(i)
if err != nil {
r.set(c, nil)
return err
}
r.set(c, []byte(fmt.Sprintf("%s(%s)", callback, d)))
return nil
}
// XML pretty response
func (r *response) XMLFormat(c int, i interface{}, indent string) error {
r.check(MIMEAppXML)
d, err := xml.MarshalIndent(i, "", indent)
if err != nil {
r.set(c, nil)
return err
}
r.set(c, d)
return nil
}
// JSON pretty response
func (r *response) JSONFormat(c int, i interface{}, indent string) error {
r.check(MIMEAppJSON)
d, err := json.MarshalIndent(i, "", indent)
if err != nil {
r.set(c, nil)
return err
}
r.set(c, d)
return nil
}
// JSON pretty response
func (r *response) JSONPFormat(c int, callback string, i interface{}, indent string) error {
r.check(MIMEAppJS)
d, err := json.MarshalIndent(i, "", indent)
if err != nil {
r.set(c, nil)
return err
}
r.set(c, []byte(fmt.Sprintf("%s(%s)", callback, d)))
return nil
}