From b512e996add3b861067dcba46489aff2e0c85669 Mon Sep 17 00:00:00 2001 From: marcobaobao Date: Tue, 24 Oct 2023 16:07:38 +0200 Subject: [PATCH] config package refactor --- main.go | 18 +++++----- server/config/parser.go | 58 +++++--------------------------- server/handlers/archive.go | 4 +-- server/handlers/login.go | 4 +-- server/internal/memory_db.go | 2 +- server/internal/message_queue.go | 2 +- server/internal/playlist.go | 2 +- server/internal/process.go | 8 ++--- server/middleware/jwt.go | 2 +- server/sys/fs.go | 4 +-- server/updater/update.go | 2 +- 11 files changed, 33 insertions(+), 73 deletions(-) diff --git a/main.go b/main.go index 2ad8723..7627764 100644 --- a/main.go +++ b/main.go @@ -59,18 +59,18 @@ func main() { c := config.Instance() - c.SetPort(port) - c.QueueSize(queueSize) - c.DownloadPath(downloadPath) - c.DownloaderPath(downloaderPath) - c.SessionFilePath(sessionFilePath) + c.Port = port + c.QueueSize = queueSize + c.DownloadPath = downloadPath + c.DownloaderPath = downloaderPath + c.SessionFilePath = sessionFilePath - c.RequireAuth(requireAuth) - c.Username(username) - c.Password(password) + c.RequireAuth = requireAuth + c.Username = username + c.Password = password // if config file is found it will be merged with the current config struct - if _, err := c.LoadFromFile(configFile); err != nil { + if err := c.LoadFile(configFile); err != nil { log.Println(cli.BgRed, "config", cli.Reset, "no config file found") } diff --git a/server/config/parser.go b/server/config/parser.go index 7107573..f0b634d 100644 --- a/server/config/parser.go +++ b/server/config/parser.go @@ -9,7 +9,7 @@ import ( var lock sync.Mutex -type serverConfig struct { +type Config struct { Port int `yaml:"port"` DownloadPath string `yaml:"downloadPath"` DownloaderPath string `yaml:"downloaderPath"` @@ -20,67 +20,27 @@ type serverConfig struct { SessionFilePath string `yaml:"session_file_path"` } -type config struct { - cfg serverConfig -} - -func (c *config) LoadFromFile(filename string) (serverConfig, error) { +func (c *Config) LoadFile(filename string) error { fd, err := os.Open(filename) if err != nil { - return serverConfig{}, err + return err } - if err := yaml.NewDecoder(fd).Decode(&c.cfg); err != nil { - return serverConfig{}, err + if err := yaml.NewDecoder(fd).Decode(c); err != nil { + return err } - return c.cfg, nil + return nil } -func (c *config) GetConfig() serverConfig { - return c.cfg -} +var instance *Config -func (c *config) SetPort(port int) { - c.cfg.Port = port -} - -func (c *config) DownloadPath(path string) { - c.cfg.DownloadPath = path -} - -func (c *config) DownloaderPath(path string) { - c.cfg.DownloaderPath = path -} - -func (c *config) RequireAuth(value bool) { - c.cfg.RequireAuth = value -} - -func (c *config) Username(username string) { - c.cfg.Username = username -} - -func (c *config) Password(password string) { - c.cfg.Password = password -} - -func (c *config) QueueSize(size int) { - c.cfg.QueueSize = size -} - -func (c *config) SessionFilePath(path string) { - c.cfg.SessionFilePath = path -} - -var instance *config - -func Instance() *config { +func Instance() *Config { if instance == nil { lock.Lock() defer lock.Unlock() if instance == nil { - instance = &config{serverConfig{}} + instance = &Config{} } } return instance diff --git a/server/handlers/archive.go b/server/handlers/archive.go index f16aadd..49064a6 100644 --- a/server/handlers/archive.go +++ b/server/handlers/archive.go @@ -66,7 +66,7 @@ type ListRequest struct { } func ListDownloaded(w http.ResponseWriter, r *http.Request) { - root := config.Instance().GetConfig().DownloadPath + root := config.Instance().DownloadPath req := new(ListRequest) err := json.NewDecoder(r.Body).Decode(&req) @@ -144,7 +144,7 @@ func SendFile(w http.ResponseWriter, r *http.Request) { decodedStr := string(decoded) - root := config.Instance().GetConfig().DownloadPath + root := config.Instance().DownloadPath // TODO: further path / file validations if strings.Contains(filepath.Dir(decodedStr), root) { diff --git a/server/handlers/login.go b/server/handlers/login.go index 855bd96..5a7ec35 100644 --- a/server/handlers/login.go +++ b/server/handlers/login.go @@ -25,8 +25,8 @@ func Login(w http.ResponseWriter, r *http.Request) { } var ( - username = config.Instance().GetConfig().Username - password = config.Instance().GetConfig().Password + username = config.Instance().Username + password = config.Instance().Password ) if username != req.Username || password != req.Password { diff --git a/server/internal/memory_db.go b/server/internal/memory_db.go index b1d640f..864ad38 100644 --- a/server/internal/memory_db.go +++ b/server/internal/memory_db.go @@ -98,7 +98,7 @@ func (m *MemoryDB) Persist() { running := m.All() sessionFile := filepath.Join( - config.Instance().GetConfig().SessionFilePath, + config.Instance().SessionFilePath, "session.dat", ) diff --git a/server/internal/message_queue.go b/server/internal/message_queue.go index 1c02df2..a319802 100644 --- a/server/internal/message_queue.go +++ b/server/internal/message_queue.go @@ -16,7 +16,7 @@ type MessageQueue struct { // CPU cores. // The queue size can be set via the qs flag. func NewMessageQueue() *MessageQueue { - size := config.Instance().GetConfig().QueueSize + size := config.Instance().QueueSize if size <= 0 { log.Fatalln("invalid queue size") diff --git a/server/internal/playlist.go b/server/internal/playlist.go index dc5f031..ff7789b 100644 --- a/server/internal/playlist.go +++ b/server/internal/playlist.go @@ -19,7 +19,7 @@ type metadata struct { func PlaylistDetect(req DownloadRequest, mq *MessageQueue, db *MemoryDB) error { var ( - downloader = config.Instance().GetConfig().DownloaderPath + downloader = config.Instance().DownloaderPath cmd = exec.Command(downloader, req.URL, "-J") ) diff --git a/server/internal/process.go b/server/internal/process.go index 4ea4e48..4e0ab2e 100644 --- a/server/internal/process.go +++ b/server/internal/process.go @@ -74,7 +74,7 @@ func (p *Process) Start() { }) out := DownloadOutput{ - Path: config.Instance().GetConfig().DownloadPath, + Path: config.Instance().DownloadPath, Filename: "%(title)s.%(ext)s", } @@ -97,7 +97,7 @@ func (p *Process) Start() { }, p.Params...) // ----------------- main block ----------------- // - cmd := exec.Command(config.Instance().GetConfig().DownloaderPath, params...) + cmd := exec.Command(config.Instance().DownloaderPath, params...) cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} r, err := cmd.StdoutPipe() @@ -192,7 +192,7 @@ func (p *Process) Kill() error { // Returns the available format for this URL func (p *Process) GetFormatsSync() (DownloadFormats, error) { - cmd := exec.Command(config.Instance().GetConfig().DownloaderPath, p.Url, "-J") + cmd := exec.Command(config.Instance().DownloaderPath, p.Url, "-J") stdout, err := cmd.Output() if err != nil { @@ -245,7 +245,7 @@ func (p *Process) SetPending() { } func (p *Process) SetMetadata() error { - cmd := exec.Command(config.Instance().GetConfig().DownloaderPath, p.Url, "-J") + cmd := exec.Command(config.Instance().DownloaderPath, p.Url, "-J") cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} stdout, err := cmd.StdoutPipe() diff --git a/server/middleware/jwt.go b/server/middleware/jwt.go index e40a98f..6c46716 100644 --- a/server/middleware/jwt.go +++ b/server/middleware/jwt.go @@ -13,7 +13,7 @@ import ( func Authenticated(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if !config.Instance().GetConfig().RequireAuth { + if !config.Instance().RequireAuth { next.ServeHTTP(w, r) return } diff --git a/server/sys/fs.go b/server/sys/fs.go index 112f340..88fda33 100644 --- a/server/sys/fs.go +++ b/server/sys/fs.go @@ -14,7 +14,7 @@ import ( // FreeSpace gets the available Bytes writable to download directory func FreeSpace() (uint64, error) { var stat unix.Statfs_t - unix.Statfs(config.Instance().GetConfig().DownloadPath, &stat) + unix.Statfs(config.Instance().DownloadPath, &stat) return (stat.Bavail * uint64(stat.Bsize)), nil } @@ -26,7 +26,7 @@ func DirectoryTree() (*[]string, error) { children []Node } - rootPath := config.Instance().GetConfig().DownloadPath + rootPath := config.Instance().DownloadPath stack := internal.NewStack[Node]() flattened := make([]string, 0) diff --git a/server/updater/update.go b/server/updater/update.go index 2c09ea1..4f3da66 100644 --- a/server/updater/update.go +++ b/server/updater/update.go @@ -8,7 +8,7 @@ import ( // Update using the builtin function of yt-dlp func UpdateExecutable() error { - cmd := exec.Command(config.Instance().GetConfig().DownloaderPath, "-U") + cmd := exec.Command(config.Instance().DownloaderPath, "-U") err := cmd.Start() if err != nil {