package api import ( "encoding/json" "fmt" "net/http" "github.com/will/k8s-agent-dashboard/internal/claude" ) func GetClaudeStream(hub *claude.EventHub) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") w.Header().Set("Cache-Control", "no-cache") w.Header().Set("Connection", "keep-alive") w.Header().Set("Access-Control-Allow-Origin", "*") flusher, ok := w.(http.Flusher) if !ok { http.Error(w, "Streaming unsupported", http.StatusInternalServerError) return } ch, cancel := hub.Subscribe() defer cancel() notify := r.Context().Done() for { select { case ev, ok := <-ch: if !ok { return } data, err := json.Marshal(ev) if err != nil { continue } fmt.Fprintf(w, "event: %s\n", ev.Type) fmt.Fprintf(w, "id: %d\n", ev.ID) fmt.Fprintf(w, "data: %s\n\n", data) flusher.Flush() case <-notify: return } } } }