✨ 优化 MySQL 连接字符串生成规则(方便后面 OAuth2 复用)
This commit is contained in:
parent
77225e8192
commit
c169f99b61
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
.idea
|
.idea
|
||||||
vendor
|
vendor
|
||||||
logs
|
logs
|
||||||
|
*.db
|
@ -1,5 +1,7 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
// MySQL配置
|
// MySQL配置
|
||||||
type mysqlConfig struct {
|
type mysqlConfig struct {
|
||||||
Host string // 主机
|
Host string // 主机
|
||||||
@ -25,3 +27,9 @@ func InitMySQLConfig() {
|
|||||||
DbName: dbName,
|
DbName: dbName,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetDSN 返回 MySQL 连接字符串
|
||||||
|
func (c mysqlConfig) GetDSN() string {
|
||||||
|
return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
||||||
|
c.Username, c.Password, c.Host, c.Port, c.DbName)
|
||||||
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package global
|
package global
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"gorm.io/driver/mysql"
|
"gorm.io/driver/mysql"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"gorm.io/gorm/logger"
|
"gorm.io/gorm/logger"
|
||||||
@ -25,9 +24,7 @@ func InitMySQLClient() {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
url := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
conn, err := gorm.Open(mysql.Open(MySQLConfig.GetDSN()), &gorm.Config{Logger: newLogger})
|
||||||
MySQLConfig.Username, MySQLConfig.Password, MySQLConfig.Host, MySQLConfig.Port, MySQLConfig.DbName)
|
|
||||||
conn, err := gorm.Open(mysql.Open(url), &gorm.Config{Logger: newLogger})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Log.Panicf("初始化MySQL连接失败, 错误信息: %v", err)
|
Log.Panicf("初始化MySQL连接失败, 错误信息: %v", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user