fix(be): use DbConfig getter method instead of direct access to fields

This commit is contained in:
Denis Gukov 2023-01-28 03:25:25 +04:00
parent d6e1315db7
commit 668c81a991
4 changed files with 38 additions and 22 deletions

View File

@ -366,9 +366,9 @@ func getSystemInfo(w http.ResponseWriter, r *http.Request) {
"version": util.Version,
//"update": updateAvailable,
"config": map[string]string{
"dbHost": dbConfig.Hostname,
"dbName": dbConfig.DbName,
"dbUser": dbConfig.Username,
"dbHost": dbConfig.GetHostname(),
"dbName": dbConfig.GetDbName(),
"dbUser": dbConfig.GetUsername(),
"path": util.Config.TmpPath,
"cmdPath": util.FindSemaphore(),
},

View File

@ -96,7 +96,7 @@ func (d *BoltDb) Connect(token string) {
if err != nil {
panic(err)
}
filename = config.Hostname
filename = config.GetHostname()
} else {
filename = d.Filename
}

View File

@ -174,7 +174,7 @@ func createDb() error {
return err
}
_, err = conn.Exec("create database " + cfg.DbName)
_, err = conn.Exec("create database " + cfg.GetDbName())
if err != nil {
log.Warn(err.Error())

View File

@ -292,34 +292,50 @@ func (d DbDriver) String() string {
}
func (d *DbConfig) IsPresent() bool {
return d.Hostname != ""
return d.GetHostname() != ""
}
func (d *DbConfig) HasSupportMultipleDatabases() bool {
return true
}
func (d *DbConfig) GetConnectionString(includeDbName bool) (connectionString string, err error) {
func (d *DbConfig) GetDbName() string {
dbName := os.Getenv("SEMAPHORE_DB_NAME")
dbUser := os.Getenv("SEMAPHORE_DB_USER")
dbPass := os.Getenv("SEMAPHORE_DB_PASS")
dbHost := os.Getenv("SEMAPHORE_DB_HOST")
if dbUser == "" {
dbUser = d.Username
if dbName != "" {
return dbName
}
return d.DbName
}
if dbPass == "" {
dbPass = d.Password
func (d *DbConfig) GetUsername() string {
username := os.Getenv("SEMAPHORE_DB_USER")
if username != "" {
return username
}
return d.Username
}
if dbHost == "" {
dbHost = d.Hostname
func (d *DbConfig) GetPassword() string {
password := os.Getenv("SEMAPHORE_DB_PASS")
if password != "" {
return password
}
return d.Password
}
if dbName == "" {
dbName = d.DbName
func (d *DbConfig) GetHostname() string {
hostname := os.Getenv("SEMAPHORE_DB_HOST")
if hostname != "" {
return hostname
}
return d.Hostname
}
func (d *DbConfig) GetConnectionString(includeDbName bool) (connectionString string, err error) {
dbName := d.GetDbName()
dbUser := d.GetUsername()
dbPass := d.GetPassword()
dbHost := d.GetHostname()
switch d.Dialect {
case DbDriverBolt:
@ -376,11 +392,11 @@ func (conf *ConfigType) PrintDbInfo() {
}
switch dialect {
case DbDriverMySQL:
fmt.Printf("MySQL %v@%v %v\n", conf.MySQL.Username, conf.MySQL.Hostname, conf.MySQL.DbName)
fmt.Printf("MySQL %v@%v %v\n", conf.MySQL.GetUsername(), conf.MySQL.GetHostname(), conf.MySQL.GetDbName())
case DbDriverBolt:
fmt.Printf("BoltDB %v\n", conf.BoltDb.Hostname)
fmt.Printf("BoltDB %v\n", conf.BoltDb.GetHostname())
case DbDriverPostgres:
fmt.Printf("Postgres %v@%v %v\n", conf.Postgres.Username, conf.Postgres.Hostname, conf.Postgres.DbName)
fmt.Printf("Postgres %v@%v %v\n", conf.Postgres.GetUsername(), conf.Postgres.GetHostname(), conf.Postgres.GetDbName())
default:
panic(fmt.Errorf("database configuration not found"))
}