forked from ant0ine/go-json-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.go
42 lines (37 loc) · 903 Bytes
/
env.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
package rest
import (
"net/http"
"sync"
)
// inpired by https://groups.google.com/forum/#!msg/golang-nuts/teSBtPvv1GQ/U12qA9N51uIJ
type env struct {
envLock sync.Mutex
envMap map[*http.Request]map[string]interface{}
}
func (self *env) setVar(r *http.Request, key string, value interface{}) {
self.envLock.Lock()
defer self.envLock.Unlock()
if self.envMap == nil {
self.envMap = make(map[*http.Request]map[string]interface{})
}
if self.envMap[r] == nil {
self.envMap[r] = make(map[string]interface{})
}
self.envMap[r][key] = value
}
func (self *env) getVar(r *http.Request, key string) interface{} {
self.envLock.Lock()
defer self.envLock.Unlock()
if self.envMap == nil {
return nil
}
if self.envMap[r] == nil {
return nil
}
return self.envMap[r][key]
}
func (self *env) clear(r *http.Request) {
self.envLock.Lock()
defer self.envLock.Unlock()
delete(self.envMap, r)
}