Skip to content

Commit

Permalink
Add feature auto publish on app start
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Oct 9, 2023
1 parent c7b6eb5 commit c9c8e73
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
13 changes: 13 additions & 0 deletions internal/streams/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,16 @@ func (s *Stream) Publish(url string) error {

return nil
}

func Publish(stream *Stream, destination any) {
switch v := destination.(type) {
case string:
if err := stream.Publish(v); err != nil {
log.Error().Err(err).Caller().Send()
}
case []any:
for _, v := range v {
Publish(stream, v)
}
}
}
18 changes: 16 additions & 2 deletions internal/streams/streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/url"
"regexp"
"sync"
"time"

"github.com/AlexxIT/go2rtc/internal/api"
"github.com/AlexxIT/go2rtc/internal/app"
Expand All @@ -13,18 +14,31 @@ import (

func Init() {
var cfg struct {
Mod map[string]any `yaml:"streams"`
Streams map[string]any `yaml:"streams"`
Publish map[string]any `yaml:"publish"`
}

app.LoadConfig(&cfg)

log = app.GetLogger("streams")

for name, item := range cfg.Mod {
for name, item := range cfg.Streams {
streams[name] = NewStream(item)
}

api.HandleFunc("api/streams", streamsHandler)

if cfg.Publish == nil {
return
}

time.AfterFunc(5*time.Second, func() {
for name, dst := range cfg.Publish {
if stream := Get(name); stream != nil {
Publish(stream, dst)
}
}
})
}

func Get(name string) *Stream {
Expand Down

0 comments on commit c9c8e73

Please sign in to comment.