mirror of
https://github.com/rjNemo/payit
synced 2026-06-06 02:16:40 +00:00
Introduced LoggerMiddleware for HTTP request logging. Refactored handler methods to return http.HandlerFunc for improved composability. Updated route registration and tests to use new handler signatures.
26 lines
594 B
Go
26 lines
594 B
Go
package web
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type WrappedWriter struct {
|
|
http.ResponseWriter
|
|
StatusCode int
|
|
}
|
|
|
|
func (w *WrappedWriter) WriteHeader(statusCode int) {
|
|
w.StatusCode = statusCode
|
|
w.ResponseWriter.WriteHeader(statusCode)
|
|
}
|
|
|
|
func LoggerMiddleware(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.Printf("%s %s %d %v", r.Method, r.URL.Path, wrapped.StatusCode, time.Since(start))
|
|
})
|
|
}
|