mirror of
https://github.com/drakkan/sftpgo.git
synced 2025-12-06 22:30:56 +03:00
committed by
Nicola Murino
parent
7eb5b01169
commit
29aadbf3e3
@@ -104,6 +104,12 @@ func GetProviderConf() dataprovider.Config {
|
|||||||
return globalConf.ProviderConf
|
return globalConf.ProviderConf
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getRedactedGlobalConf() globalConfig {
|
||||||
|
conf := globalConf
|
||||||
|
conf.ProviderConf.Password = "[redacted]"
|
||||||
|
return conf
|
||||||
|
}
|
||||||
|
|
||||||
// LoadConfig loads the configuration
|
// LoadConfig loads the configuration
|
||||||
// configDir will be added to the configuration search paths.
|
// configDir will be added to the configuration search paths.
|
||||||
// The search path contains by default the current directory and on linux it contains
|
// The search path contains by default the current directory and on linux it contains
|
||||||
@@ -116,13 +122,15 @@ func LoadConfig(configDir, configName string) error {
|
|||||||
viper.AddConfigPath(".")
|
viper.AddConfigPath(".")
|
||||||
viper.SetConfigName(configName)
|
viper.SetConfigName(configName)
|
||||||
if err = viper.ReadInConfig(); err != nil {
|
if err = viper.ReadInConfig(); err != nil {
|
||||||
logger.Warn(logSender, "", "error loading configuration file: %v. Default configuration will be used: %+v", err, globalConf)
|
logger.Warn(logSender, "", "error loading configuration file: %v. Default configuration will be used: %+v",
|
||||||
|
err, getRedactedGlobalConf())
|
||||||
logger.WarnToConsole("error loading configuration file: %v. Default configuration will be used.", err)
|
logger.WarnToConsole("error loading configuration file: %v. Default configuration will be used.", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = viper.Unmarshal(&globalConf)
|
err = viper.Unmarshal(&globalConf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warn(logSender, "", "error parsing configuration file: %v. Default configuration will be used: %+v", err, globalConf)
|
logger.Warn(logSender, "", "error parsing configuration file: %v. Default configuration will be used: %+v",
|
||||||
|
err, getRedactedGlobalConf())
|
||||||
logger.WarnToConsole("error parsing configuration file: %v. Default configuration will be used.", err)
|
logger.WarnToConsole("error parsing configuration file: %v. Default configuration will be used.", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -136,6 +144,6 @@ func LoadConfig(configDir, configName string) error {
|
|||||||
logger.Warn(logSender, "", "Configuration error: %v", err)
|
logger.Warn(logSender, "", "Configuration error: %v", err)
|
||||||
logger.WarnToConsole("Configuration error: %v", err)
|
logger.WarnToConsole("Configuration error: %v", err)
|
||||||
}
|
}
|
||||||
logger.Debug(logSender, "", "config file used: '%v', config loaded: %+v", viper.ConfigFileUsed(), globalConf)
|
logger.Debug(logSender, "", "config file used: '%v', config loaded: %+v", viper.ConfigFileUsed(), getRedactedGlobalConf())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,25 +15,34 @@ type MySQLProvider struct {
|
|||||||
|
|
||||||
func initializeMySQLProvider() error {
|
func initializeMySQLProvider() error {
|
||||||
var err error
|
var err error
|
||||||
var connectionString string
|
|
||||||
logSender = MySQLDataProviderName
|
logSender = MySQLDataProviderName
|
||||||
if len(config.ConnectionString) == 0 {
|
dbHandle, err := sql.Open("mysql", getMySQLConnectionString(false))
|
||||||
connectionString = fmt.Sprintf("%v:%v@tcp([%v]:%v)/%v?charset=utf8&interpolateParams=true&timeout=10s&tls=%v&writeTimeout=10s&readTimeout=10s",
|
|
||||||
config.Username, config.Password, config.Host, config.Port, config.Name, getSSLMode())
|
|
||||||
} else {
|
|
||||||
connectionString = config.ConnectionString
|
|
||||||
}
|
|
||||||
dbHandle, err := sql.Open("mysql", connectionString)
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
providerLog(logger.LevelDebug, "mysql database handle created, connection string: %#v, pool size: %v", connectionString, config.PoolSize)
|
providerLog(logger.LevelDebug, "mysql database handle created, connection string: %#v, pool size: %v",
|
||||||
|
getMySQLConnectionString(true), config.PoolSize)
|
||||||
dbHandle.SetMaxOpenConns(config.PoolSize)
|
dbHandle.SetMaxOpenConns(config.PoolSize)
|
||||||
dbHandle.SetConnMaxLifetime(1800 * time.Second)
|
dbHandle.SetConnMaxLifetime(1800 * time.Second)
|
||||||
provider = MySQLProvider{dbHandle: dbHandle}
|
provider = MySQLProvider{dbHandle: dbHandle}
|
||||||
} else {
|
} else {
|
||||||
providerLog(logger.LevelWarn, "error creating mysql database handler, connection string: %#v, error: %v", connectionString, err)
|
providerLog(logger.LevelWarn, "error creating mysql database handler, connection string: %#v, error: %v",
|
||||||
|
getMySQLConnectionString(true), err)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
func getMySQLConnectionString(redactedPwd bool) string {
|
||||||
|
var connectionString string
|
||||||
|
if len(config.ConnectionString) == 0 {
|
||||||
|
password := config.Password
|
||||||
|
if redactedPwd {
|
||||||
|
password = "[redacted]"
|
||||||
|
}
|
||||||
|
connectionString = fmt.Sprintf("%v:%v@tcp([%v]:%v)/%v?charset=utf8&interpolateParams=true&timeout=10s&tls=%v&writeTimeout=10s&readTimeout=10s",
|
||||||
|
config.Username, password, config.Host, config.Port, config.Name, getSSLMode())
|
||||||
|
} else {
|
||||||
|
connectionString = config.ConnectionString
|
||||||
|
}
|
||||||
|
return connectionString
|
||||||
|
}
|
||||||
|
|
||||||
func (p MySQLProvider) checkAvailability() error {
|
func (p MySQLProvider) checkAvailability() error {
|
||||||
return sqlCommonCheckAvailability(p.dbHandle)
|
return sqlCommonCheckAvailability(p.dbHandle)
|
||||||
|
|||||||
@@ -14,25 +14,35 @@ type PGSQLProvider struct {
|
|||||||
|
|
||||||
func initializePGSQLProvider() error {
|
func initializePGSQLProvider() error {
|
||||||
var err error
|
var err error
|
||||||
var connectionString string
|
|
||||||
logSender = PGSQLDataProviderName
|
logSender = PGSQLDataProviderName
|
||||||
if len(config.ConnectionString) == 0 {
|
dbHandle, err := sql.Open("postgres", getPGSQLConnectionString(false))
|
||||||
connectionString = fmt.Sprintf("host='%v' port=%v dbname='%v' user='%v' password='%v' sslmode=%v connect_timeout=10",
|
|
||||||
config.Host, config.Port, config.Name, config.Username, config.Password, getSSLMode())
|
|
||||||
} else {
|
|
||||||
connectionString = config.ConnectionString
|
|
||||||
}
|
|
||||||
dbHandle, err := sql.Open("postgres", connectionString)
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
providerLog(logger.LevelDebug, "postgres database handle created, connection string: %#v, pool size: %v", connectionString, config.PoolSize)
|
providerLog(logger.LevelDebug, "postgres database handle created, connection string: %#v, pool size: %v",
|
||||||
|
getPGSQLConnectionString(true), config.PoolSize)
|
||||||
dbHandle.SetMaxOpenConns(config.PoolSize)
|
dbHandle.SetMaxOpenConns(config.PoolSize)
|
||||||
provider = PGSQLProvider{dbHandle: dbHandle}
|
provider = PGSQLProvider{dbHandle: dbHandle}
|
||||||
} else {
|
} else {
|
||||||
providerLog(logger.LevelWarn, "error creating postgres database handler, connection string: %#v, error: %v", connectionString, err)
|
providerLog(logger.LevelWarn, "error creating postgres database handler, connection string: %#v, error: %v",
|
||||||
|
getPGSQLConnectionString(true), err)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getPGSQLConnectionString(redactedPwd bool) string {
|
||||||
|
var connectionString string
|
||||||
|
if len(config.ConnectionString) == 0 {
|
||||||
|
password := config.Password
|
||||||
|
if redactedPwd {
|
||||||
|
password = "[redacted]"
|
||||||
|
}
|
||||||
|
connectionString = fmt.Sprintf("host='%v' port=%v dbname='%v' user='%v' password='%v' sslmode=%v connect_timeout=10",
|
||||||
|
config.Host, config.Port, config.Name, config.Username, password, getSSLMode())
|
||||||
|
} else {
|
||||||
|
connectionString = config.ConnectionString
|
||||||
|
}
|
||||||
|
return connectionString
|
||||||
|
}
|
||||||
|
|
||||||
func (p PGSQLProvider) checkAvailability() error {
|
func (p PGSQLProvider) checkAvailability() error {
|
||||||
return sqlCommonCheckAvailability(p.dbHandle)
|
return sqlCommonCheckAvailability(p.dbHandle)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user