Set up multiple conns in config

This commit is contained in:
lash 2025-01-19 18:26:39 +00:00
parent 9b3dad579b
commit 60ff1b0ab3
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
2 changed files with 63 additions and 14 deletions

View File

@ -3,20 +3,21 @@ package config
import ( import (
"strings" "strings"
"git.defalsify.org/vise.git/logging"
"git.grassecon.net/grassrootseconomics/visedriver/env" "git.grassecon.net/grassrootseconomics/visedriver/env"
"git.grassecon.net/grassrootseconomics/visedriver/storage"
) )
var ( var (
logg = logging.NewVanilla().WithDomain("visedriver-config")
defaultLanguage = "eng" defaultLanguage = "eng"
languages []string languages []string
)
var (
DbConn string
ResourceDbConn string
UserDbConn string
StateDbConn string
DefaultLanguage string DefaultLanguage string
dbConn string
dbConnMissing bool
stateDbConn string
resourceDbConn string
userDbConn string
Languages []string Languages []string
) )
@ -38,16 +39,64 @@ func setLanguage() error {
return nil return nil
} }
func setConn() error { func setConn() error {
DbConn = env.GetEnv("DB_CONN", "") dbConn = env.GetEnv("DB_CONN", "?")
UserDbConn = env.GetEnv("DB_CONN_USER", DbConn) stateDbConn = env.GetEnv("DB_CONN_STATE", dbConn)
StateDbConn = env.GetEnv("DB_CONN_STATE", DbConn) resourceDbConn = env.GetEnv("DB_CONN_RESOURCE", dbConn)
ResourceDbConn = env.GetEnv("DB_CONN_RESOURCE", DbConn) userDbConn = env.GetEnv("DB_CONN_USER", dbConn)
return nil return nil
} }
func ApplyConn(connStr *string, stateConnStr *string, resourceConnStr *string, userConnStr *string) {
if connStr != nil {
dbConn = *connStr
}
if stateConnStr != nil {
stateDbConn = *stateConnStr
}
if resourceConnStr != nil {
resourceDbConn = *resourceConnStr
}
if userConnStr != nil {
userDbConn = *userConnStr
}
if dbConn == "?" {
logg.Warnf("no db connection found, using memdb for everything")
dbConn = ""
}
if stateDbConn == "?" {
stateDbConn = dbConn
}
if resourceDbConn == "?" {
resourceDbConn = dbConn
}
if userDbConn == "?" {
userDbConn = dbConn
}
}
func GetConns() (storage.Conns, error) {
o := storage.NewConns()
c, err := storage.ToConnData(stateDbConn)
if err != nil {
return o, err
}
o.Set(c, storage.STORETYPE_STATE)
c, err = storage.ToConnData(resourceDbConn)
if err != nil {
return o, err
}
o.Set(c, storage.STORETYPE_RESOURCE)
c, err = storage.ToConnData(userDbConn)
if err != nil {
return o, err
}
o.Set(c, storage.STORETYPE_USER)
return o, nil
}
// LoadConfig initializes the configuration values after environment variables are loaded. // LoadConfig initializes the configuration values after environment variables are loaded.
func LoadConfig() error { func LoadConfig() error {
err := setConn() err := setConn()

View File

@ -27,7 +27,7 @@ func NewConns() Conns {
return c return c
} }
func (c Conns) Set(typ int8, conn ConnData) { func (c Conns) Set(conn ConnData, typ int8) {
if typ < 0 || typ >= _STORETYPE_MAX { if typ < 0 || typ >= _STORETYPE_MAX {
panic(fmt.Errorf("invalid store type: %d", typ)) panic(fmt.Errorf("invalid store type: %d", typ))
} }