gpaste/middleware.go
Torjus Håkestad e3ff8065f1
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Add some middleware
2022-01-19 03:23:54 +01:00

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)
}