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