mirror of
https://github.com/1f349/dendrite.git
synced 2024-11-22 19:51:39 +00:00
Merge pull request #97 from matrix-org/rob/util-logging-context-fix
Update github.com/matrix-org/util for request context fix for logging
This commit is contained in:
commit
cdd2617f8e
2
vendor/manifest
vendored
2
vendor/manifest
vendored
@ -98,7 +98,7 @@
|
|||||||
{
|
{
|
||||||
"importpath": "github.com/matrix-org/util",
|
"importpath": "github.com/matrix-org/util",
|
||||||
"repository": "https://github.com/matrix-org/util",
|
"repository": "https://github.com/matrix-org/util",
|
||||||
"revision": "bc9d5e2d2f68a2ca279fce0fa2f28a91ecf301ed",
|
"revision": "53326ed5598b226681112cbd441f59f3cffc9c82",
|
||||||
"branch": "master"
|
"branch": "master"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
17
vendor/src/github.com/matrix-org/util/json.go
vendored
17
vendor/src/github.com/matrix-org/util/json.go
vendored
@ -93,11 +93,10 @@ func Protect(handler http.HandlerFunc) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MakeJSONAPI creates an HTTP handler which always responds to incoming requests with JSON responses.
|
// RequestWithLogging sets up standard logging for http.Requests.
|
||||||
// Incoming http.Requests will have a logger (with a request ID/method/path logged) attached to the Context.
|
// http.Requests will have a logger (with a request ID/method/path logged) attached to the Context.
|
||||||
// This can be accessed via GetLogger(Context).
|
// This can be accessed via GetLogger(Context).
|
||||||
func MakeJSONAPI(handler JSONRequestHandler) http.HandlerFunc {
|
func RequestWithLogging(req *http.Request) *http.Request {
|
||||||
return Protect(func(w http.ResponseWriter, req *http.Request) {
|
|
||||||
reqID := RandomString(12)
|
reqID := RandomString(12)
|
||||||
// Set a Logger and request ID on the context
|
// Set a Logger and request ID on the context
|
||||||
ctx := context.WithValue(req.Context(), ctxValueLogger, log.WithFields(log.Fields{
|
ctx := context.WithValue(req.Context(), ctxValueLogger, log.WithFields(log.Fields{
|
||||||
@ -111,6 +110,16 @@ func MakeJSONAPI(handler JSONRequestHandler) http.HandlerFunc {
|
|||||||
logger := GetLogger(req.Context())
|
logger := GetLogger(req.Context())
|
||||||
logger.Print("Incoming request")
|
logger.Print("Incoming request")
|
||||||
|
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
req = RequestWithLogging(req)
|
||||||
|
|
||||||
if req.Method == "OPTIONS" {
|
if req.Method == "OPTIONS" {
|
||||||
SetCORSHeaders(w)
|
SetCORSHeaders(w)
|
||||||
w.WriteHeader(200)
|
w.WriteHeader(200)
|
||||||
|
Loading…
Reference in New Issue
Block a user