-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
50 lines (43 loc) · 901 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
package router
import (
"io"
"net/http"
)
// RequestContext is...
type RequestContext struct {
Env map[interface{}]interface{}
Params []Param
req *http.Request
}
type iogoContextReadCloser struct {
io.ReadCloser
context *RequestContext
}
func SetContext(req *http.Request) *RequestContext {
c, ok := req.Body.(iogoContextReadCloser)
if !ok {
c = iogoContextReadCloser{
ReadCloser: req.Body,
context: &RequestContext{Env: make(map[interface{}]interface{})},
}
req.Body = c
}
return c.context
}
// Context is...
func Context(req *http.Request) *RequestContext {
return req.Body.(iogoContextReadCloser).context
}
// Param is...
func (c *RequestContext) Param(name string) string {
for _, e := range c.Params {
if e.Name == name {
return e.Value
}
}
return ""
}
// SetParams is...
func (c *RequestContext) setParams(params []Param) {
c.Params = params
}