* testing message queue * better mq syncronisation * major code refactoring, concern separation. * bugfix * code refactoring * queuesize configurable via flags * code refactoring * comments * code refactoring, updated readme
76 lines
1.3 KiB
Go
76 lines
1.3 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"sync"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
var lock sync.Mutex
|
|
|
|
type serverConfig struct {
|
|
Port int `yaml:"port"`
|
|
DownloadPath string `yaml:"downloadPath"`
|
|
DownloaderPath string `yaml:"downloaderPath"`
|
|
RequireAuth bool `yaml:"require_auth"`
|
|
RPCSecret string `yaml:"rpc_secret"`
|
|
QueueSize int `yaml:"queue_size"`
|
|
}
|
|
|
|
type config struct {
|
|
cfg serverConfig
|
|
}
|
|
|
|
func (c *config) LoadFromFile(filename string) (serverConfig, error) {
|
|
bytes, err := os.ReadFile(filename)
|
|
if err != nil {
|
|
return serverConfig{}, err
|
|
}
|
|
|
|
yaml.Unmarshal(bytes, &c.cfg)
|
|
|
|
return c.cfg, nil
|
|
}
|
|
|
|
func (c *config) GetConfig() serverConfig {
|
|
return c.cfg
|
|
}
|
|
|
|
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) RPCSecret(secret string) {
|
|
c.cfg.RPCSecret = secret
|
|
}
|
|
|
|
func (c *config) QueueSize(size int) {
|
|
c.cfg.QueueSize = size
|
|
}
|
|
|
|
var instance *config
|
|
|
|
func Instance() *config {
|
|
if instance == nil {
|
|
lock.Lock()
|
|
defer lock.Unlock()
|
|
if instance == nil {
|
|
instance = &config{serverConfig{}}
|
|
}
|
|
}
|
|
return instance
|
|
}
|