extract and de-duplicate HTTP status capture
1 file changed, 5 insertions(+), 17 deletions(-)
changed files
M domain/analytics/middleware.go → domain/analytics/middleware.go
@@ -3,6 +3,8 @@ import ( "context" "net/http" + + sharedhttp "alin.ovh/homestead/shared/http" ) type contextKey struct{}@@ -22,14 +24,14 @@ func CounterMiddleware(counter Counter) func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - rw := newStatusCapturingResponseWriter(w) + rw := sharedhttp.NewStatusCapturingResponseWriter(w) next.ServeHTTP(rw, r) countKey, ok := GetCountKey(r) if !ok { - if rw.status >= 201 { - countKey = http.StatusText(rw.status) + if rw.Status >= 201 { + countKey = http.StatusText(rw.Status) } else { countKey = r.URL.Path }@@ -39,17 +41,3 @@ counter.Count(r, countKey) }) } } - -type statusCapturingResponseWriter struct { - http.ResponseWriter - status int -} - -func newStatusCapturingResponseWriter(w http.ResponseWriter) *statusCapturingResponseWriter { - return &statusCapturingResponseWriter{w, http.StatusOK} -} - -func (w *statusCapturingResponseWriter) WriteHeader(code int) { - w.status = code - w.ResponseWriter.WriteHeader(code) -}