package api import ( "encoding/json" "net/http" "time" ) const upstreamDownAfter = 10 * time.Second // HandleStatus returns the current stream status as JSON. // GET /api/status func (a *App) HandleStatus(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } a.mu.RLock() status := a.Status a.mu.RUnlock() // Detect upstream health from the hub. Once the hub receives data, the // channel is considered live; if no data arrives within the threshold // after a switch, flag upstream_down so all watchers see the warning. gotData, since := a.Hub.GotData() if status.Transitioning && gotData { a.mu.Lock() a.Status.Transitioning = false a.Status.UpstreamDown = false a.mu.Unlock() status.Transitioning = false status.UpstreamDown = false } else if status.Transitioning && !gotData && since >= upstreamDownAfter { a.mu.Lock() a.Status.UpstreamDown = true a.mu.Unlock() status.UpstreamDown = true } w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(status) }