Add WebSocket support methods to statusWriter in gateway.go
Publish telemt-api gateway Docker image / test (push) Successful in 28s
Publish telemt-api gateway Docker image / build-and-push (push) Successful in 2m3s

- Implemented Hijack method to preserve WebSocket upgrade support through middleware.
- Added ReadFrom method to maintain fast-path behavior for io.Copy when the underlying writer supports it.
- Introduced Unwrap method to allow access to the original ResponseWriter, enhancing flexibility in response handling.
This commit is contained in:
Denozordec
2026-03-31 09:57:15 +07:00
parent 309b4a0e64
commit 6c1f8a8a37
+23
View File
@@ -1,9 +1,11 @@
package server
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"net"
"net/http"
@@ -403,6 +405,27 @@ func (s *statusWriter) Flush() {
}
}
// Hijack preserves WebSocket upgrade support through middleware wrappers.
func (s *statusWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if h, ok := s.ResponseWriter.(http.Hijacker); ok {
return h.Hijack()
}
return nil, nil, http.ErrNotSupported
}
// ReadFrom keeps io.Copy fast-path behavior when the underlying writer supports it.
func (s *statusWriter) ReadFrom(r io.Reader) (int64, error) {
if rf, ok := s.ResponseWriter.(io.ReaderFrom); ok {
return rf.ReadFrom(r)
}
return io.Copy(s.ResponseWriter, r)
}
// Unwrap lets net/http.ResponseController reach the original writer.
func (s *statusWriter) Unwrap() http.ResponseWriter {
return s.ResponseWriter
}
// Shutdown idle connections on the shared transport.
func (g *Gateway) Shutdown(ctx context.Context) error {
g.transport.CloseIdleConnections()