-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
45 lines (40 loc) · 1.14 KB
/
handlers.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
package confmgr
import (
"fmt"
"github.com/gorilla/context"
"github.com/moensch/confmgr/backends"
"net/http"
"strings"
)
func (c *ConfMgr) Index(w http.ResponseWriter, r *http.Request, b backend.ConfigBackend) {
fmt.Fprintln(w, "Welcome!")
}
/*
* Extract scope variables from x-cfg-FOO request headers
*/
func (c *ConfMgr) SetRequestScopeFromHeaders(headers http.Header) {
c.RequestScope = make(map[string]string)
for hdrname, hdrval := range headers {
hdrname = strings.ToLower(hdrname)
if strings.HasPrefix(hdrname, "x-cfg-") {
scopevar := strings.TrimPrefix(hdrname, "x-cfg-")
scopeval := strings.ToLower(hdrval[0])
c.RequestScope[scopevar] = scopeval
}
}
}
func ScopeFromHeaders(headers http.Header) map[string]string {
scope := make(map[string]string)
for hdrname, hdrval := range headers {
hdrname = strings.ToLower(hdrname)
if strings.HasPrefix(hdrname, "x-cfg-") {
scopevar := strings.TrimPrefix(hdrname, "x-cfg-")
scopeval := strings.ToLower(hdrval[0])
scope[scopevar] = scopeval
}
}
return scope
}
func GetRequestScope(r *http.Request) map[string]string {
return context.Get(r, ReqScope).(map[string]string)
}