-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
80 lines (52 loc) · 993 Bytes
/
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
package http200ok
import (
"github.com/julienschmidt/httprouter"
"net/http"
"sync"
)
type Context struct {
Request *http.Request
Response http.ResponseWriter
WebSocket webSocket
mutex sync.Mutex
values map[string]interface{}
handlers []Handler
params httprouter.Params
index int
stop bool
}
func (c *Context) IsPost() bool {
return c.Request.Method == "POST"
}
func (c *Context) RequestParam(key string) string {
return c.params.ByName(key)
}
func (c *Context) Set(key string, value interface{}) {
c.mutex.Lock()
c.values[key] = value
c.mutex.Unlock()
}
func (c *Context) Get(key string) interface{} {
defer c.mutex.Unlock()
c.mutex.Lock()
if v, found := c.values[key]; found {
return v
}
return nil
}
func (c *Context) Next() {
c.index++
c.run()
}
func (c *Context) Stop() {
c.stop = true
}
func (c *Context) run() {
for c.index < len(c.handlers) {
c.handlers[c.index](c)
c.index++
if c.stop {
return
}
}
}