backend code

This commit is contained in:
2025-08-24 15:55:44 +02:00
parent 14a03d6a77
commit 4e1b4bad0a
7 changed files with 383 additions and 2 deletions

View File

@@ -34,6 +34,7 @@ import (
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/status"
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/subscription"
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/subscription/task"
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/twitch"
"github.com/marcopiovanello/yt-dlp-web-ui/v3/server/user"
_ "modernc.org/sqlite"
@@ -51,6 +52,7 @@ type serverConfig struct {
db *sql.DB
mq *internal.MessageQueue
lm *livestream.Monitor
tm *twitch.Monitor
}
// TODO: change scope
@@ -115,6 +117,15 @@ func RunBlocking(rc *RunConfig) {
go lm.Schedule()
go lm.Restore()
tm := twitch.NewMonitor(
twitch.NewAuthenticationManager(
config.Instance().Twitch.ClientId,
config.Instance().Twitch.ClientSecret,
),
)
go tm.Monitor(context.TODO(), time.Minute*1, twitch.DEFAULT_DOWNLOAD_HANDLER(mdb, mq))
go tm.Restore()
srv := newServer(serverConfig{
frontend: rc.App,
swagger: rc.Swagger,
@@ -122,10 +133,11 @@ func RunBlocking(rc *RunConfig) {
mq: mq,
db: db,
lm: lm,
tm: tm,
})
go gracefulShutdown(srv, mdb)
go autoPersist(time.Minute*5, mdb, lm)
go autoPersist(time.Minute*5, mdb, lm, tm)
var (
network = "tcp"
@@ -235,6 +247,11 @@ func newServer(c serverConfig) *http.Server {
// Subscriptions
r.Route("/subscriptions", subscription.Container(c.db, cronTaskRunner).ApplyRouter())
// Twitch
r.Route("/twitch", func(r chi.Router) {
r.Post("/add", twitch.MonitorUserHandler(c.tm))
})
return &http.Server{Handler: r}
}
@@ -258,7 +275,12 @@ func gracefulShutdown(srv *http.Server, db *internal.MemoryDB) {
}()
}
func autoPersist(d time.Duration, db *internal.MemoryDB, lm *livestream.Monitor) {
func autoPersist(
d time.Duration,
db *internal.MemoryDB,
lm *livestream.Monitor,
tm *twitch.Monitor,
) {
for {
if err := db.Persist(); err != nil {
slog.Warn("failed to persisted session", slog.Any("err", err))
@@ -267,6 +289,10 @@ func autoPersist(d time.Duration, db *internal.MemoryDB, lm *livestream.Monitor)
slog.Warn(
"failed to persisted livestreams monitor session", slog.Any("err", err.Error()))
}
if err := tm.Persist(); err != nil {
slog.Warn(
"failed to persisted twitch monitor session", slog.Any("err", err.Error()))
}
slog.Debug("sucessfully persisted session")
time.Sleep(d)
}