recordingHandler attaches a record to the request context so the proxy can fill in the outcome, then writes exactly one line per request once the proxy is done with it.
(proxy http.Handler, rec *recorder)
| 129 | // fill in the outcome, then writes exactly one line per request once the proxy |
| 130 | // is done with it. |
| 131 | func recordingHandler(proxy http.Handler, rec *recorder) http.Handler { |
| 132 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 133 | entry := &record{ |
| 134 | Time: time.Now().UTC().Format(time.RFC3339Nano), |
| 135 | Method: r.Method, |
| 136 | Path: r.URL.RequestURI(), |
| 137 | Host: r.Host, |
| 138 | AuthHeader: r.Header.Get("Authorization") != "", |
| 139 | } |
| 140 | proxy.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), recordKey{}, entry))) |
| 141 | rec.Write(entry) |
| 142 | }) |
| 143 | } |
| 144 | |
| 145 | // newProxy builds the reverse proxy. The upstream URL carries the canonical |
| 146 | // host that requests are forwarded to and that responses are rewritten from, |