updated server config

This commit is contained in:
2024-09-17 10:39:30 +02:00
parent 4f4ea1a599
commit 0f260100f2
3 changed files with 55 additions and 61 deletions

37
main.go
View File

@@ -71,16 +71,26 @@ func main() {
c := config.Instance() c := config.Instance()
c.Host = host {
c.Port = port // init the config struct with the values from flags
c.QueueSize = queueSize // TODO: find an alternative way to populate the config struct from flags or config file
c.DownloadPath = downloadPath c.Host = host
c.DownloaderPath = downloaderPath c.Port = port
c.SessionFilePath = sessionFilePath
c.RequireAuth = requireAuth c.QueueSize = queueSize
c.Username = username
c.Password = password 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 // limit concurrent downloads for systems with 2 or less logical cores
if runtime.NumCPU() <= 2 { if runtime.NumCPU() <= 2 {
@@ -95,12 +105,7 @@ func main() {
openid.Configure() openid.Configure()
server.RunBlocking(&server.RunConfig{ server.RunBlocking(&server.RunConfig{
Host: c.Host, App: frontend,
Port: c.Port, Swagger: swagger,
DBPath: localDatabasePath,
FileLogging: enableFileLogging,
LogFile: logFile,
App: frontend,
Swagger: swagger,
}) })
} }

View File

@@ -9,19 +9,21 @@ import (
) )
type Config struct { type Config struct {
CurrentLogFile string CurrentLogFile string
LogPath string `yaml:"log_path"` LogPath string `yaml:"log_path"`
BaseURL string `yaml:"base_url"` EnableFileLogging bool `yaml:"enable_file_logging"`
Host string `yaml:"host"` BaseURL string `yaml:"base_url"`
Port int `yaml:"port"` Host string `yaml:"host"`
DownloadPath string `yaml:"downloadPath"` Port int `yaml:"port"`
DownloaderPath string `yaml:"downloaderPath"` DownloadPath string `yaml:"downloadPath"`
RequireAuth bool `yaml:"require_auth"` DownloaderPath string `yaml:"downloaderPath"`
Username string `yaml:"username"` RequireAuth bool `yaml:"require_auth"`
Password string `yaml:"password"` Username string `yaml:"username"`
QueueSize int `yaml:"queue_size"` Password string `yaml:"password"`
SessionFilePath string `yaml:"session_file_path"` QueueSize int `yaml:"queue_size"`
path string LocalDatabasePath string `yaml:"local_database_path"`
SessionFilePath string `yaml:"session_file_path"`
path string
UseOpenId bool `yaml:"use_openid"` UseOpenId bool `yaml:"use_openid"`
OpenIdProviderURL string `yaml:"openid_provider_url"` OpenIdProviderURL string `yaml:"openid_provider_url"`

View File

@@ -34,27 +34,20 @@ import (
) )
type RunConfig struct { type RunConfig struct {
Host string App fs.FS
Port int Swagger fs.FS
DBPath string
LogFile string
FileLogging bool
App fs.FS
Swagger fs.FS
} }
type serverConfig struct { type serverConfig struct {
frontend fs.FS frontend fs.FS
swagger fs.FS swagger fs.FS
host string
port int
mdb *internal.MemoryDB mdb *internal.MemoryDB
db *sql.DB db *sql.DB
mq *internal.MessageQueue mq *internal.MessageQueue
lm *livestream.Monitor lm *livestream.Monitor
} }
func RunBlocking(cfg *RunConfig) { func RunBlocking(rc *RunConfig) {
mdb := internal.NewMemoryDB() mdb := internal.NewMemoryDB()
// ---- LOGGING --------------------------------------------------- // ---- LOGGING ---------------------------------------------------
@@ -63,9 +56,11 @@ func RunBlocking(cfg *RunConfig) {
logging.NewObservableLogger(), // for web-ui logging.NewObservableLogger(), // for web-ui
} }
conf := config.Instance()
// file based logging // file based logging
if cfg.FileLogging { if conf.EnableFileLogging {
logger, err := logging.NewRotableLogger(cfg.LogFile) logger, err := logging.NewRotableLogger(conf.LogPath)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -88,7 +83,7 @@ func RunBlocking(cfg *RunConfig) {
slog.SetDefault(logger) slog.SetDefault(logger)
// ---------------------------------------------------------------- // ----------------------------------------------------------------
db, err := sql.Open("sqlite", cfg.DBPath) db, err := sql.Open("sqlite", conf.LocalDatabasePath)
if err != nil { if err != nil {
slog.Error("failed to open database", slog.String("err", err.Error())) slog.Error("failed to open database", slog.String("err", err.Error()))
} }
@@ -109,10 +104,8 @@ func RunBlocking(cfg *RunConfig) {
go lm.Restore() go lm.Restore()
srv := newServer(serverConfig{ srv := newServer(serverConfig{
frontend: cfg.App, frontend: rc.App,
swagger: cfg.Swagger, swagger: rc.Swagger,
host: cfg.Host,
port: cfg.Port,
mdb: mdb, mdb: mdb,
mq: mq, mq: mq,
db: db, db: db,
@@ -120,17 +113,17 @@ func RunBlocking(cfg *RunConfig) {
}) })
go gracefulShutdown(srv, mdb) go gracefulShutdown(srv, mdb)
go autoPersist(time.Minute*5, mdb) go autoPersist(time.Minute*5, mdb, lm)
var ( var (
network = "tcp" network = "tcp"
address = fmt.Sprintf("%s:%d", cfg.Host, cfg.Port) address = fmt.Sprintf("%s:%d", conf.Host, conf.Port)
) )
// support unix sockets // support unix sockets
if strings.HasPrefix(cfg.Host, "/") { if strings.HasPrefix(conf.Host, "/") {
network = "unix" network = "unix"
address = cfg.Host address = conf.Host
} }
listener, err := net.Listen(network, address) listener, err := net.Listen(network, address)
@@ -147,13 +140,6 @@ func RunBlocking(cfg *RunConfig) {
} }
func newServer(c serverConfig) *http.Server { 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) service := ytdlpRPC.Container(c.mdb, c.mq, c.lm)
rpc.Register(service) 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 { for {
if err := db.Persist(); err != nil { if err := db.Persist(); err != nil {
slog.Warn("failed to persisted session", slog.Any("err", err))
}
if err := lm.Persist(); err != nil {
slog.Warn( slog.Warn(
"failed to persisted session", "failed to persisted livestreams monitor session", slog.Any("err", err.Error()))
slog.String("err", err.Error()),
)
} }
slog.Debug("sucessfully persisted session") slog.Debug("sucessfully persisted session")
time.Sleep(d) time.Sleep(d)