diff --git a/internal/handlers/middleware.go b/internal/handlers/middleware.go index 3f75b25..6da88c1 100644 --- a/internal/handlers/middleware.go +++ b/internal/handlers/middleware.go @@ -9,15 +9,15 @@ import ( func LoggingMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { start := time.Now() - + // Create a response writer wrapper to capture status code wrapper := &responseWriter{ResponseWriter: w, statusCode: http.StatusOK} - + next.ServeHTTP(wrapper, r) - + duration := time.Since(start) timestamp := time.Now().Format(time.RFC3339) - + log.Printf("[%s] %s %s - %d (%v)", timestamp, r.Method, r.URL.Path, wrapper.statusCode, duration) }) } @@ -32,12 +32,12 @@ func CORSMiddleware(next http.Handler) http.Handler { w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization") w.Header().Set("Access-Control-Max-Age", "86400") - + if r.Method == http.MethodOptions { w.WriteHeader(http.StatusOK) return } - + next.ServeHTTP(w, r) }) } @@ -50,4 +50,4 @@ type responseWriter struct { func (rw *responseWriter) WriteHeader(code int) { rw.statusCode = code rw.ResponseWriter.WriteHeader(code) -} \ No newline at end of file +}