add api endpoints and change config syntax

This commit is contained in:
seydx
2025-06-05 11:33:03 +03:00
parent 020549ef60
commit 493fa1ef6a
4 changed files with 85 additions and 20 deletions

View File

@@ -122,3 +122,65 @@ func apiStreamsDOT(w http.ResponseWriter, r *http.Request) {
api.Response(w, dot, "text/vnd.graphviz")
}
func apiPreload(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
src := query.Get("src")
query.Del("src")
videoQuery := query.Get("video")
audioQuery := query.Get("audio")
micQuery := query.Get("microphone")
if src == "" {
http.Error(w, "no source", http.StatusBadRequest)
return
}
switch r.Method {
case "PUT":
// check if stream exists
if stream := Get(src); stream == nil {
http.Error(w, "stream not found", http.StatusNotFound)
return
}
// check if consumer exists
if cons, ok := preloads[src]; ok {
cons.Stop()
delete(preloads, src)
}
var rawQuery string
if videoQuery != "" {
rawQuery += "video=" + videoQuery + "#"
}
if audioQuery != "" {
rawQuery += "audio=" + audioQuery + "#"
}
if micQuery != "" {
rawQuery += "microphone=" + micQuery
}
if err := app.PatchConfig([]string{"preload", src}, rawQuery); err != nil {
log.Error().Err(err).Str("src", src).Msg("Failed to patch config for PUT")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
Preload(src, rawQuery)
case "DELETE":
if cons, ok := preloads[src]; ok {
cons.Stop()
delete(preloads, src)
}
if err := app.PatchConfig([]string{"preload", src}, nil); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
default:
http.Error(w, "", http.StatusMethodNotAllowed)
}
}