From 00db216b72ab769c26e010d7302e99af89868595 Mon Sep 17 00:00:00 2001 From: Kittisak Phormraksa Date: Mon, 13 May 2024 13:02:21 +0700 Subject: [PATCH] Create multiple stream sources via API (#8) * Ignore .goreload * API support multiple stream sources --- .gitignore | 2 ++ internal/streams/api.go | 28 +++++++++++++++++++++------- internal/streams/streams.go | 5 +++-- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 04ae894a..8e622330 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ go2rtc_win* 0_test.go .DS_Store + +.goreload \ No newline at end of file diff --git a/internal/streams/api.go b/internal/streams/api.go index 2dc0a275..07b2728f 100644 --- a/internal/streams/api.go +++ b/internal/streams/api.go @@ -2,6 +2,7 @@ package streams import ( "net/http" + "strings" "sync" "github.com/AlexxIT/go2rtc/internal/api" @@ -57,13 +58,26 @@ func apiStreams(w http.ResponseWriter, r *http.Request) { name = src } - if New(name, src) == nil { - http.Error(w, "", http.StatusBadRequest) - return - } - - if err := app.PatchConfig(name, src, "streams"); err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) + if srcs := strings.Split(src, ","); len(srcs) == 1 { + if New(name, src) == nil { + http.Error(w, "", http.StatusBadRequest) + return + } + if err := app.PatchConfig(name, src, "streams"); err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + } + } else { + srcAny := make([]any, len(srcs)) + for i, s := range srcs { + srcAny[i] = s + } + if New(name, srcAny) == nil { + http.Error(w, "", http.StatusBadRequest) + return + } + if err := app.PatchConfig(name, srcAny, "streams"); err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + } } case "PATCH": diff --git a/internal/streams/streams.go b/internal/streams/streams.go index 317b005f..737c9a96 100644 --- a/internal/streams/streams.go +++ b/internal/streams/streams.go @@ -56,8 +56,9 @@ func Validate(source string) error { return nil } -func New(name string, source string) *Stream { - if Validate(source) != nil { +func New(name string, source any) *Stream { + // not allow creating dynamic streams with spaces in the source + if src, ok := source.(string); ok && Validate(src) != nil { return nil }