-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.go
62 lines (51 loc) · 1.1 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
package gapi
import (
"net/http"
"github.com/julienschmidt/httprouter"
)
type Context struct {
req *http.Request
resp http.ResponseWriter
chain []HandleFunc
next int
values map[string]string
params httprouter.Params
}
func (ctx *Context) Set(name string, value string) {
if ctx.values == nil {
ctx.values = map[string]string{}
}
ctx.values[name] = value
}
func (ctx *Context) Get(name string) (string, bool) {
v, ok := ctx.values[name]
return v, ok
}
func (ctx *Context) Request() *http.Request {
return ctx.req
}
func (ctx *Context) Response() http.ResponseWriter {
return ctx.resp
}
func (ctx *Context) SetResponse(r http.ResponseWriter) {
ctx.resp = r
}
func (ctx *Context) Params() httprouter.Params {
return ctx.params
}
func (ctx *Context) Next() error {
if ctx.next < len(ctx.chain) {
h := ctx.chain[ctx.next]
ctx.next++
return h(ctx)
}
return nil
}
func (ctx *Context) reset(w http.ResponseWriter, req *http.Request, params httprouter.Params, chain []HandleFunc) {
ctx.req = req
ctx.resp = w
ctx.chain = chain
ctx.next = 0
ctx.values = nil
ctx.params = params
}