-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
587 lines (518 loc) · 12.5 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
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
package gow
import (
"encoding/json"
"encoding/xml"
"fmt"
"github.com/gkzy/gow/render"
"io"
"io/ioutil"
"math"
"mime/multipart"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"sync"
)
//Context gow context
type Context struct {
Writer ResponseWriter
responseWriter responseWriter
Req *http.Request
handlers HandlersChain
Keys map[string]interface{}
Path string
Method string
IP string //方便外部其他方法设置IP
Params Params
StatusCode int
// Data html template data map
Data map[interface{}]interface{}
mu sync.RWMutex
index int8
engine *Engine
fullPath string
}
const (
abortIndex = math.MaxInt8 / 2
)
func (c *Context) reset() {
c.Writer = &c.responseWriter
c.Params = c.Params[0:0]
c.handlers = nil
c.index = -1
c.fullPath = ""
c.Keys = nil
c.Data = nil
}
func (c *Context) Next() {
c.index++
for c.index < int8(len(c.handlers)) {
c.handlers[c.index](c)
c.index++
}
}
//HandlerName get handler name
func (c *Context) HandlerName() string {
return nameOfFunction(c.handlers.Last())
}
//Abort abort http response
func (c *Context) AbortCode(statusCode int) {
c.Status(statusCode)
c.Writer.WriteHeaderNow()
c.Abort()
}
// StopRun stop run
func (c *Context) StopRun() {
c.index = abortIndex
}
//Abort abort http response
func (c *Context) Abort() {
c.AbortCode(http.StatusNonAuthoritativeInfo)
}
//Fail fail http response
func (c *Context) Fail(statusCode int, err string) {
//TODO:日志记录
c.AbortCode(statusCode)
_, _ = c.Writer.Write([]byte(err))
}
// GetIP get client ip address
func (c *Context) GetIP() (ip string) {
addr := c.Req.RemoteAddr
str := strings.Split(addr, ":")
if len(str) > 1 {
ip = str[0]
}
c.IP = ip
return
}
//SetKey
func (c *Context) SetKey(key string, v interface{}) {
if c.Keys == nil {
c.Keys = make(map[string]interface{})
}
c.mu.Lock()
c.Keys[key] = v
c.mu.Unlock()
}
//GetKey
func (c *Context) GetKey(key string) interface{} {
var (
ok bool
v interface{}
)
c.mu.RLock()
if c.Keys != nil {
v, ok = c.Keys[key]
} else {
v, ok = nil, false
}
if !ok || v == nil {
//TODO: record logs:
//key %s does not exist
}
c.mu.RUnlock()
return v
}
// SetHeader set http response header
func (c *Context) SetHeader(key string, v string) {
if v == "" {
c.Writer.Header().Del(key)
}
c.Writer.Header().Set(key, v)
}
// GetHeader get http request header value by key
func (c *Context) GetHeader(key string) string {
return c.Req.Header.Get(key)
}
//Status set http status code
func (c *Context) Status(statusCode int) {
c.StatusCode = statusCode
c.Writer.WriteHeader(statusCode)
}
// WriteString response text
func (c *Context) ServerString(statusCode int, msg string) {
c.Writer.Header().Set("Content-Type", "text/plain;charset=utf-8")
c.Status(statusCode)
_, _ = c.Writer.Write([]byte(msg))
}
//String response text
func (c *Context) String(msg string) {
c.ServerString(http.StatusOK, msg)
}
// ServerJSON response json format
func (c *Context) ServerJSON(statusCode int, data interface{}) {
if statusCode < 0 {
statusCode = http.StatusOK
}
c.SetHeader("Content-Type", "application/json; charset=utf-8")
c.Status(statusCode)
encoder := json.NewEncoder(c.Writer)
if c.engine.RunMode == devMode {
encoder.SetIndent("", " ")
}
if err := encoder.Encode(data); err != nil {
c.Fail(http.StatusServiceUnavailable, err.Error())
}
}
// JSON response successful json format
func (c *Context) JSON(v interface{}) {
c.ServerJSON(http.StatusOK, v)
}
// ServerXML response xml
func (c *Context) ServerXML(statusCode int, data interface{}) {
if statusCode < 0 {
statusCode = http.StatusOK
}
c.SetHeader("Content-Type", "application/xml; charset=utf-8")
c.Status(statusCode)
encoder := xml.NewEncoder(c.Writer)
if err := encoder.Encode(data); err != nil {
c.Fail(http.StatusServiceUnavailable, err.Error())
}
}
//XML XML
func (c *Context) XML(data interface{}) {
c.ServerXML(http.StatusOK, data)
}
// ServerHTML ServerHTML
func (c *Context) ServerHTML(statusCode int, name string) {
//未设置 AutoRender时,不渲染模板
if !c.engine.AutoRender {
c.ServerString(404, string(default404Body))
return
}
c.Status(statusCode)
c.engine.HTMLRender = render.HTMLRender{}.Instance(c.engine.viewsPath, name, c.engine.FuncMap, c.engine.delims, c.engine.AutoRender, c.engine.RunMode, c.Data)
err := c.engine.HTMLRender.Render(c.Writer)
if err != nil {
c.Fail(http.StatusServiceUnavailable, err.Error())
}
}
// HTML
func (c *Context) HTML(name string) {
c.ServerHTML(http.StatusOK, name)
}
// DecodeJSONBody json decoder request.Body to v
func (c *Context) DecodeJSONBody(v interface{}) error {
decoder := json.NewDecoder(c.Req.Body)
if err := decoder.Decode(&v); err != nil {
return err
}
return nil
}
// RequestBody request body
func (c *Context) RequestBody() []byte {
if c.Req.Body == nil {
return []byte{}
}
var b []byte
b, _ = ioutil.ReadAll(c.Req.Body)
return b
}
// Param get Param by name
func (c *Context) Param(name string) string {
return c.Params.ByName(name)
}
// UserAgent get useragent
func (c *Context) UserAgent() string {
return c.GetHeader("User-Agent")
}
// Query Query
func (c *Context) Query(key string) string {
return c.Req.URL.Query().Get(key)
}
// Form
func (c *Context) Form(key string) string {
return c.Req.FormValue(key)
}
//input
func (c *Context) input() url.Values {
if c.Req.Form == nil {
c.Req.ParseForm()
}
return c.Req.Form
}
//formValue formValue
func (c *Context) formValue(key string) string {
if v := c.Form(key); v != "" {
return v
}
if c.Req.Form == nil {
c.Req.ParseForm()
}
return c.Req.Form.Get(key)
}
//GetString 按key返回字串值,可以设置default值
func (c *Context) GetString(key string, def ...string) string {
if v := c.formValue(key); v != "" {
return v
}
if len(def) > 0 {
return def[0]
}
return ""
}
//GetStrings GetStrings
func (c *Context) GetStrings(key string, def ...[]string) []string {
var defaultDef []string
if len(def) > 0 {
defaultDef = def[0]
}
if v := c.input(); v == nil {
return defaultDef
} else if kv := v[key]; len(kv) > 0 {
return kv
}
return defaultDef
}
//GetInt
func (c *Context) GetInt(key string, def ...int) (int, error) {
v := c.formValue(key)
if len(v) == 0 && len(def) > 0 {
return def[0], nil
}
return strconv.Atoi(v)
}
//GetInt8 GetInt8
// -128~127
func (c *Context) GetInt8(key string, def ...int8) (int8, error) {
v := c.formValue(key)
if len(v) == 0 && len(def) > 0 {
return def[0], nil
}
i64, err := strconv.ParseInt(v, 10, 8)
return int8(i64), err
}
//GetUint8 GetUint8
// 0~255
func (c *Context) GetUint8(key string, def ...uint8) (uint8, error) {
v := c.formValue(key)
if len(v) == 0 && len(def) > 0 {
return def[0], nil
}
i64, err := strconv.ParseUint(v, 10, 8)
return uint8(i64), err
}
//GetInt16 GetInt16
// -32768~32767
func (c *Context) GetInt16(key string, def ...int16) (int16, error) {
v := c.formValue(key)
if len(v) == 0 && len(def) > 0 {
return def[0], nil
}
i64, err := strconv.ParseInt(v, 10, 16)
return int16(i64), err
}
//GetUint8 GetUint8
// 0~65535
func (c *Context) GetUint16(key string, def ...uint16) (uint16, error) {
v := c.formValue(key)
if len(v) == 0 && len(def) > 0 {
return def[0], nil
}
i64, err := strconv.ParseUint(v, 10, 16)
return uint16(i64), err
}
//GetInt32 GetInt32
// -2147483648~2147483647
func (c *Context) GetInt32(key string, def ...int32) (int32, error) {
v := c.formValue(key)
if len(v) == 0 && len(def) > 0 {
return def[0], nil
}
i64, err := strconv.ParseInt(v, 10, 32)
return int32(i64), err
}
//GetUint32 GetUint32
// 0~4294967295
func (c *Context) GetUint32(key string, def ...uint32) (uint32, error) {
v := c.formValue(key)
if len(v) == 0 && len(def) > 0 {
return def[0], nil
}
i64, err := strconv.ParseUint(v, 10, 32)
return uint32(i64), err
}
//GetInt64 GetInt64
// -9223372036854775808~9223372036854775807
func (c *Context) GetInt64(key string, def ...int64) (int64, error) {
v := c.formValue(key)
if len(v) == 0 && len(def) > 0 {
return def[0], nil
}
return strconv.ParseInt(v, 10, 64)
}
//GetUint64 GetUint64
// 0~18446744073709551615
func (c *Context) GetUint64(key string, def ...uint64) (uint64, error) {
v := c.formValue(key)
if len(v) == 0 && len(def) > 0 {
return def[0], nil
}
i64, err := strconv.ParseUint(v, 10, 64)
return uint64(i64), err
}
//GetInt64 GetInt64
func (c *Context) GetFloat64(key string, def ...float64) (float64, error) {
v := c.formValue(key)
if len(v) == 0 && len(def) > 0 {
return def[0], nil
}
return strconv.ParseFloat(v, 64)
}
//GetBool GetBool
func (c *Context) GetBool(key string, def ...bool) (bool, error) {
v := c.formValue(key)
if len(v) == 0 && len(def) > 0 {
return def[0], nil
}
return strconv.ParseBool(v)
}
// Redirect http redirect
// like : 301 302 ...
func (c *Context) Redirect(statusCode int, url string) {
c.Status(statusCode)
http.Redirect(c.Writer, c.Req, url, statusCode)
}
//Download data to download
func (c *Context) Download(data []byte) {
c.SetHeader("Content-Type", "application/octet-stream; charset=utf-8")
c.Status(200)
c.Writer.Write(data)
}
// GetCookie get request cookie
// c.GetCookie("url")
func (c *Context) GetCookie(key string) string {
ck, err := c.Req.Cookie(key)
if err != nil {
return ""
}
val, _ := url.QueryUnescape(ck.Value)
return val
}
// SetCookie set cookie
// c.SetCookie("url","https://gow.22v.net",72*time.Hour,"",true,true)
func (c *Context) SetCookie(key, value string, maxAge int, path, domain string, secure, httpOnly bool) {
if path == "" {
path = "/"
}
http.SetCookie(c.Writer, &http.Cookie{
Name: key,
Value: url.QueryEscape(value),
MaxAge: maxAge,
Path: path,
Domain: domain,
SameSite: http.SameSiteDefaultMode,
Secure: secure,
HttpOnly: httpOnly,
})
}
//File read file to http body stream
func (c *Context) File(filePath string) {
c.Status(http.StatusOK)
http.ServeFile(c.Writer, c.Req, filePath)
}
//GetFile get single from form
func (c *Context) GetFile(key string) (multipart.File, *multipart.FileHeader, error) {
if c.Req.MultipartForm == nil {
if err := c.Req.ParseMultipartForm(c.engine.MaxMultipartMemory); err != nil {
return nil, nil, err
}
}
return c.Req.FormFile(key)
}
//GetFiles get []file from form
func (c *Context) GetFiles(key string) ([]*multipart.FileHeader, error) {
if files, ok := c.Req.MultipartForm.File[key]; ok {
return files, nil
}
return nil, http.ErrMissingFile
}
// SaveToFile saves uploaded file to new path.
// Upload the file and save it on the server
// c.SaveToFile("file","./upload/1.jpg")
func (c *Context) SaveToFile(fromFile, toFile string) error {
file, _, err := c.Req.FormFile(fromFile)
if err != nil {
return err
}
defer file.Close()
f, err := os.OpenFile(toFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer f.Close()
io.Copy(f, file)
return nil
}
// URL get request url
func (c *Context) URL() string {
return c.Req.URL.String()
}
// Referer get request referer
func (c *Context) Referer() string {
return c.Req.Referer()
}
// IsAjax return is ajax request
func (c *Context) IsAjax() bool {
return c.GetHeader("X-Requested-With") == "XMLHttpRequest"
}
// IsWebsocket return is websocket request
func (c *Context) IsWebsocket() bool {
if strings.Contains(strings.ToLower(c.GetHeader("Connection")), "upgrade") &&
strings.EqualFold(c.GetHeader("Upgrade"), "websocket") {
return true
}
return false
}
//================== memory session ======================
// SetSession
func (c *Context) SetSession(key string, v interface{}) {
setSession(key, v)
}
// GetSession return interface{}
func (c *Context) GetSession(key string) interface{} {
return getSession(key)
}
//GetSessionString GetSessionString
// return string
func (c *Context) GetSessionString(key string) string {
ret := c.GetSession(key)
v, ok := ret.(string)
if ok {
return v
}
return ""
}
// GetSessionInt return int
// default 0
func (c *Context) GetSessionInt(key string) int {
v := c.GetSessionInt64(key)
return int(v)
}
// GetSessionInt64 return int64
// default 0
func (c *Context) GetSessionInt64(key string) int64 {
ret := c.GetSession(key)
v, err := strconv.ParseInt(fmt.Sprintf("%v", ret), 10, 64)
if err != nil {
return 0
}
return v
}
// GetSessionBool return bool
// default false
func (c *Context) GetSessionBool(key string) bool {
ret := c.GetSession(key)
v, ok := ret.(bool)
if ok {
return v
}
return false
}
// DeleteSession delete session key
func (c *Context) DeleteSession(key string) {
deleteSession(key)
}