config package refactor

This commit is contained in:
2023-10-24 16:07:38 +02:00
parent 0c7415df28
commit b512e996ad
11 changed files with 33 additions and 73 deletions

18
main.go
View File

@@ -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")
}

View File

@@ -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

View File

@@ -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) {

View File

@@ -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 {

View File

@@ -98,7 +98,7 @@ func (m *MemoryDB) Persist() {
running := m.All()
sessionFile := filepath.Join(
config.Instance().GetConfig().SessionFilePath,
config.Instance().SessionFilePath,
"session.dat",
)

View File

@@ -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")

View File

@@ -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")
)

View File

@@ -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()

View File

@@ -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
}

View File

@@ -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)

View File

@@ -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 {