-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathhttp.go
More file actions
26 lines (23 loc) 路 747 Bytes
/
Copy pathhttp.go
File metadata and controls
26 lines (23 loc) 路 747 Bytes
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
package debug
import (
"net/http"
"goa.design/clue/log"
)
// HTTP returns a middleware that manages whether debug log entries are written.
// This middleware should be used in conjunction with the MountDebugLogEnabler
// function. If the request already has a logger, it must be request-local;
// placing log.HTTP before this middleware creates one.
func HTTP() func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if debugLogs.Load() {
ctx = log.Context(ctx, log.WithDebug())
} else {
ctx = log.Context(ctx, log.WithNoDebug())
}
next.ServeHTTP(w, r.WithContext(ctx))
})
return handler
}
}