diff --git a/main.go b/main.go index 960a2fe..d2f9513 100644 --- a/main.go +++ b/main.go @@ -71,16 +71,26 @@ func main() { c := config.Instance() - c.Host = host - c.Port = port - c.QueueSize = queueSize - c.DownloadPath = downloadPath - c.DownloaderPath = downloaderPath - c.SessionFilePath = sessionFilePath + { + // init the config struct with the values from flags + // TODO: find an alternative way to populate the config struct from flags or config file + c.Host = host + c.Port = port - c.RequireAuth = requireAuth - c.Username = username - c.Password = password + c.QueueSize = queueSize + + c.DownloadPath = downloadPath + c.DownloaderPath = downloaderPath + c.SessionFilePath = sessionFilePath + c.LocalDatabasePath = localDatabasePath + + c.LogPath = logFile + c.EnableFileLogging = enableFileLogging + + c.RequireAuth = requireAuth + c.Username = username + c.Password = password + } // limit concurrent downloads for systems with 2 or less logical cores if runtime.NumCPU() <= 2 { @@ -95,12 +105,7 @@ func main() { openid.Configure() server.RunBlocking(&server.RunConfig{ - Host: c.Host, - Port: c.Port, - DBPath: localDatabasePath, - FileLogging: enableFileLogging, - LogFile: logFile, - App: frontend, - Swagger: swagger, + App: frontend, + Swagger: swagger, }) } diff --git a/server/config/config.go b/server/config/config.go index 08d4604..edb2491 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -9,19 +9,21 @@ import ( ) type Config struct { - CurrentLogFile string - LogPath string `yaml:"log_path"` - BaseURL string `yaml:"base_url"` - Host string `yaml:"host"` - Port int `yaml:"port"` - DownloadPath string `yaml:"downloadPath"` - DownloaderPath string `yaml:"downloaderPath"` - RequireAuth bool `yaml:"require_auth"` - Username string `yaml:"username"` - Password string `yaml:"password"` - QueueSize int `yaml:"queue_size"` - SessionFilePath string `yaml:"session_file_path"` - path string + CurrentLogFile string + LogPath string `yaml:"log_path"` + EnableFileLogging bool `yaml:"enable_file_logging"` + BaseURL string `yaml:"base_url"` + Host string `yaml:"host"` + Port int `yaml:"port"` + DownloadPath string `yaml:"downloadPath"` + DownloaderPath string `yaml:"downloaderPath"` + RequireAuth bool `yaml:"require_auth"` + Username string `yaml:"username"` + Password string `yaml:"password"` + QueueSize int `yaml:"queue_size"` + LocalDatabasePath string `yaml:"local_database_path"` + SessionFilePath string `yaml:"session_file_path"` + path string UseOpenId bool `yaml:"use_openid"` OpenIdProviderURL string `yaml:"openid_provider_url"` diff --git a/server/server.go b/server/server.go index 5bb3888..7bab18c 100644 --- a/server/server.go +++ b/server/server.go @@ -34,27 +34,20 @@ import ( ) type RunConfig struct { - Host string - Port int - DBPath string - LogFile string - FileLogging bool - App fs.FS - Swagger fs.FS + App fs.FS + Swagger fs.FS } type serverConfig struct { frontend fs.FS swagger fs.FS - host string - port int mdb *internal.MemoryDB db *sql.DB mq *internal.MessageQueue lm *livestream.Monitor } -func RunBlocking(cfg *RunConfig) { +func RunBlocking(rc *RunConfig) { mdb := internal.NewMemoryDB() // ---- LOGGING --------------------------------------------------- @@ -63,9 +56,11 @@ func RunBlocking(cfg *RunConfig) { logging.NewObservableLogger(), // for web-ui } + conf := config.Instance() + // file based logging - if cfg.FileLogging { - logger, err := logging.NewRotableLogger(cfg.LogFile) + if conf.EnableFileLogging { + logger, err := logging.NewRotableLogger(conf.LogPath) if err != nil { panic(err) } @@ -88,7 +83,7 @@ func RunBlocking(cfg *RunConfig) { slog.SetDefault(logger) // ---------------------------------------------------------------- - db, err := sql.Open("sqlite", cfg.DBPath) + db, err := sql.Open("sqlite", conf.LocalDatabasePath) if err != nil { slog.Error("failed to open database", slog.String("err", err.Error())) } @@ -109,10 +104,8 @@ func RunBlocking(cfg *RunConfig) { go lm.Restore() srv := newServer(serverConfig{ - frontend: cfg.App, - swagger: cfg.Swagger, - host: cfg.Host, - port: cfg.Port, + frontend: rc.App, + swagger: rc.Swagger, mdb: mdb, mq: mq, db: db, @@ -120,17 +113,17 @@ func RunBlocking(cfg *RunConfig) { }) go gracefulShutdown(srv, mdb) - go autoPersist(time.Minute*5, mdb) + go autoPersist(time.Minute*5, mdb, lm) var ( network = "tcp" - address = fmt.Sprintf("%s:%d", cfg.Host, cfg.Port) + address = fmt.Sprintf("%s:%d", conf.Host, conf.Port) ) // support unix sockets - if strings.HasPrefix(cfg.Host, "/") { + if strings.HasPrefix(conf.Host, "/") { network = "unix" - address = cfg.Host + address = conf.Host } listener, err := net.Listen(network, address) @@ -147,13 +140,6 @@ func RunBlocking(cfg *RunConfig) { } func newServer(c serverConfig) *http.Server { - go func() { - for { - c.lm.Persist() - time.Sleep(time.Minute * 5) - } - }() - service := ytdlpRPC.Container(c.mdb, c.mq, c.lm) rpc.Register(service) @@ -246,13 +232,14 @@ func gracefulShutdown(srv *http.Server, db *internal.MemoryDB) { }() } -func autoPersist(d time.Duration, db *internal.MemoryDB) { +func autoPersist(d time.Duration, db *internal.MemoryDB, lm *livestream.Monitor) { for { if err := db.Persist(); err != nil { + slog.Warn("failed to persisted session", slog.Any("err", err)) + } + if err := lm.Persist(); err != nil { slog.Warn( - "failed to persisted session", - slog.String("err", err.Error()), - ) + "failed to persisted livestreams monitor session", slog.Any("err", err.Error())) } slog.Debug("sucessfully persisted session") time.Sleep(d)