From eb18a502bfd7bc13aaec6e923a9e4ac1fc01b7e4 Mon Sep 17 00:00:00 2001 From: Michael Tiller Date: Mon, 9 Jan 2023 21:48:37 -0500 Subject: [PATCH] fix: implement Flusher interface for wrapped ResponseWriters --- context.go | 9 +++++++++ middleware/logger.go | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/context.go b/context.go index 84ae24e3..43eacc54 100644 --- a/context.go +++ b/context.go @@ -100,6 +100,9 @@ type Context interface { // WriteContent wraps http.ServeContent in order to handle serving streams // it will handle Range and Modified (like If-Unmodified-Since) headers. WriteContent(name string, content io.ReadSeeker, lastModified time.Time) + + // Implement the http.Flusher interface + Flush() } type hcontext struct { @@ -431,3 +434,9 @@ func (c *hcontext) WriteContent(name string, content io.ReadSeeker, lastModified http.ServeContent(c.ResponseWriter, c.r, name, lastModified, content) c.closed = true } + +func (c *hcontext) Flush() { + if f, ok := c.ResponseWriter.(http.Flusher); ok { + f.Flush() + } +} diff --git a/middleware/logger.go b/middleware/logger.go index f147af98..ffa3ee1a 100644 --- a/middleware/logger.go +++ b/middleware/logger.go @@ -70,6 +70,14 @@ func (r *statusRecorder) WriteHeader(statusCode int) { r.ResponseWriter.WriteHeader(statusCode) } +// `statusRecorder` implements the Flusher interface if the +// underlying `ResponseWriter` does +func (r *statusRecorder) Flush() { + if f, ok := r.ResponseWriter.(http.Flusher); ok { + f.Flush() + } +} + // Logger creates a new middleware to set a tagged `*zap.SugarLogger` in the // request context. It debug logs request info. If the current terminal is a // TTY, it will try to use colored output automatically.