-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
269 lines (215 loc) · 5.46 KB
/
context.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
package https
import (
"bytes"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"github.com/gorilla/schema"
"html"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
)
type Context struct {
W http.ResponseWriter
R *http.Request
MaxMem int64 //upload file memory size
}
func NewContext(w http.ResponseWriter, r *http.Request) *Context {
return &Context{W: w, R: r, MaxMem: 0x4000000}
}
func (p *Context) NotFound() {
http.NotFound(p.W, p.R)
}
//code should be status code in http package, example: http.StatusContinue
func (p *Context) Error(code int) {
msg := fmt.Sprintf("%d %s", code, http.StatusText(code))
http.Error(p.W, msg, code)
}
func (p *Context) ServeFile(name string) {
http.ServeFile(p.W, p.R, name)
}
func (p *Context) FormValue(name string) string {
return p.R.FormValue(name)
}
func (p *Context) Query() url.Values {
return p.R.URL.Query()
}
func (p *Context) Form() url.Values {
p.R.ParseForm()
return p.R.Form
}
func (p *Context) PostForm() url.Values {
p.R.ParseForm()
return p.R.PostForm
}
// ReadForm binds the formObject with the form data
// it supports any kind of type, including custom structs.
// It will return nothing if request data are empty.
// The struct field tag is "form".
//
func (p *Context) ReadForm(data interface{}) error {
values := p.Form()
if len(values) == 0 {
return nil
}
return decoderForm.Decode(data, values)
}
// ReadQuery binds the "ptr" with the url query string. The struct field tag is "url".
func (p *Context) ReadQuery(data interface{}) error {
values := p.Query()
if len(values) == 0 {
return nil
}
return decoderQuery.Decode(data, values)
}
//add header to the response.
func (p *Context) AddHeader(name, value string) {
p.W.Header().Add(name, value)
}
//set header
func (p *Context) SetHeader(name, value string) {
p.W.Header().Set(name, value)
}
//del response header
func (p *Context) DelHeader(name string) {
p.W.Header().Del(name)
}
//h may be nil
func (p *Context) WriteHeader(statusCode int, h http.Header) {
if h != nil {
hto := p.W.Header()
for k, v := range h {
hto[k] = v
}
}
p.W.WriteHeader(statusCode)
}
func (p *Context) Write(data []byte) (n int, err error) {
return p.W.Write(data)
}
func (p *Context) WriteString(str string) error {
_, err := p.W.Write([]byte(html.EscapeString(str)))
return err
}
func (p *Context) WriteDataJSON(data []byte) error {
var buf bytes.Buffer
json.HTMLEscape(&buf, data)
p.SetHeader(headerTypeContentType, headerTypeContentJSON)
_, err := buf.WriteTo(p.W)
return err
}
func (p *Context) WriteJSON(v interface{}) error {
data, err := json.Marshal(v)
if err == nil {
err = p.WriteDataJSON(data)
}
return err
}
func (p *Context) WriteDataXML(data []byte) error {
p.SetHeader(headerTypeContentType, headerTypeContentXML)
_, err := p.W.Write(data)
return err
}
func (p *Context) WriteXML(v interface{}) error {
data, err := xml.Marshal(v)
if err == nil {
err = p.WriteDataXML(data)
}
return err
}
func (p *Context) WriteHTML(data string) error {
p.SetHeader(headerTypeContentType, headerTypeContentHTML)
return p.WriteString(data)
}
func (p *Context) WriteText(data string) error {
p.SetHeader(headerTypeContentType, headerTypeContentText)
_, err := p.Write([]byte(data))
return err
}
//read header from request
func (p *Context) GetHeader(name string) string {
return p.R.Header.Get(name)
}
func (p *Context) GetBody() ([]byte, error) {
if p.R.Body == nil {
return nil, errEmptyBody
} else {
return ioutil.ReadAll(p.R.Body)
}
}
func (p *Context) UnmarshalBody(v interface{}, unmarshaler UnmarshalerFunc) error {
data, err := p.GetBody()
if err == nil {
err = unmarshaler(data, v)
}
return err
}
func (p *Context) ReadJSON(v interface{}) error {
return p.UnmarshalBody(v, UnmarshalerFunc(json.Unmarshal))
}
func (p *Context) ReadXML(v interface{}) error {
return p.UnmarshalBody(v, UnmarshalerFunc(xml.Unmarshal))
}
func (p *Context) ReadHTML() (string, error) {
var str string
data, err := p.GetBody()
if err == nil {
str = html.UnescapeString(string(data))
}
return str, err
}
func (p *Context) ReadText() (string, error) {
var str string
data, err := p.GetBody()
if err == nil {
str = string(data)
}
return str, err
}
func (p *Context) FormFile(key string) (multipart.File, *multipart.FileHeader, error) {
if err := p.R.ParseMultipartForm(p.MaxMem); err != nil {
return nil, nil, err
}
return p.R.FormFile(key)
}
func (p *Context) UploadFile(key string, createFile func(string) io.WriteCloser) error {
file, info, err := p.FormFile(key)
if err == nil {
defer file.Close()
if out := createFile(info.Filename); out != nil {
defer out.Close()
_, err = io.Copy(out, file)
} else {
err = errCreateFile
}
}
return err
}
func init() {
decoderForm = schema.NewDecoder()
decoderQuery = schema.NewDecoder()
decoderForm.SetAliasTag("form")
decoderQuery.SetAliasTag("url")
}
type (
UnmarshalerFunc func(data []byte, v interface{}) error
)
var (
errUnknownDataType = errors.New("Unknown data type")
errDataType = errors.New("Error data type")
errEmptyBody = errors.New("Empty body")
errCreateFile = errors.New("Create file failed")
decoderForm *schema.Decoder
decoderQuery *schema.Decoder
)
const (
headerTypeContentType = "Content-Type"
headerTypeContentJSON = "application/json;charset=utf-8"
headerTypeContentXML = "text/xml;charset=utf-8"
headerTypeContentHTML = "text/html;charset=utf-8"
headerTypeContentText = "text/plain;charset=utf-8"
)