Add feature auto publish on app start

This commit is contained in:
Alex X
2023-10-09 23:09:28 +03:00
parent c7b6eb5d5b
commit c9c8e73587
2 changed files with 29 additions and 2 deletions
+13
View File
@@ -17,3 +17,16 @@ func (s *Stream) Publish(url string) error {
return nil 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)
}
}
}
+16 -2
View File
@@ -5,6 +5,7 @@ import (
"net/url" "net/url"
"regexp" "regexp"
"sync" "sync"
"time"
"github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/api"
"github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/app"
@@ -13,18 +14,31 @@ import (
func Init() { func Init() {
var cfg struct { var cfg struct {
Mod map[string]any `yaml:"streams"` Streams map[string]any `yaml:"streams"`
Publish map[string]any `yaml:"publish"`
} }
app.LoadConfig(&cfg) app.LoadConfig(&cfg)
log = app.GetLogger("streams") log = app.GetLogger("streams")
for name, item := range cfg.Mod { for name, item := range cfg.Streams {
streams[name] = NewStream(item) streams[name] = NewStream(item)
} }
api.HandleFunc("api/streams", streamsHandler) 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 { func Get(name string) *Stream {