-
Notifications
You must be signed in to change notification settings - Fork 4
/
valuer.go
51 lines (43 loc) · 1.17 KB
/
valuer.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
package server
import (
"context"
"net/http"
"sync"
)
// values is a map where we can store values for the request context
// these values will then be available for other components such as
// the render engine.
type valuer struct {
data map[string]any
moot sync.Mutex
}
// Value returns the value for the key specified.
func (v *valuer) Value(key string) any {
return v.data[key]
}
// Values returns the values stored in the valuer.
func (v *valuer) Values() map[string]any {
return v.data
}
// Set sets the value for the key specified.
func (v *valuer) Set(key string, value any) {
v.moot.Lock()
defer v.moot.Unlock()
v.data[key] = value
}
// For each of the requests we want to have a valuer intance so
// that we can store values in the context that can be used by
// other components.
func setValuer(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vlr := &valuer{
data: map[string]any{
// Adding base values that are useful for the handlers.
"request": r,
"currentURL": r.URL.String(),
},
}
r = r.WithContext(context.WithValue(r.Context(), "valuer", vlr))
next.ServeHTTP(w, r)
})
}