Skip to content

Commit

Permalink
Update type for flushable writer
Browse files Browse the repository at this point in the history
  • Loading branch information
akclace committed Dec 24, 2024
1 parent d73571c commit a816585
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions internal/server/audit_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@ import (
"github.com/segmentio/ksuid"
)

type FlushableWriter interface {
http.ResponseWriter
http.Flusher
}

// FlushableStatusResponseWriter wraps http.ResponseWriter to capture the status code.
type FlushableStatusResponseWriter struct {
http.ResponseWriter
FlushableWriter
statusCode int
}

// WriteHeader captures the status code.
func (f *FlushableStatusResponseWriter) WriteHeader(code int) {
f.statusCode = code
f.ResponseWriter.WriteHeader(code)
}

func (f *FlushableStatusResponseWriter) Flush() {
f.ResponseWriter.(http.Flusher).Flush()
f.FlushableWriter.WriteHeader(code)
}

// StatusResponseWriter wraps http.ResponseWriter to capture the status code.
Expand Down Expand Up @@ -210,10 +211,10 @@ func (server *Server) handleStatus(next http.Handler) http.Handler {

// Wrap the ResponseWriter
var crw any
if _, ok := w.(http.Flusher); ok {
if fw, ok := w.(FlushableWriter); ok {
crw = &FlushableStatusResponseWriter{
ResponseWriter: w,
statusCode: http.StatusOK, // Default status
FlushableWriter: fw,
statusCode: http.StatusOK, // Default status
}
} else {
crw = &StatusResponseWriter{
Expand Down

0 comments on commit a816585

Please sign in to comment.