8000 feat: support customize dsn for mysql and pg (#6031) · anwen-anyi/alist@1f83550 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 1f83550

Browse files
Mars160xhofe
andauthored
feat: support customize dsn for mysql and pg (AlistGo#6031)
* support for unixsocket to connect to mysql * feat: customize dsn for mysql and pg --------- Co-authored-by: Andy Hsu <i@nn.ci>
1 parent 424ab2d commit 1f83550

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

internal/bootstrap/db.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,21 @@ func InitDB() {
5656
}
5757
case "mysql":
5858
{
59+
//[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN]
5960
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local&tls=%s",
6061
database.User, database.Password, database.Host, database.Port, database.Name, database.SSLMode)
62+
if database.DSN != "" {
63+
dsn = database.DSN
64+
}
6165
dB, err = gorm.Open(mysql.Open(dsn), gormConfig)
6266
}
6367
case "postgres":
6468
{
6569
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=Asia/Shanghai",
6670
database.Host, database.User, database.Password, database.Name, database.Port, database.SSLMode)
71+
if database.DSN != "" {
72+
dsn = database.DSN
73+
}
6774
dB, err = gorm.Open(postgres.Open(dsn), gormConfig)
6875
}
6976
default:

internal/conf/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Database struct {
1717
DBFile string `json:"db_file" env:"FILE"`
1818
TablePrefix string `json:"table_prefix" env:"TABLE_PREFIX"`
1919
SSLMode string `json:"ssl_mode" env:"SSL_MODE"`
20+
DSN string `json:"dsn" env:"DSN"`
2021
}
2122

2223
type Scheme struct {

0 commit comments

Comments
 (0)
0