package config import ( "io" "log" "os" "github.com/BurntSushi/toml" ) type DbConfig struct { Dialect string `toml:"dialect"` Username string `toml:"username"` PasswordSecret string `toml:"password-secret"` Url string `toml:"url"` Port string `toml:"port"` Name string `toml:"name"` } type StackConfiguration struct { ConfigLoaded bool AllowFreshAdminGeneration bool `toml:"gen-fresh-admin"` AdminEmails []string `toml:"admin-emails"` AdminHmacEnv string `toml:"admin-hmac-env"` UserHmacEnv string `toml:"user-hmac-env"` AuthedRateLimitConfig string `toml:"auth-rate-limit-defs"` UnauthedRateLimitConfig string `toml:"unauth-rate-limit-defs"` Db DbConfig `toml:"db"` } var Environment = os.Getenv("STACK_ENVIRONMENT") var configInternal = StackConfiguration{} func Config() StackConfiguration { return configInternal } func GetConfigPath(filename string) string { if Environment == "" { Environment = "dev" } return "config/" + Environment + "/" + filename } func LoadConfig() { file, err := os.Open(GetConfigPath("conf.toml")) if err != nil { panic(err) } defer file.Close() b, err := io.ReadAll(file) if err != nil { panic(err) } err = toml.Unmarshal(b, &configInternal) if err != nil { panic(err) } configInternal.ConfigLoaded = true log.Printf("Loaded Config for stack '%s':\n%+v\n", Environment, configInternal) }