mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 00:39:13 +00:00
feat(confg): reload on save and refactor logging (#275)
* feat(confg): reload on save * refactor(logging): rework
This commit is contained in:
parent
198528a474
commit
91b094f4f4
56 changed files with 995 additions and 873 deletions
|
@ -6,32 +6,15 @@ import (
|
|||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/internal/logger"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var Config domain.Config
|
||||
|
||||
func Defaults() domain.Config {
|
||||
return domain.Config{
|
||||
Host: "localhost",
|
||||
Port: 7474,
|
||||
LogLevel: "TRACE",
|
||||
LogPath: "",
|
||||
BaseURL: "/",
|
||||
SessionSecret: "secret-session-key",
|
||||
CustomDefinitions: "",
|
||||
DatabaseType: "sqlite",
|
||||
PostgresHost: "",
|
||||
PostgresPort: 0,
|
||||
PostgresDatabase: "",
|
||||
PostgresUser: "",
|
||||
PostgresPass: "",
|
||||
}
|
||||
}
|
||||
|
||||
func writeConfig(configPath string, configFile string) error {
|
||||
path := filepath.Join(configPath, configFile)
|
||||
|
||||
|
@ -108,9 +91,45 @@ sessionSecret = "secret-session-key"`)
|
|||
return nil
|
||||
}
|
||||
|
||||
func Read(configPath string) domain.Config {
|
||||
config := Defaults()
|
||||
type Config interface {
|
||||
DynamicReload(log logger.Logger)
|
||||
}
|
||||
|
||||
type AppConfig struct {
|
||||
Config *domain.Config
|
||||
m sync.Mutex
|
||||
}
|
||||
|
||||
func New(configPath string, version string) *AppConfig {
|
||||
c := &AppConfig{}
|
||||
c.defaults()
|
||||
c.Config.Version = version
|
||||
|
||||
c.load(configPath)
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *AppConfig) defaults() {
|
||||
c.Config = &domain.Config{
|
||||
Version: "dev",
|
||||
Host: "localhost",
|
||||
Port: 7474,
|
||||
LogLevel: "TRACE",
|
||||
LogPath: "",
|
||||
BaseURL: "/",
|
||||
SessionSecret: "secret-session-key",
|
||||
CustomDefinitions: "",
|
||||
DatabaseType: "sqlite",
|
||||
PostgresHost: "",
|
||||
PostgresPort: 0,
|
||||
PostgresDatabase: "",
|
||||
PostgresUser: "",
|
||||
PostgresPass: "",
|
||||
}
|
||||
}
|
||||
|
||||
func (c *AppConfig) load(configPath string) {
|
||||
// or use viper.SetDefault(val, def)
|
||||
//viper.SetDefault("host", config.Host)
|
||||
//viper.SetDefault("port", config.Port)
|
||||
|
@ -133,7 +152,6 @@ func Read(configPath string) domain.Config {
|
|||
}
|
||||
|
||||
viper.SetConfigFile(path.Join(configPath, "config.toml"))
|
||||
config.ConfigPath = configPath
|
||||
} else {
|
||||
viper.SetConfigName("config")
|
||||
|
||||
|
@ -148,11 +166,27 @@ func Read(configPath string) domain.Config {
|
|||
log.Printf("config read error: %q", err)
|
||||
}
|
||||
|
||||
if err := viper.Unmarshal(&config); err != nil {
|
||||
if err := viper.Unmarshal(&c.Config); err != nil {
|
||||
log.Fatalf("Could not unmarshal config file: %v", viper.ConfigFileUsed())
|
||||
}
|
||||
|
||||
Config = config
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
func (c *AppConfig) DynamicReload(log logger.Logger) {
|
||||
viper.OnConfigChange(func(e fsnotify.Event) {
|
||||
c.m.Lock()
|
||||
|
||||
logLevel := viper.GetString("logLevel")
|
||||
c.Config.LogLevel = logLevel
|
||||
log.SetLogLevel(c.Config.LogLevel)
|
||||
|
||||
logPath := viper.GetString("logPath")
|
||||
c.Config.LogPath = logPath
|
||||
|
||||
log.Debug().Msg("config file reloaded!")
|
||||
|
||||
c.m.Unlock()
|
||||
})
|
||||
viper.WatchConfig()
|
||||
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue