Update PUT /api/streams for support multiple src params

This commit is contained in:
Alex X
2024-10-24 20:44:37 +03:00
parent 95a5283c86
commit a8d394efd7
3 changed files with 7 additions and 31 deletions
+1 -1
View File
@@ -101,7 +101,7 @@ func apiPair(id, url string) error {
return err return err
} }
streams.New(id, []string{conn.URL()}) streams.New(id, conn.URL())
return app.PatchConfig(id, conn.URL(), "streams") return app.PatchConfig(id, conn.URL(), "streams")
} }
+4 -28
View File
@@ -1,7 +1,6 @@
package streams package streams
import ( import (
"encoding/json"
"net/http" "net/http"
"github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/api"
@@ -9,18 +8,13 @@ import (
"github.com/AlexxIT/go2rtc/pkg/probe" "github.com/AlexxIT/go2rtc/pkg/probe"
) )
func returnAllStreams(w http.ResponseWriter) {
api.ResponseJSON(w, streams)
}
func apiStreams(w http.ResponseWriter, r *http.Request) { func apiStreams(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query() query := r.URL.Query()
src := query.Get("src") src := query.Get("src")
// without source - return all streams list // without source - return all streams list
// PUT checks first body for sources if src == "" && r.Method != "POST" {
if src == "" && r.Method != "POST" && r.Method != "PUT" { api.ResponseJSON(w, streams)
returnAllStreams(w)
return return
} }
@@ -53,31 +47,13 @@ func apiStreams(w http.ResponseWriter, r *http.Request) {
if name == "" { if name == "" {
name = src name = src
} }
var sources []string
if src != "" {
sources = []string{src}
} else if r.Header.Get("Content-Type") == "application/json" {
var data struct {
Sources []string `json:"sources"`
}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
log.Error().Err(err).Caller().Send()
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
sources = data.Sources
} else {
// without source(s) - return all streams list
returnAllStreams(w)
return
}
if New(name, sources) == nil { if New(name, query["src"]...) == nil {
http.Error(w, "", http.StatusBadRequest) http.Error(w, "", http.StatusBadRequest)
return return
} }
if err := app.PatchConfig(name, sources, "streams"); err != nil { if err := app.PatchConfig(name, query["src"], "streams"); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
} }
+2 -2
View File
@@ -56,7 +56,7 @@ func Validate(source string) error {
return nil return nil
} }
func New(name string, sources []string) *Stream { func New(name string, sources ...string) *Stream {
for _, source := range sources { for _, source := range sources {
if Validate(source) != nil { if Validate(source) != nil {
return nil return nil
@@ -107,7 +107,7 @@ func Patch(name string, source string) *Stream {
} }
// create new stream with this name // create new stream with this name
return New(name, []string{source}) return New(name, source)
} }
func GetOrPatch(query url.Values) *Stream { func GetOrPatch(query url.Values) *Stream {