mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
feat(config): allow config environment variables (#691)
* feat(config): allow config environment variables * generate secure config default token * fix: read env vars * fix: go mod tidy * fix: revert default port change * fix: do not ignore default config values --------- Co-authored-by: ze0s <ze0s@riseup.net>
This commit is contained in:
parent
fdc957c571
commit
2ce82cc687
3 changed files with 42 additions and 35 deletions
|
@ -86,7 +86,7 @@ checkForUpdates = true
|
|||
sessionSecret = "{{ .sessionSecret }}"
|
||||
`
|
||||
|
||||
func writeConfig(configPath string, configFile string) error {
|
||||
func (c *AppConfig) writeConfig(configPath string, configFile string) error {
|
||||
cfgPath := filepath.Join(configPath, configFile)
|
||||
|
||||
// check if configPath exists, if not create it
|
||||
|
@ -132,9 +132,6 @@ func writeConfig(configPath string, configFile string) error {
|
|||
}
|
||||
defer f.Close()
|
||||
|
||||
// generate default sessionSecret
|
||||
sessionSecret := api.GenerateSecureToken(16)
|
||||
|
||||
// setup text template to inject variables into
|
||||
tmpl, err := template.New("config").Parse(configTemplate)
|
||||
if err != nil {
|
||||
|
@ -143,7 +140,7 @@ func writeConfig(configPath string, configFile string) error {
|
|||
|
||||
tmplVars := map[string]string{
|
||||
"host": host,
|
||||
"sessionSecret": sessionSecret,
|
||||
"sessionSecret": c.Config.SessionSecret,
|
||||
}
|
||||
|
||||
var buffer bytes.Buffer
|
||||
|
@ -193,7 +190,7 @@ func (c *AppConfig) defaults() {
|
|||
LogMaxSize: 50,
|
||||
LogMaxBackups: 3,
|
||||
BaseURL: "/",
|
||||
SessionSecret: "secret-session-key",
|
||||
SessionSecret: api.GenerateSecureToken(16),
|
||||
CustomDefinitions: "",
|
||||
CheckForUpdates: true,
|
||||
DatabaseType: "sqlite",
|
||||
|
@ -203,6 +200,7 @@ func (c *AppConfig) defaults() {
|
|||
PostgresUser: "",
|
||||
PostgresPass: "",
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (c *AppConfig) load(configPath string) {
|
||||
|
@ -222,7 +220,7 @@ func (c *AppConfig) load(configPath string) {
|
|||
|
||||
// check if path and file exists
|
||||
// if not, create path and file
|
||||
if err := writeConfig(configPath, "config.toml"); err != nil {
|
||||
if err := c.writeConfig(configPath, "config.toml"); err != nil {
|
||||
log.Printf("write error: %q", err)
|
||||
}
|
||||
|
||||
|
@ -236,12 +234,22 @@ func (c *AppConfig) load(configPath string) {
|
|||
viper.AddConfigPath("$HOME/.autobrr")
|
||||
}
|
||||
|
||||
viper.SetEnvPrefix("AUTOBRR")
|
||||
|
||||
// read config
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
log.Printf("config read error: %q", err)
|
||||
}
|
||||
|
||||
if err := viper.Unmarshal(&c.Config); err != nil {
|
||||
for _, key := range viper.AllKeys() {
|
||||
envKey := strings.ToUpper(strings.ReplaceAll(key, ".", "_"))
|
||||
err := viper.BindEnv(key, "AUTOBRR_"+envKey)
|
||||
if err != nil {
|
||||
log.Fatal("config: unable to bind env: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
if err := viper.Unmarshal(c.Config); err != nil {
|
||||
log.Fatalf("Could not unmarshal config file: %v", viper.ConfigFileUsed())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue