-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
142 lines (118 loc) · 2.73 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
package fastserver
import (
"bufio"
"bytes"
"encoding/json"
"github.com/valyala/fasthttp"
"net"
"net/http"
)
type Context struct {
FastCtx *fasthttp.RequestCtx
idx int
Path string
Method string
handlers []Handler
Params Params
RequestURI string
value interface{}
//values map[string]interface{}
}
func (c *Context) Run() {
for c.idx < len(c.handlers) {
hd := c.handlers[c.idx]
hd(c)
c.idx++
}
}
func (c *Context) Next() {
c.idx++
c.Run()
}
func (c *Context) reset() {
c.Params = c.Params[:0]
c.idx = 0
c.value = nil
}
func (c *Context) Abort() {
c.idx = len(c.handlers)
}
//clearFunc ,value should be clear
func (c *Context)SetUserValueObj(v interface{}){
c.value = v
}
func (c *Context)GetUserValueObj()interface{}{
return c.value
}
func (c *Context) AbortWithStatusJson(status int, o interface{}) error {
c.Abort()
//c.FastCtx.SetStatusCode(status)
js, err := json.Marshal(o)
if err != nil {
return err
}
c.AbortWithData(status, js)
return nil
}
func (c *Context) AbortWithData(status int, data []byte) {
c.Abort()
c.FastCtx.SetStatusCode(status)
c.FastCtx.Write(data)
}
func (c *Context) SetUserValue(key string, val interface{}) {
c.FastCtx.SetUserValue(key, val)
}
func (c *Context) GetUserValue(key string) interface{} {
return c.FastCtx.Value(key)
}
func (c *Context) Redirect(uri string, statusCode int) {
c.FastCtx.Redirect(uri, statusCode)
}
// 获取标准的http request
func (c *Context) StdHttpRequest() *http.Request {
fc := c.FastCtx
req, _ := http.NewRequest(c.Method, c.RequestURI, bytes.NewReader(c.FastCtx.PostBody()))
fc.Request.Header.VisitAll(func(key, value []byte) {
k := tostring(key)
v := tostring(value)
req.Header.Set(k, v)
})
return req
}
// 获取标准的 http responseWriter
func (c *Context) StdResponseWriter() http.ResponseWriter {
return newHttpRespW(c.FastCtx)
}
func (c *Context) SetResponseHeader(k, v string) {
c.FastCtx.Response.Header.Set(k, v)
}
func (c *Context) GetRequestHeader(k string) string {
return tostring(c.FastCtx.Request.Header.Peek(k))
}
type H map[string]interface{}
type httpRespW struct {
ctx *fasthttp.RequestCtx
header http.Header
}
func (h *httpRespW) Hijack() (net.Conn, *bufio.ReadWriter, error) {
h.ctx.Hijack(func(c net.Conn) {
})
conn := h.ctx.Conn()
return conn, bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn)), nil
}
func newHttpRespW(ctx *fasthttp.RequestCtx) http.ResponseWriter {
rw := &httpRespW{
ctx: ctx,
header: http.Header{},
}
return rw
}
func (h *httpRespW) Header() http.Header {
return h.header
}
func (h *httpRespW) Write(bytes []byte) (int, error) {
return h.ctx.Write(bytes)
}
func (h *httpRespW) WriteHeader(statusCode int) {
h.ctx.SetStatusCode(statusCode)
}