ai cooking
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

log status code (#245)

* log status code

* default to ok if they don't set it

* toss user in there too

authored by

Paul Miller and committed by
GitHub
7d14f1ac 603a7c81

+22 -3
+22 -3
cmd/careme/middleware.go
··· 5 5 "net/http" 6 6 "runtime/debug" 7 7 "time" 8 + 9 + "github.com/clerk/clerk-sdk-go/v2" 8 10 ) 9 11 10 12 type logger struct { 11 13 http.Handler 12 14 } 13 15 16 + type loggingResponseWriter struct { 17 + http.ResponseWriter 18 + statusCode int 19 + } 20 + 21 + func (lrw *loggingResponseWriter) WriteHeader(code int) { 22 + lrw.statusCode = code 23 + lrw.ResponseWriter.WriteHeader(code) 24 + } 25 + 14 26 func (l *logger) ServeHTTP(w http.ResponseWriter, r *http.Request) { 15 27 start := time.Now() 16 - l.Handler.ServeHTTP(w, r) 28 + //should we use auth client? 29 + user := "" 30 + if claims, ok := clerk.SessionClaimsFromContext(r.Context()); ok { 31 + user = claims.Subject 32 + } 33 + 34 + lrw := &loggingResponseWriter{w, http.StatusOK} 35 + l.Handler.ServeHTTP(lrw, r) 17 36 if r.URL.Path == "/ready" { 18 37 return 19 38 } 20 - // TODO log status code. 21 - slog.Info("request", "method", r.Method, "url", r.URL.Path, "query", r.URL.Query(), "form", r.Form, "duration", time.Since(start)) 39 + 40 + slog.Info("request", "method", r.Method, "url", r.URL.Path, "query", r.URL.Query(), "response", lrw.statusCode, "user", user, "form", r.Form, "duration", time.Since(start)) 22 41 } 23 42 24 43 type recoverer struct {