2024-09-02 13:38:46 -05:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/charmbracelet/log"
|
|
|
|
|
|
|
|
|
|
"net/http"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type wrappedWriter struct {
|
|
|
|
|
http.ResponseWriter
|
|
|
|
|
statusCode int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *wrappedWriter) WriteHeader(statusCode int) {
|
|
|
|
|
w.ResponseWriter.WriteHeader(statusCode)
|
|
|
|
|
w.statusCode = statusCode
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Logging(next http.Handler) http.Handler {
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
|
|
wrapped := &wrappedWriter{
|
|
|
|
|
ResponseWriter: w,
|
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next.ServeHTTP(wrapped, r)
|
2024-09-28 08:27:53 -05:00
|
|
|
log.Error(
|
|
|
|
|
"INFO",
|
|
|
|
|
wrapped.statusCode,
|
|
|
|
|
r.Method,
|
|
|
|
|
r.URL.Path,
|
|
|
|
|
time.Since(start),
|
|
|
|
|
)
|
2024-09-02 13:38:46 -05:00
|
|
|
})
|
|
|
|
|
}
|