package api import ( "net/http" ) // HandleTS streams the shared channel's raw MPEG-TS bytes to a viewer. // Every connected browser subscribes to the same upstream via the hub, so the // server holds only one connection to the IPTV source regardless of how many // clients are watching. // GET /ts func (a *App) HandleTS(w http.ResponseWriter, r *http.Request) { flusher, ok := w.(http.Flusher) if !ok { http.Error(w, "streaming unsupported", http.StatusInternalServerError) return } w.Header().Set("Content-Type", "video/mp2t") w.Header().Set("Cache-Control", "no-cache, no-store") w.Header().Set("Connection", "keep-alive") w.WriteHeader(http.StatusOK) flusher.Flush() sub, unsub := a.Hub.Subscribe() defer unsub() ctx := r.Context() for { select { case <-ctx.Done(): return case <-sub.Done(): return case chunk, ok := <-sub.Chan(): if !ok { return } if _, err := w.Write(chunk); err != nil { return } flusher.Flush() } } }