vendor: Update github.com/matrix-org/util

This commit is contained in:
Robert Swain 2017-04-28 16:24:07 +02:00
parent f2437be52b
commit 5d4432218b
2 changed files with 20 additions and 13 deletions

2
vendor/manifest vendored
View file

@ -98,7 +98,7 @@
{
"importpath": "github.com/matrix-org/util",
"repository": "https://github.com/matrix-org/util",
"revision": "bc9d5e2d2f68a2ca279fce0fa2f28a91ecf301ed",
"revision": "8b11d9882e131d58ff40525c8f7b9a7f4b811a43",
"branch": "master"
},
{

View file

@ -93,23 +93,30 @@ func Protect(handler http.HandlerFunc) http.HandlerFunc {
}
}
// SetupRequestLogging sets up standard logging for http.Requests.
// http.Requests will have a logger (with a request ID/method/path logged) attached to the Context.
// This can be accessed via GetLogger(Context).
func SetupRequestLogging(req *http.Request) {
reqID := RandomString(12)
// Set a Logger and request ID on the context
ctx := context.WithValue(req.Context(), ctxValueLogger, log.WithFields(log.Fields{
"req.method": req.Method,
"req.path": req.URL.Path,
"req.id": reqID,
}))
ctx = context.WithValue(ctx, ctxValueRequestID, reqID)
req = req.WithContext(ctx)
logger := GetLogger(req.Context())
logger.Print("Incoming request")
}
// MakeJSONAPI creates an HTTP handler which always responds to incoming requests with JSON responses.
// Incoming http.Requests will have a logger (with a request ID/method/path logged) attached to the Context.
// This can be accessed via GetLogger(Context).
func MakeJSONAPI(handler JSONRequestHandler) http.HandlerFunc {
return Protect(func(w http.ResponseWriter, req *http.Request) {
reqID := RandomString(12)
// Set a Logger and request ID on the context
ctx := context.WithValue(req.Context(), ctxValueLogger, log.WithFields(log.Fields{
"req.method": req.Method,
"req.path": req.URL.Path,
"req.id": reqID,
}))
ctx = context.WithValue(ctx, ctxValueRequestID, reqID)
req = req.WithContext(ctx)
logger := GetLogger(req.Context())
logger.Print("Incoming request")
SetupRequestLogging(req)
if req.Method == "OPTIONS" {
SetCORSHeaders(w)