users/server/middleware.go

26 lines
561 B
Go
Raw Normal View History

2023-10-22 19:57:54 +00:00
package server
import (
"net/http"
"time"
"github.com/go-chi/chi/v5/middleware"
)
func (s *UserServer) MiddlewareLogging(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
t1 := time.Now()
defer func(ww middleware.WrapResponseWriter) {
s.Logger.Info("Served request.",
"status", ww.Status(),
"path", r.URL.Path,
"duration", time.Since(t1),
"written", ww.BytesWritten())
}(ww)
next.ServeHTTP(ww, r)
}
return http.HandlerFunc(fn)
}