Skip to content

Commit

Permalink
Update PUT /api/streams for support multiple src params
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Oct 24, 2024
1 parent 95a5283 commit a8d394e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 31 deletions.
2 changes: 1 addition & 1 deletion internal/homekit/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func apiPair(id, url string) error {
return err
}

streams.New(id, []string{conn.URL()})
streams.New(id, conn.URL())

return app.PatchConfig(id, conn.URL(), "streams")
}
Expand Down
32 changes: 4 additions & 28 deletions internal/streams/api.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
package streams

import (
"encoding/json"
"net/http"

"github.com/AlexxIT/go2rtc/internal/api"
"github.com/AlexxIT/go2rtc/internal/app"
"github.com/AlexxIT/go2rtc/pkg/probe"
)

func returnAllStreams(w http.ResponseWriter) {
api.ResponseJSON(w, streams)
}

func apiStreams(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
src := query.Get("src")

// without source - return all streams list
// PUT checks first body for sources
if src == "" && r.Method != "POST" && r.Method != "PUT" {
returnAllStreams(w)
if src == "" && r.Method != "POST" {
api.ResponseJSON(w, streams)
return
}

Expand Down Expand Up @@ -53,31 +47,13 @@ func apiStreams(w http.ResponseWriter, r *http.Request) {
if name == "" {
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)
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)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/streams/streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func Validate(source string) error {
return nil
}

func New(name string, sources []string) *Stream {
func New(name string, sources ...string) *Stream {
for _, source := range sources {
if Validate(source) != nil {
return nil
Expand Down Expand Up @@ -107,7 +107,7 @@ func Patch(name string, source string) *Stream {
}

// create new stream with this name
return New(name, []string{source})
return New(name, source)
}

func GetOrPatch(query url.Values) *Stream {
Expand Down

0 comments on commit a8d394e

Please sign in to comment.