Add comprehensive Claude Code monitoring and realtime streaming to the K8s dashboard. Includes API endpoints for health, stats, summary, inventory, and live event streaming. Frontend provides overview, usage, inventory, debug, and live feed views.
52 lines
986 B
Go
52 lines
986 B
Go
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
|
|
}
|
|
}
|
|
}
|
|
}
|