package api import ( "net/http" "sort" "gopkg.in/yaml.v3" "streamd/apps/streamd/internal/auth" "streamd/apps/streamd/internal/modstore" "streamd/modhost" "streamd/mpd" "streamd/repo" ) // appInfo describes one modules// provider to the dashboard, so the UI // never has to hardcode a provider or channel name. type appInfo struct { Name string `json:"name"` Channels []channelInfo `json:"channels"` Rewriter string `json:"rewriter"` HasConfig bool `json:"has_config"` Error string `json:"error,omitempty"` } type channelInfo struct { ID string `json:"id"` Label string `json:"label"` } // apps answers GET /api/apps: which providers exist under the panel modules store. // A single module that fails to describe is skipped with an error field rather // than failing the whole list (bad uploads must not blank the stream pickers). func (h *Handler) apps(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { writeErr(w, http.StatusMethodNotAllowed, "method not allowed") return } if _, ok := auth.Guard(w, r, auth.RoleViewer); !ok { return } out := []appInfo{} mods, err := modhost.Discover(repo.Root()) if err != nil { writeErr(w, http.StatusInternalServerError, err.Error()) return } for _, m := range mods { bin, err := modhost.EnsureBuilt(m) if err != nil { info := appInfo{Name: m.Name, Channels: []channelInfo{}, Rewriter: "none", Error: err.Error()} h.fillChannelsFromStore(&info, m.Name) out = append(out, info) h.logger().Warn("module describe skipped", "module", m.Name, "err", err) continue } d, err := modhost.RunDescribe(bin) if err != nil { info := appInfo{Name: m.Name, Channels: []channelInfo{}, Rewriter: "none", Error: err.Error()} h.fillChannelsFromStore(&info, m.Name) out = append(out, info) h.logger().Warn("module describe skipped", "module", m.Name, "err", err) continue } if d.Name == "" { d.Name = m.Name } info := appInfo{Name: d.Name, Channels: []channelInfo{}, Rewriter: "none"} for _, ch := range d.Channels { info.Channels = append(info.Channels, channelInfo{ID: ch.ID, Label: ch.Label}) } if d.Rewriter != "" { info.Rewriter = d.Rewriter } // Older module binaries may not see the panel store; fill from module.yaml. if len(info.Channels) == 0 { h.fillChannelsFromStore(&info, m.Name) } info.HasConfig = len(info.Channels) > 0 out = append(out, info) } writeJSON(w, http.StatusOK, map[string]any{ "apps": out, "rewriters": mpd.Names(), }) } func (h *Handler) fillChannelsFromStore(info *appInfo, name string) { if h == nil || h.Modules == nil || info == nil { return } chans := channelsFromStoreYAML(h.Modules, name) if len(chans) == 0 { return } info.Channels = chans info.HasConfig = true if info.Error != "" { // Soft: yaml channels recovered even though describe failed. info.Error = "" } } // channelsFromStoreYAML reads channels from the panel module.yaml when describe // returns none (common with uploaded binaries that resolve values via cwd). func channelsFromStoreYAML(store *modstore.Store, name string) []channelInfo { if store == nil { return nil } raw, err := store.ReadYAML(name) if err != nil || len(raw) == 0 { return nil } var doc struct { Channels map[string]struct { Label string `yaml:"label"` } `yaml:"channels"` } if err := yaml.Unmarshal(raw, &doc); err != nil || len(doc.Channels) == 0 { return nil } ids := make([]string, 0, len(doc.Channels)) for id := range doc.Channels { ids = append(ids, id) } sort.Strings(ids) out := make([]channelInfo, 0, len(ids)) for _, id := range ids { label := doc.Channels[id].Label if label == "" { label = id } out = append(out, channelInfo{ID: id, Label: label}) } return out }