forked from nytimes/gizmo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_native.go
55 lines (48 loc) · 1.28 KB
/
context_native.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
// +build go1.7
package server
import (
"fmt"
"log"
"net/http"
"github.com/NYTimes/gizmo/web"
)
// AddIPToContext will attempt to pull an IP address out of the request and
// set it into a gorilla context.
func AddIPToContext(r *http.Request) {
ip, err := GetIP(r)
if err != nil {
LogWithFields(r).Warningf("unable to get IP: %s", err)
} else {
vars := web.Vars(r)
vars["ip"] = ip
web.SetRouteVars(r, vars)
}
if ip = GetForwardedIP(r); len(ip) > 0 {
vars := web.Vars(r)
vars["forward-for-ip"] = ip
web.SetRouteVars(r, vars)
}
}
// ContextFields will take a request and convert a context map to logrus Fields.
func ContextFields(r *http.Request) map[string]interface{} {
fields := map[string]interface{}{}
for k, v := range web.Vars(r) {
strK := fmt.Sprintf("%+v", k)
typeK := fmt.Sprintf("%T-%+v", k, k)
// gorilla.mux adds the route to context.
// we want to remove it for now
if typeK == "mux.contextKey-1" || typeK == "mux.contextKey-0" {
log.Print("mux key!?")
continue
}
// web.varsKey for _all_ mux variables (gorilla or httprouter)
if typeK == "web.contextKey-2" {
log.Print("muxvars key!?")
strK = "muxvars"
}
fields[strK] = fmt.Sprintf("%#+v", v)
}
fields["path"] = r.URL.Path
fields["rawquery"] = r.URL.RawQuery
return fields
}