mirror of
https://gitlab.durp.info/durfy/modules/durpify.git
synced 2026-05-07 08:00:30 -05:00
38 lines
647 B
Go
38 lines
647 B
Go
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)
|
|
log.Error(
|
|
"INFO",
|
|
wrapped.statusCode,
|
|
r.Method,
|
|
r.URL.Path,
|
|
time.Since(start),
|
|
)
|
|
})
|
|
}
|