visedriver/config/config.go

150 lines
3.5 KiB
Go
Raw Permalink Normal View History

2025-01-12 11:24:50 +01:00
package config
import (
"strings"
2025-01-19 19:26:39 +01:00
"git.defalsify.org/vise.git/logging"
2025-01-12 11:24:50 +01:00
"git.grassecon.net/grassrootseconomics/visedriver/env"
2025-01-19 19:26:39 +01:00
"git.grassecon.net/grassrootseconomics/visedriver/storage"
2025-01-12 11:24:50 +01:00
)
var (
2025-01-22 19:37:54 +01:00
logg = logging.NewVanilla().WithDomain("visedriver-config")
defaultLanguage = "eng"
languages []string
DefaultLanguage string
dbConn string
dbConnMissing bool
dbConnMode storage.DbMode
stateDbConn string
stateDbConnMode storage.DbMode
resourceDbConn string
resourceDbConnMode storage.DbMode
userDbConn string
userDbConnMode storage.DbMode
Languages []string
2025-01-12 11:24:50 +01:00
)
type Override struct {
DbConn string
2025-01-22 19:37:54 +01:00
DbConnMode storage.DbMode
StateConn string
2025-01-22 19:37:54 +01:00
StateConnMode storage.DbMode
ResourceConn string
2025-01-22 19:37:54 +01:00
ResourceConnMode storage.DbMode
UserConn string
2025-01-22 19:37:54 +01:00
UserConnMode storage.DbMode
}
2025-01-12 11:24:50 +01:00
func setLanguage() error {
defaultLanguage = env.GetEnv("DEFAULT_LANGUAGE", defaultLanguage)
languages = strings.Split(env.GetEnv("LANGUAGES", defaultLanguage), ",")
haveDefaultLanguage := false
2025-01-21 16:28:55 +01:00
for i, v := range languages {
2025-01-12 11:24:50 +01:00
languages[i] = strings.ReplaceAll(v, " ", "")
if languages[i] == defaultLanguage {
haveDefaultLanguage = true
}
}
if !haveDefaultLanguage {
languages = append([]string{defaultLanguage}, languages...)
}
return nil
}
func setConn() error {
2025-01-19 19:26:39 +01:00
dbConn = env.GetEnv("DB_CONN", "?")
stateDbConn = env.GetEnv("DB_CONN_STATE", dbConn)
resourceDbConn = env.GetEnv("DB_CONN_RESOURCE", dbConn)
userDbConn = env.GetEnv("DB_CONN_USER", dbConn)
2025-01-12 11:24:50 +01:00
return nil
}
func ApplyConn(override *Override) {
if override.DbConn != "?" {
dbConn = override.DbConn
stateDbConn = override.StateConn
resourceDbConn = override.ResourceConn
userDbConn = override.UserConn
2025-01-19 19:26:39 +01:00
}
2025-01-22 13:31:35 +01:00
dbConnMode = override.DbConnMode
if override.StateConn != "?" {
stateDbConn = override.StateConn
2025-01-19 19:26:39 +01:00
}
if override.ResourceConn != "?" {
resourceDbConn = override.ResourceConn
2025-01-19 19:26:39 +01:00
}
if override.UserConn != "?" {
userDbConn = override.UserConn
2025-01-19 19:26:39 +01:00
}
if dbConn == "?" {
dbConn = ""
}
if stateDbConn == "?" {
stateDbConn = dbConn
stateDbConnMode = dbConnMode
2025-01-19 19:26:39 +01:00
}
if resourceDbConn == "?" {
resourceDbConn = dbConn
resourceDbConnMode = dbConnMode
2025-01-19 19:26:39 +01:00
}
if userDbConn == "?" {
userDbConn = dbConn
2025-01-22 13:31:35 +01:00
userDbConnMode = dbConnMode
}
logg.Debugf("conns", "conn", dbConn, "user", userDbConn)
if override.DbConnMode != storage.DBMODE_ANY {
dbConnMode = override.DbConnMode
}
2025-01-22 13:31:35 +01:00
if override.StateConnMode != storage.DBMODE_ANY {
stateDbConnMode = override.StateConnMode
}
if override.ResourceConnMode != storage.DBMODE_ANY {
resourceDbConnMode = override.ResourceConnMode
}
if override.UserConnMode != storage.DBMODE_ANY {
userDbConnMode = override.UserConnMode
2025-01-19 19:26:39 +01:00
}
}
func GetConns() (storage.Conns, error) {
o := storage.NewConns()
c, err := storage.ToConnDataMode(stateDbConn, stateDbConnMode)
2025-01-19 19:26:39 +01:00
if err != nil {
return o, err
}
o.Set(c, storage.STORETYPE_STATE)
c, err = storage.ToConnDataMode(resourceDbConn, resourceDbConnMode)
2025-01-19 19:26:39 +01:00
if err != nil {
return o, err
}
o.Set(c, storage.STORETYPE_RESOURCE)
c, err = storage.ToConnDataMode(userDbConn, userDbConnMode)
2025-01-19 19:26:39 +01:00
if err != nil {
return o, err
}
o.Set(c, storage.STORETYPE_USER)
return o, nil
}
2025-01-12 11:24:50 +01:00
// LoadConfig initializes the configuration values after environment variables are loaded.
func LoadConfig() error {
err := setConn()
if err != nil {
return err
}
err = setLanguage()
if err != nil {
return err
}
DefaultLanguage = defaultLanguage
Languages = languages
return nil
}