forked from ShiningRush/droplet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
103 lines (82 loc) · 1.79 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
package droplet
import "context"
type Context interface {
Context() context.Context
SetContext(context.Context)
Get(key string) interface{}
GetString(key string) string
Set(key string, value interface{})
SetInput(interface{})
Input() interface{}
SetOutput(interface{})
Output() interface{}
SetPath(path string)
Path() string
// Rewritten indicates whether the request path has been rewritten.
Rewritten() bool
// SetRewrite sets the rewrite flag.
SetRewrite(rewritten bool)
}
type emptyContext struct {
cxt context.Context
dict map[string]interface{}
input interface{}
output interface{}
path string
rewritten bool
}
func NewContext() *emptyContext {
c := &emptyContext{}
c.dict = make(map[string]interface{})
c.cxt = context.TODO()
return c
}
func (c *emptyContext) Context() context.Context {
return c.cxt
}
func (c *emptyContext) SetContext(cxt context.Context) {
c.cxt = cxt
}
func (c *emptyContext) Set(key string, value interface{}) {
c.dict[key] = value
}
func (c *emptyContext) Get(key string) interface{} {
rs, ok := c.dict[key]
if !ok {
return nil
}
return rs
}
func (c *emptyContext) GetString(key string) string {
rs, ok := c.dict[key]
s := ""
s, ok = rs.(string)
if !ok {
return ""
}
return s
}
func (c *emptyContext) SetInput(input interface{}) {
c.input = input
}
func (c *emptyContext) Input() interface{} {
return c.input
}
func (c *emptyContext) SetOutput(output interface{}) {
c.output = output
}
func (c *emptyContext) Output() interface{} {
return c.output
}
func (c *emptyContext) SetPath(path string) {
c.path = path
}
func (c *emptyContext) Path() string {
return c.path
}
func (c *emptyContext) SetRewrite(rewritten bool) {
c.rewritten = rewritten
}
func (c *emptyContext) Rewritten() bool {
return c.rewritten
}