From a965e5d13027bc45f9f1adc706d07d3f6e0eb111 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/streams.go | 32 +++++++++++++++++++++++--------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 522f6275..559b482d 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ go2rtc.json 0_test.go .DS_Store + +.goreload \ No newline at end of file diff --git a/internal/streams/streams.go b/internal/streams/streams.go index 987744c6..df59dc89 100644 --- a/internal/streams/streams.go +++ b/internal/streams/streams.go @@ -4,6 +4,7 @@ import ( "net/http" "net/url" "regexp" + "strings" "sync" "time" @@ -47,9 +48,9 @@ func Get(name string) *Stream { var sanitize = regexp.MustCompile(`\s`) -func New(name string, source string) *Stream { +func New(name string, source any) *Stream { // not allow creating dynamic streams with spaces in the source - if sanitize.MatchString(source) { + if src, ok := source.(string); ok && sanitize.MatchString(src) { return nil } @@ -162,13 +163,26 @@ func streamsHandler(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":