31 lines
655 B
Go
31 lines
655 B
Go
|
package gpaste
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
"time"
|
||
|
|
||
|
"github.com/go-chi/chi/v5/middleware"
|
||
|
)
|
||
|
|
||
|
func (s *HTTPServer) MiddlewareAccessLogger(next http.Handler) http.Handler {
|
||
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
||
|
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
|
||
|
t1 := time.Now()
|
||
|
|
||
|
reqID := middleware.GetReqID(r.Context())
|
||
|
|
||
|
defer func() {
|
||
|
s.AccessLogger.Infow(r.Method,
|
||
|
"path", r.URL.Path,
|
||
|
"status", ww.Status(),
|
||
|
"written", ww.BytesWritten(),
|
||
|
"remote_addr", r.RemoteAddr,
|
||
|
"processing_time_ms", time.Since(t1).Milliseconds(),
|
||
|
"req_id", reqID)
|
||
|
}()
|
||
|
|
||
|
next.ServeHTTP(ww, r)
|
||
|
}
|
||
|
return http.HandlerFunc(fn)
|
||
|
}
|