diff --git a/daemon/daemon.go b/daemon/daemon.go index a684439..eb020e7 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -3,8 +3,10 @@ package daemon import ( "context" "fmt" + "log/slog" "net/http" service "spud/service" + "strconv" ) type Daemon struct { @@ -16,9 +18,30 @@ type Daemon struct { type HandlerFuncWithContext = func(w http.ResponseWriter, r *http.Request, c context.Context) +type RecordingResponseWriter struct { + responseWriter http.ResponseWriter + + StatusCode int +} + +func (r *RecordingResponseWriter) WriteHeader(statusCode int) { + r.StatusCode = statusCode + r.responseWriter.WriteHeader(statusCode) +} + +func (r RecordingResponseWriter) Write(b []byte) (int, error) { + return r.responseWriter.Write(b) +} + +func (r RecordingResponseWriter) Header() http.Header { + return r.responseWriter.Header() +} + func handleFuncWithContext(h HandlerFuncWithContext, c context.Context) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - h(w, r, c) + respWriter := &RecordingResponseWriter{responseWriter: w} + h(respWriter, r, c) + slog.Info("Request", "Method", r.Method, "Path", r.URL.Path, "Status", strconv.Itoa(respWriter.StatusCode)) } }