optimize config
This commit is contained in:
parent
32d791051b
commit
2f869861ef
18
gorm.go
18
gorm.go
@ -38,8 +38,8 @@ type Config struct {
|
||||
ExtraParameters string `toml:"extra_parameters"` //数据库连接扩展参数
|
||||
}
|
||||
|
||||
func NewDB(dbcfg *Config) (*gorm.DB, error) {
|
||||
return open(dbcfg)
|
||||
func NewDB(dbcfg Config) (*gorm.DB, error) {
|
||||
return open(&dbcfg)
|
||||
}
|
||||
|
||||
func NewSession(db *gorm.DB) *gorm.DB {
|
||||
@ -67,7 +67,7 @@ func (dbcfg *Config) Id() string {
|
||||
return dbcfg.id
|
||||
}
|
||||
|
||||
func GetDsn(dbcfg *Config) (dsn string) {
|
||||
func (dbcfg *Config) GetDsn() (dsn string) {
|
||||
switch dbcfg.Type {
|
||||
case "mysql":
|
||||
//dsn := "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
|
||||
@ -97,11 +97,21 @@ func GetDsn(dbcfg *Config) (dsn string) {
|
||||
return dsn
|
||||
}
|
||||
|
||||
func (dbcfg Config) GetSecDsn() string {
|
||||
dsn := dbcfg.GetDsn()
|
||||
if len(dbcfg.Password) > 5 {
|
||||
p, l := dbcfg.Password, len(dbcfg.Password)
|
||||
return strings.Replace(dsn, p[2:l-3], "*****", 1)
|
||||
} else {
|
||||
return dsn
|
||||
}
|
||||
}
|
||||
|
||||
func open(dbcfg *Config) (*gorm.DB, error) {
|
||||
var (
|
||||
db *gorm.DB
|
||||
err error
|
||||
dsn = GetDsn(dbcfg)
|
||||
dsn = dbcfg.GetDsn()
|
||||
cacheValue, inCacheYes = _dbCache.Load(dbcfg.Id())
|
||||
cachedDb, inCacheYes2 = cacheValue.(*gorm.DB)
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user