config package refactor
This commit is contained in:
18
main.go
18
main.go
@@ -59,18 +59,18 @@ func main() {
|
|||||||
|
|
||||||
c := config.Instance()
|
c := config.Instance()
|
||||||
|
|
||||||
c.SetPort(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.RequireAuth(requireAuth)
|
c.RequireAuth = requireAuth
|
||||||
c.Username(username)
|
c.Username = username
|
||||||
c.Password(password)
|
c.Password = password
|
||||||
|
|
||||||
// if config file is found it will be merged with the current config struct
|
// 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")
|
log.Println(cli.BgRed, "config", cli.Reset, "no config file found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
var lock sync.Mutex
|
var lock sync.Mutex
|
||||||
|
|
||||||
type serverConfig struct {
|
type Config struct {
|
||||||
Port int `yaml:"port"`
|
Port int `yaml:"port"`
|
||||||
DownloadPath string `yaml:"downloadPath"`
|
DownloadPath string `yaml:"downloadPath"`
|
||||||
DownloaderPath string `yaml:"downloaderPath"`
|
DownloaderPath string `yaml:"downloaderPath"`
|
||||||
@@ -20,67 +20,27 @@ type serverConfig struct {
|
|||||||
SessionFilePath string `yaml:"session_file_path"`
|
SessionFilePath string `yaml:"session_file_path"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type config struct {
|
func (c *Config) LoadFile(filename string) error {
|
||||||
cfg serverConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *config) LoadFromFile(filename string) (serverConfig, error) {
|
|
||||||
fd, err := os.Open(filename)
|
fd, err := os.Open(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return serverConfig{}, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := yaml.NewDecoder(fd).Decode(&c.cfg); err != nil {
|
if err := yaml.NewDecoder(fd).Decode(c); err != nil {
|
||||||
return serverConfig{}, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.cfg, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *config) GetConfig() serverConfig {
|
var instance *Config
|
||||||
return c.cfg
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *config) SetPort(port int) {
|
func Instance() *Config {
|
||||||
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 {
|
|
||||||
if instance == nil {
|
if instance == nil {
|
||||||
lock.Lock()
|
lock.Lock()
|
||||||
defer lock.Unlock()
|
defer lock.Unlock()
|
||||||
if instance == nil {
|
if instance == nil {
|
||||||
instance = &config{serverConfig{}}
|
instance = &Config{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return instance
|
return instance
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ type ListRequest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ListDownloaded(w http.ResponseWriter, r *http.Request) {
|
func ListDownloaded(w http.ResponseWriter, r *http.Request) {
|
||||||
root := config.Instance().GetConfig().DownloadPath
|
root := config.Instance().DownloadPath
|
||||||
req := new(ListRequest)
|
req := new(ListRequest)
|
||||||
|
|
||||||
err := json.NewDecoder(r.Body).Decode(&req)
|
err := json.NewDecoder(r.Body).Decode(&req)
|
||||||
@@ -144,7 +144,7 @@ func SendFile(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
decodedStr := string(decoded)
|
decodedStr := string(decoded)
|
||||||
|
|
||||||
root := config.Instance().GetConfig().DownloadPath
|
root := config.Instance().DownloadPath
|
||||||
|
|
||||||
// TODO: further path / file validations
|
// TODO: further path / file validations
|
||||||
if strings.Contains(filepath.Dir(decodedStr), root) {
|
if strings.Contains(filepath.Dir(decodedStr), root) {
|
||||||
|
|||||||
@@ -25,8 +25,8 @@ func Login(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
username = config.Instance().GetConfig().Username
|
username = config.Instance().Username
|
||||||
password = config.Instance().GetConfig().Password
|
password = config.Instance().Password
|
||||||
)
|
)
|
||||||
|
|
||||||
if username != req.Username || password != req.Password {
|
if username != req.Username || password != req.Password {
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ func (m *MemoryDB) Persist() {
|
|||||||
running := m.All()
|
running := m.All()
|
||||||
|
|
||||||
sessionFile := filepath.Join(
|
sessionFile := filepath.Join(
|
||||||
config.Instance().GetConfig().SessionFilePath,
|
config.Instance().SessionFilePath,
|
||||||
"session.dat",
|
"session.dat",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ type MessageQueue struct {
|
|||||||
// CPU cores.
|
// CPU cores.
|
||||||
// The queue size can be set via the qs flag.
|
// The queue size can be set via the qs flag.
|
||||||
func NewMessageQueue() *MessageQueue {
|
func NewMessageQueue() *MessageQueue {
|
||||||
size := config.Instance().GetConfig().QueueSize
|
size := config.Instance().QueueSize
|
||||||
|
|
||||||
if size <= 0 {
|
if size <= 0 {
|
||||||
log.Fatalln("invalid queue size")
|
log.Fatalln("invalid queue size")
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ type metadata struct {
|
|||||||
|
|
||||||
func PlaylistDetect(req DownloadRequest, mq *MessageQueue, db *MemoryDB) error {
|
func PlaylistDetect(req DownloadRequest, mq *MessageQueue, db *MemoryDB) error {
|
||||||
var (
|
var (
|
||||||
downloader = config.Instance().GetConfig().DownloaderPath
|
downloader = config.Instance().DownloaderPath
|
||||||
cmd = exec.Command(downloader, req.URL, "-J")
|
cmd = exec.Command(downloader, req.URL, "-J")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ func (p *Process) Start() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
out := DownloadOutput{
|
out := DownloadOutput{
|
||||||
Path: config.Instance().GetConfig().DownloadPath,
|
Path: config.Instance().DownloadPath,
|
||||||
Filename: "%(title)s.%(ext)s",
|
Filename: "%(title)s.%(ext)s",
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,7 +97,7 @@ func (p *Process) Start() {
|
|||||||
}, p.Params...)
|
}, p.Params...)
|
||||||
|
|
||||||
// ----------------- main block ----------------- //
|
// ----------------- main block ----------------- //
|
||||||
cmd := exec.Command(config.Instance().GetConfig().DownloaderPath, params...)
|
cmd := exec.Command(config.Instance().DownloaderPath, params...)
|
||||||
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||||
|
|
||||||
r, err := cmd.StdoutPipe()
|
r, err := cmd.StdoutPipe()
|
||||||
@@ -192,7 +192,7 @@ func (p *Process) Kill() error {
|
|||||||
|
|
||||||
// Returns the available format for this URL
|
// Returns the available format for this URL
|
||||||
func (p *Process) GetFormatsSync() (DownloadFormats, error) {
|
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()
|
stdout, err := cmd.Output()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -245,7 +245,7 @@ func (p *Process) SetPending() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Process) SetMetadata() error {
|
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}
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||||
|
|
||||||
stdout, err := cmd.StdoutPipe()
|
stdout, err := cmd.StdoutPipe()
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import (
|
|||||||
|
|
||||||
func Authenticated(next http.Handler) http.Handler {
|
func Authenticated(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if !config.Instance().GetConfig().RequireAuth {
|
if !config.Instance().RequireAuth {
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import (
|
|||||||
// FreeSpace gets the available Bytes writable to download directory
|
// FreeSpace gets the available Bytes writable to download directory
|
||||||
func FreeSpace() (uint64, error) {
|
func FreeSpace() (uint64, error) {
|
||||||
var stat unix.Statfs_t
|
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
|
return (stat.Bavail * uint64(stat.Bsize)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,7 +26,7 @@ func DirectoryTree() (*[]string, error) {
|
|||||||
children []Node
|
children []Node
|
||||||
}
|
}
|
||||||
|
|
||||||
rootPath := config.Instance().GetConfig().DownloadPath
|
rootPath := config.Instance().DownloadPath
|
||||||
|
|
||||||
stack := internal.NewStack[Node]()
|
stack := internal.NewStack[Node]()
|
||||||
flattened := make([]string, 0)
|
flattened := make([]string, 0)
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
// Update using the builtin function of yt-dlp
|
// Update using the builtin function of yt-dlp
|
||||||
func UpdateExecutable() error {
|
func UpdateExecutable() error {
|
||||||
cmd := exec.Command(config.Instance().GetConfig().DownloaderPath, "-U")
|
cmd := exec.Command(config.Instance().DownloaderPath, "-U")
|
||||||
|
|
||||||
err := cmd.Start()
|
err := cmd.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user