mirror of
https://github.com/Theodor-Springmann-Stiftung/musenalm.git
synced 2025-12-16 06:15:29 +00:00
Allow for setting a testuser via settings
This commit is contained in:
@@ -12,35 +12,50 @@ import (
|
||||
// WARNING: this is not intended to be used in a multi-threaded environment
|
||||
// Instatiate this once on startup before any goroutines are started
|
||||
const (
|
||||
DEFAULT_GIT_DIR = "data_git"
|
||||
DEFAULT_GND_DIR = "cache_gnd"
|
||||
DEFAULT_GEO_DIR = "cache_geo"
|
||||
DEFAULT_IMG_DIR = "data_bilder"
|
||||
|
||||
DEFAULT_PORT = "8080"
|
||||
DEFAULT_ADDR = "localhost"
|
||||
DEFAULT_HTTPS = false
|
||||
|
||||
ENV_PREFIX = "KGPZ"
|
||||
ENV_PREFIX = "MUSENALM"
|
||||
)
|
||||
|
||||
type ConfigProvider struct {
|
||||
Files []string
|
||||
Files []string
|
||||
DevFiles []string
|
||||
*Config
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
// At least one of these should be set
|
||||
Debug bool `json:"debug,omitempty" envconfig:"DEBUG"`
|
||||
AllowTestLogin bool `json:"allow_test_login,omitempty" envconfig:"ALLOW_TEST_LOGIN"`
|
||||
}
|
||||
|
||||
func NewConfigProvider(files []string) *ConfigProvider {
|
||||
return &ConfigProvider{Files: files}
|
||||
func NewConfigProvider(files []string, devfiles []string) *ConfigProvider {
|
||||
return &ConfigProvider{Files: files, DevFiles: devfiles}
|
||||
}
|
||||
|
||||
func (c *ConfigProvider) Read() error {
|
||||
c.Config = &Config{}
|
||||
|
||||
for _, file := range c.Files {
|
||||
c.Config = readSettingsFile(c.Config, file)
|
||||
conf, err := readSettingsFile(file)
|
||||
if err == nil {
|
||||
c.Config = conf
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, file := range c.DevFiles {
|
||||
conf, err := readSettingsFile(file)
|
||||
if c.Debug {
|
||||
if err == nil {
|
||||
c.Config = conf
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
c.Config = readSettingsEnv(c.Config)
|
||||
c.Config = readDefaults(c.Config)
|
||||
@@ -51,11 +66,12 @@ func (c *ConfigProvider) Validate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func readSettingsFile(cfg *Config, path string) *Config {
|
||||
func readSettingsFile(path string) (*Config, error) {
|
||||
cfg := &Config{}
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
slog.Error("Error opening config file ", "path", path, "error", err)
|
||||
return cfg
|
||||
return cfg, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
@@ -63,7 +79,7 @@ func readSettingsFile(cfg *Config, path string) *Config {
|
||||
err = dec.Decode(cfg)
|
||||
helpers.Assert(err, "Error decoding config file")
|
||||
|
||||
return cfg
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func readSettingsEnv(cfg *Config) *Config {
|
||||
@@ -72,7 +88,6 @@ func readSettingsEnv(cfg *Config) *Config {
|
||||
}
|
||||
|
||||
func readDefaults(cfg *Config) *Config {
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user