package middleware

import (
	"log/slog"
	"net/http"
	"time"
)

// Log logs the request and execution time
// https://rickyanto.com/how-to-write-simple-middleware-using-go-nethttp-standard-library/
func Log(next http.HandlerFunc) http.HandlerFunc {
	fn := func(w http.ResponseWriter, r *http.Request) {
		start := time.Now()
		slog.Info("Received request", "Path", r.URL.Path, "Method", r.Method, "Time", start)
		next.ServeHTTP(w, r)
		slog.Info("Finished request", "Path", r.URL.Path, "Method", r.Method, "ExecutionTime", time.Since(start))
	}

	return fn
}
