updated server config
This commit is contained in:
15
main.go
15
main.go
@@ -71,16 +71,26 @@ func main() {
|
|||||||
|
|
||||||
c := config.Instance()
|
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.Host = host
|
||||||
c.Port = port
|
c.Port = port
|
||||||
|
|
||||||
c.QueueSize = queueSize
|
c.QueueSize = queueSize
|
||||||
|
|
||||||
c.DownloadPath = downloadPath
|
c.DownloadPath = downloadPath
|
||||||
c.DownloaderPath = downloaderPath
|
c.DownloaderPath = downloaderPath
|
||||||
c.SessionFilePath = sessionFilePath
|
c.SessionFilePath = sessionFilePath
|
||||||
|
c.LocalDatabasePath = localDatabasePath
|
||||||
|
|
||||||
|
c.LogPath = logFile
|
||||||
|
c.EnableFileLogging = enableFileLogging
|
||||||
|
|
||||||
c.RequireAuth = requireAuth
|
c.RequireAuth = requireAuth
|
||||||
c.Username = username
|
c.Username = username
|
||||||
c.Password = password
|
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,11 +105,6 @@ func main() {
|
|||||||
openid.Configure()
|
openid.Configure()
|
||||||
|
|
||||||
server.RunBlocking(&server.RunConfig{
|
server.RunBlocking(&server.RunConfig{
|
||||||
Host: c.Host,
|
|
||||||
Port: c.Port,
|
|
||||||
DBPath: localDatabasePath,
|
|
||||||
FileLogging: enableFileLogging,
|
|
||||||
LogFile: logFile,
|
|
||||||
App: frontend,
|
App: frontend,
|
||||||
Swagger: swagger,
|
Swagger: swagger,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
type Config struct {
|
type Config struct {
|
||||||
CurrentLogFile string
|
CurrentLogFile string
|
||||||
LogPath string `yaml:"log_path"`
|
LogPath string `yaml:"log_path"`
|
||||||
|
EnableFileLogging bool `yaml:"enable_file_logging"`
|
||||||
BaseURL string `yaml:"base_url"`
|
BaseURL string `yaml:"base_url"`
|
||||||
Host string `yaml:"host"`
|
Host string `yaml:"host"`
|
||||||
Port int `yaml:"port"`
|
Port int `yaml:"port"`
|
||||||
@@ -20,6 +21,7 @@ type Config struct {
|
|||||||
Username string `yaml:"username"`
|
Username string `yaml:"username"`
|
||||||
Password string `yaml:"password"`
|
Password string `yaml:"password"`
|
||||||
QueueSize int `yaml:"queue_size"`
|
QueueSize int `yaml:"queue_size"`
|
||||||
|
LocalDatabasePath string `yaml:"local_database_path"`
|
||||||
SessionFilePath string `yaml:"session_file_path"`
|
SessionFilePath string `yaml:"session_file_path"`
|
||||||
path string
|
path string
|
||||||
|
|
||||||
|
|||||||
@@ -34,11 +34,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type RunConfig struct {
|
type RunConfig struct {
|
||||||
Host string
|
|
||||||
Port int
|
|
||||||
DBPath string
|
|
||||||
LogFile string
|
|
||||||
FileLogging bool
|
|
||||||
App fs.FS
|
App fs.FS
|
||||||
Swagger fs.FS
|
Swagger fs.FS
|
||||||
}
|
}
|
||||||
@@ -46,15 +41,13 @@ type RunConfig struct {
|
|||||||
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user