mirror of
https://github.com/wso2/open-mcp-auth-proxy.git
synced 2025-06-27 17:13:31 +00:00
34 lines
761 B
Go
34 lines
761 B
Go
package proxy
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net/http"
|
|
"net/http/httputil"
|
|
"time"
|
|
)
|
|
|
|
// HandleSSE sets up a go-routine to wait for context cancellation
|
|
// and flushes the response if possible.
|
|
func HandleSSE(w http.ResponseWriter, r *http.Request, rp *httputil.ReverseProxy) {
|
|
ctx := r.Context()
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
<-ctx.Done()
|
|
log.Printf("INFO: SSE connection closed from %s (path: %s)", r.RemoteAddr, r.URL.Path)
|
|
close(done)
|
|
}()
|
|
|
|
rp.ServeHTTP(w, r)
|
|
if f, ok := w.(http.Flusher); ok {
|
|
f.Flush()
|
|
}
|
|
|
|
<-done
|
|
}
|
|
|
|
// NewShutdownContext is a little helper to gracefully shut down
|
|
func NewShutdownContext(timeout time.Duration) (context.Context, context.CancelFunc) {
|
|
return context.WithTimeout(context.Background(), timeout)
|
|
}
|