forked from Odinman/ogo
/
rest_context.go
570 lines (493 loc) · 13.7 KB
/
rest_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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
package ogo
import (
"bytes"
"compress/flate"
"compress/gzip"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"github.com/zenazn/goji/web"
)
const (
//env key
RequestIDKey = "_reqid_"
SaveBodyKey = "_sb_"
SaveResultKey = "_sr_"
NoLogKey = "_nl_"
PaginationKey = "_pagination_"
FieldsKey = "_fields_"
TimeRangeKey = "_tr_"
OrderByKey = "_ob_"
ConditionsKey = "_conditions_"
LogPrefixKey = "_prefix_"
EndpointKey = "_endpoint_"
RowkeyKey = "_rk_"
SelectorKey = "_selector_"
MimeTypeKey = "_mimetype_"
DispositionMTKey = "_dmt_"
ContentMD5Key = "_md5_"
DispositionPrefix = "_dp_"
//1x1 gir
base64GifPixel = "R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs="
//base64GifPixel = "R0lGODlhAQABAJAAAP8AAAAAACH5BAUQAAAALAAAAAABAAEAAAICBAEAOw=="
)
var (
SUCCODE = map[string]int{
"get": http.StatusOK,
"delete": http.StatusNoContent,
"put": http.StatusCreated,
"post": http.StatusCreated,
"patch": http.StatusResetContent,
"head": http.StatusOK,
}
FAILCODE = map[string]int{ //定义正常出错
"get": http.StatusNotFound,
"delete": http.StatusNotAcceptable,
"put": http.StatusNotAcceptable,
"post": http.StatusNotAcceptable, //冲突
"patch": http.StatusNotAcceptable,
"head": http.StatusConflict,
}
EmptyGifBytes, _ = base64.StdEncoding.DecodeString(base64GifPixel)
)
// http context, 封装第三方包goji
type RESTContext struct {
web.C
Response http.ResponseWriter
Request *http.Request
Status int
ContentLength int
RequestBody []byte
Access *Access
Route *Route
}
type RESTError struct {
Massage string `json:"massage"`
Errors map[string]string `json:"errors"`
status int
}
// implement error interface
func (re *RESTError) Error() string { return re.Massage }
/* {{{ func newContext(c web.C, w http.ResponseWriter, r *http.Request) *RESTContext
*
*/
func newContext(c web.C, w http.ResponseWriter, r *http.Request) (*RESTContext, error) {
rc := &RESTContext{
C: c,
Response: w,
Request: r,
}
//request body
if r.Method != "GET" && r.Method != "HEAD" && r.Method != "DELETE" {
//rc.Trace("content-type: %s", r.Header.Get("Content-Type"))
if strings.Contains(r.Header.Get("Content-Type"), "multipart/") {
rc.Trace("parse multipart")
if err := r.ParseMultipartForm(env.MaxMemory); err != nil {
rc.Error("parse multipart form error: %s", err)
return rc, err
}
} else {
rc.RequestBody, _ = ioutil.ReadAll(r.Body)
defer r.Body.Close()
//ReadAll会清空r.Body, 下面需要写回去
//bf := bytes.NewBuffer(rc.RequestBody)
//r.Body = ioutil.NopCloser(bf)
}
}
return rc, nil
}
/* }}} */
/* {{{ func rcHolder(c web.C, w http.ResponseWriter, r *http.Request) (func(c web.C, w http.ResponseWriter, r *http.Request) *RESTContext)
* 利用闭包初始化RESTContext, 并防止某些关键字段被重写(RequestBody)
*/
func RCHolder(c web.C, w http.ResponseWriter, r *http.Request) (*RESTContext, func(c web.C, w http.ResponseWriter, r *http.Request) *RESTContext, error) {
//初始化, RequestBody之类的保持住
rc, err := newContext(c, w, r)
if err != nil {
return rc, nil, err
}
fn := func(c web.C, w http.ResponseWriter, r *http.Request) *RESTContext {
rc.C = c
rc.Response = w
rc.Request = r
return rc
}
return rc, fn, nil
}
/* }}} */
/* {{{ func (rc *RESTContext) NewRESTError(status int, msg interface{}) (re error)
*
*/
func (rc *RESTContext) NewRESTError(status int, msg interface{}) (re error) {
errors := make(map[string]string)
errors["method"] = rc.Request.Method
errors["path"] = rc.Request.URL.Path
errors["code"] = fmt.Sprint(status) // 备用, 可存储比httpstatus更详细的错误代码,目前只存httpstatus
var message string
if msg == nil {
message = http.StatusText(status)
} else {
message = fmt.Sprint(msg)
}
re = &RESTError{
Massage: message,
Errors: errors,
status: status,
}
return
}
/* }}} */
/* {{{ func (rc *RESTContext) HTTPError(status int) (err error)
*
*/
func (rc *RESTContext) HTTPError(status int) (err error) {
rc.RESTHeader(status)
// write data
err = rc.RESTBody(rc.NewRESTError(status, nil))
return
}
/* }}} */
/* {{{ func (rc *RESTContext) WriteBytes(data []byte) (n int, e error)
* 输出内容,如果需要压缩,统一在这里进行
*/
func (rc *RESTContext) WriteBytes(data []byte) (n int, e error) {
if dLen := len(data); dLen > 0 { //有内容才需要
if env.EnableGzip == true && rc.Request.Header.Get("Accept-Encoding") != "" {
splitted := strings.SplitN(rc.Request.Header.Get("Accept-Encoding"), ",", -1)
encodings := make([]string, len(splitted))
for i, val := range splitted {
encodings[i] = strings.TrimSpace(val)
}
for _, val := range encodings {
if val == "gzip" {
rc.Response.Header().Set("Content-Encoding", "gzip")
b := new(bytes.Buffer)
w, _ := gzip.NewWriterLevel(b, gzip.BestSpeed)
w.Write(data)
w.Close()
data = b.Bytes()
break
} else if val == "deflate" {
rc.Response.Header().Set("Content-Encoding", "deflate")
b := new(bytes.Buffer)
w, _ := flate.NewWriter(b, flate.BestSpeed)
w.Write(data)
w.Close()
data = b.Bytes()
break
}
}
}
rc.ContentLength = dLen
rc.Response.Header().Set("Content-Length", strconv.Itoa(rc.ContentLength))
}
if rc.Status == 0 {
rc.Status = http.StatusOK
}
//在Write之前要WriteHeader
rc.Response.WriteHeader(rc.Status)
if len(data) > 0 {
_, e = rc.Response.Write(data)
}
return
}
/* }}} */
/* {{{ func (rc *RESTContext) ServeBinary(mimetype string, data []byte)
* 直接出二进制内容
*/
func (rc *RESTContext) ServeBinary(mimetype string, data []byte) {
rc.Response.Header().Set("Content-Type", mimetype)
rc.WriteBytes(data)
}
/* }}} */
/* {{{ func (rc *RESTContext) RESTHeader(status int)
*
*/
func (rc *RESTContext) RESTHeader(status int) {
// Content-Type always json
rc.Response.Header().Set("Content-Type", "application/json; charset=UTF-8")
// status
//rc.Response.WriteHeader(status)
rc.Status = status
}
/* }}} */
/* {{{ func (rc *RESTContext) RESTBody(data interface{}) (err error)
*
*/
func (rc *RESTContext) RESTBody(data interface{}) (err error) {
var content []byte
if method := strings.ToLower(rc.Request.Method); method != "head" {
if data != nil {
if env.IndentJSON {
content, _ = json.MarshalIndent(data, "", " ")
} else {
content, _ = json.Marshal(data)
}
}
}
//write header & data
_, err = rc.WriteBytes(content)
return
}
/* }}} */
/* {{{ func (rc *RESTContext) RESTOK(data interface{}) (err error)
* 属于request的错误
*/
func (rc *RESTContext) RESTOK(data interface{}) (err error) {
var status int
method := strings.ToLower(rc.Request.Method)
if _, ok := SUCCODE[method]; !ok {
status = http.StatusOK //默认都是StatusOK
} else {
status = SUCCODE[method]
}
rc.RESTHeader(status)
if sr := rc.GetEnv(SaveResultKey); sr != nil && sr.(bool) == true {
rc.Access.App = data
}
// write data
err = rc.RESTBody(data)
return
}
/* }}} */
/* {{{ func (rc *RESTContext) HTTPOK(data []byte) (err error)
* 属于request的错误
*/
func (rc *RESTContext) HTTPOK(data []byte) (err error) {
rc.Response.Header().Set("Content-Type", "text/html; charset=UTF-8")
rc.Status = http.StatusOK
// write data
_, err = rc.WriteBytes(data)
return
}
/* }}} */
/* {{{ func (rc *RESTContext) HTTPEmptyGif() (err error)
* 属于request的错误
*/
func (rc *RESTContext) HTTPEmptyGif() (err error) {
rc.Response.Header().Set("Content-Type", "image/gif")
rc.Status = http.StatusOK
// write data
_, err = rc.WriteBytes(EmptyGifBytes)
return
}
/* }}} */
/* {{{ func (rc *RESTContext) HTTPBack() (err error)
* 属于request的错误
*/
func (rc *RESTContext) HTTPBack() (err error) {
rc.Status = http.StatusOK
rc.Response.Header().Set("Content-Type", "text/html; charset=UTF-8")
rc.Response.Header().Set("Cache-Control", "max-age=0")
rc.Response.Header().Set("Cache-Control", "no-cache")
rc.Response.Header().Set("Cache-Control", "must-revalidate")
rc.Response.Header().Set("Cache-Control", "private")
rc.Response.Header().Set("Expires", "Mon, 26 Jul 1997 05:00:00 GMT")
rc.Response.Header().Set("Pragma", "no-cache")
// write data
data := []byte(`<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Cache-Control" content="max-age=0" forua="true" />
<meta http-equiv="Cache-Control" content="no-cache" forua="true" />
<meta http-equiv="Cache-Control" content="must-revalidate" forua="true" />
<title></title>
</head>
<body><p><a href="javascript:history.back(1)">Back</a></p></body>
</html>`)
_, err = rc.WriteBytes(data)
return
}
/* }}} */
/* {{{ func (rc *RESTContext) HTTPRedirect(url string) (err error)
* 属于request的错误
*/
func (rc *RESTContext) HTTPRedirect(url string) (err error) {
rc.Status = http.StatusFound //302
rc.Response.Header().Set("Content-Type", "text/html; charset=UTF-8")
rc.Response.Header().Set("Cache-Control", "max-age=0")
rc.Response.Header().Set("Cache-Control", "no-cache")
rc.Response.Header().Set("Cache-Control", "must-revalidate")
rc.Response.Header().Set("Cache-Control", "private")
rc.Response.Header().Set("Expires", "Mon, 26 Jul 1997 05:00:00 GMT")
rc.Response.Header().Set("Pragma", "no-cache")
rc.Response.Header().Set("Location", url)
err = rc.RESTBody(nil)
return
}
/* }}} */
/* {{{ func (rc *RESTContext) RESTNotOK(msg interface{}) (err error)
* 属于request的错误
*/
func (rc *RESTContext) RESTNotOK(msg interface{}) (err error) {
var status int
method := strings.ToLower(rc.Request.Method)
if _, ok := FAILCODE[method]; !ok {
status = http.StatusBadRequest //默认都是StatusOK
} else {
status = FAILCODE[method]
}
rc.RESTHeader(status)
// write data
if msg != nil {
err = rc.RESTBody(rc.NewRESTError(status, msg))
} else {
err = rc.RESTBody(nil)
}
return
}
/* }}} */
/* {{{ func (rc *RESTContext) RESTGenericError(status int, msg interface{}) (err error)
* 普通错误,就是没有抓到error时报的错
*/
func (rc *RESTContext) RESTGenericError(status int, msg interface{}) (err error) {
rc.RESTHeader(status)
// write data
if msg != nil {
err = rc.RESTBody(rc.NewRESTError(status, msg))
} else {
err = rc.RESTBody(nil)
}
return
}
/* }}} */
/* {{{ func (rc *RESTContext) RESTNotFound(msg interface{}) (err error)
*
*/
func (rc *RESTContext) RESTNotFound(msg interface{}) (err error) {
return rc.RESTGenericError(http.StatusNotFound, msg)
}
/* }}} */
/* {{{ func (rc *RESTContext) RESTPanic(msg interface{}) (err error)
* 内部错误
*/
func (rc *RESTContext) RESTPanic(msg interface{}) (err error) {
return rc.RESTGenericError(http.StatusInternalServerError, msg)
}
/* }}} */
/* {{{ func (rc *RESTContext) RESTError(msg interface{}) (err error)
* 内部错误
*/
func (rc *RESTContext) RESTError(err error) error {
if re, ok := err.(*RESTError); ok {
// 标准错误,直接输出
rc.RESTHeader(re.status)
return rc.RESTBody(re)
} else {
//普通错误, 普通输入
rc.RESTNotOK(err)
}
return nil
}
/* }}} */
/* {{{ func (rc *RESTContext) RESTBadRequest(msg interface{}) (err error)
* BadRequest
*/
func (rc *RESTContext) RESTBadRequest(msg interface{}) (err error) {
return rc.RESTGenericError(http.StatusBadRequest, msg)
}
/* }}} */
/* {{{ func (rc *RESTContext) GetQueryParam(key string) (string, int)
*/
func (rc *RESTContext) GetQueryParam(key string) (r string, c int) {
v := rc.Request.Form[key]
c = len(v)
if c == 1 {
return string(v[0]), c
} else {
return string(strings.Join(v, ",")), c
}
}
/* }}} */
/* {{{ func (rc *RESTContext) SetEnv(k string, v interface{})
* 设置环境变量
*/
func (rc *RESTContext) SetEnv(k string, v interface{}) {
if k != "" {
rc.Env[k] = v
}
}
/* }}} */
/* {{{ func (rc *RESTContext) GetEnv(k string) (v interface{})
* 设置环境变量
*/
func (rc *RESTContext) GetEnv(k string) (v interface{}) {
var ok bool
if v, ok = rc.Env[k]; ok {
return v
}
return nil
}
/* }}} */
/* {{{ func (rc *RESTContext) SaveAccess()
* 设置环境变量
*/
func (rc *RESTContext) SaveAccess() {
if nl := rc.GetEnv(NoLogKey); nl == true {
return
}
if rc.Access != nil {
rc.Access.Save()
}
}
/* }}} */
/* {{{ RESTContext loggers
* 可以在每个debug信息带上session
*/
func (rc *RESTContext) Trace(format string, v ...interface{}) {
rc.logf("trace", format, v...)
}
func (rc *RESTContext) Debug(format string, v ...interface{}) {
rc.logf("debug", format, v...)
}
func (rc *RESTContext) Info(format string, v ...interface{}) {
rc.logf("info", format, v...)
}
func (rc *RESTContext) Print(format string, v ...interface{}) {
rc.logf("info", format, v...)
}
func (rc *RESTContext) Warn(format string, v ...interface{}) {
rc.logf("warn", format, v...)
}
func (rc *RESTContext) Error(format string, v ...interface{}) {
rc.logf("error", format, v...)
}
func (rc *RESTContext) Critical(format string, v ...interface{}) {
rc.logf("critical", format, v...)
}
func (rc *RESTContext) logf(tag, format string, v ...interface{}) {
if nl := rc.GetEnv(NoLogKey); nl == true {
// no logging
return
}
var prefix string
if p := rc.GetEnv(LogPrefixKey); p != nil {
prefix = p.(string)
}
if prefix != "" {
format = prefix + " " + format
}
switch strings.ToLower(tag) {
case "trace":
logger.Trace(format, v...)
case "debug":
logger.Debug(format, v...)
case "info":
logger.Info(format, v...)
case "warn":
logger.Warn(format, v...)
case "error":
logger.Error(format, v...)
case "critial":
logger.Critical(format, v...)
default:
logger.Debug(format, v...)
}
}
/* }}} */