mirror of https://github.com/Xhofe/alist
feat: support customize dsn for mysql and pg (#6031)
* support for unixsocket to connect to mysql * feat: customize dsn for mysql and pg --------- Co-authored-by: Andy Hsu <i@nn.ci>pull/6081/head
parent
424ab2d0c0
commit
1f835502ba
|
@ -56,14 +56,21 @@ func InitDB() {
|
|||
}
|
||||
case "mysql":
|
||||
{
|
||||
//[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...¶mN=valueN]
|
||||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local&tls=%s",
|
||||
database.User, database.Password, database.Host, database.Port, database.Name, database.SSLMode)
|
||||
if database.DSN != "" {
|
||||
dsn = database.DSN
|
||||
}
|
||||
dB, err = gorm.Open(mysql.Open(dsn), gormConfig)
|
||||
}
|
||||
case "postgres":
|
||||
{
|
||||
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=Asia/Shanghai",
|
||||
database.Host, database.User, database.Password, database.Name, database.Port, database.SSLMode)
|
||||
if database.DSN != "" {
|
||||
dsn = database.DSN
|
||||
}
|
||||
dB, err = gorm.Open(postgres.Open(dsn), gormConfig)
|
||||
}
|
||||
default:
|
||||
|
|
|
@ -17,6 +17,7 @@ type Database struct {
|
|||
DBFile string `json:"db_file" env:"FILE"`
|
||||
TablePrefix string `json:"table_prefix" env:"TABLE_PREFIX"`
|
||||
SSLMode string `json:"ssl_mode" env:"SSL_MODE"`
|
||||
DSN string `json:"dsn" env:"DSN"`
|
||||
}
|
||||
|
||||
type Scheme struct {
|
||||
|
|
Loading…
Reference in New Issue