-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontext.go
69 lines (59 loc) · 1.38 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
// Copyright 2017 King Qiu. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
// https://github.com/qjw/kelly
package kelly
import (
"bytes"
"io/ioutil"
"net/http"
)
type Context struct {
http.ResponseWriter
http.Hijacker
http.Flusher
r *http.Request
// 下一个处理逻辑,用于middleware
next http.HandlerFunc
// 用于支持设置context数据
dataContext
// render
renderOp
// request
request
// binder
binder
}
func (c *Context) Request() *http.Request {
return c.r
}
func (c *Context) SetBody(body []byte) {
c.r.Body.Close()
c.r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
}
// 重置request,用于支持context库补充额外value
func (c *Context) SetRequest(r *http.Request) {
c.r = r
// 无须更新dataContext,因为后者不依赖于context库,而是map实现
}
func (c *Context) InvokeNext() {
if c.next != nil {
c.next.ServeHTTP(c, c.Request())
} else {
panic("invalid invoke next")
}
}
func newContext(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) *Context {
c := &Context{
ResponseWriter: w,
Hijacker: w.(http.Hijacker),
Flusher: w.(http.Flusher),
r: r,
next: next,
dataContext: newMapHttpContext(r),
request: newRequest(r),
}
c.renderOp = newRender(c)
c.binder = newBinder(c)
return c
}