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

15
main.go
View File

@@ -71,16 +71,26 @@ func main() {
c := config.Instance()
{
// 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.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,11 +105,6 @@ func main() {
openid.Configure()
server.RunBlocking(&server.RunConfig{
Host: c.Host,
Port: c.Port,
DBPath: localDatabasePath,
FileLogging: enableFileLogging,
LogFile: logFile,
App: frontend,
Swagger: swagger,
})

View File

@@ -11,6 +11,7 @@ import (
type Config struct {
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"`
@@ -20,6 +21,7 @@ type Config struct {
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

View File

@@ -34,11 +34,6 @@ import (
)
type RunConfig struct {
Host string
Port int
DBPath string
LogFile string
FileLogging bool
App fs.FS
Swagger fs.FS
}
@@ -46,15 +41,13 @@ type RunConfig struct {
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)