songzhibin97
3 years ago
8 changed files with 132 additions and 2 deletions
-
20server/config.yaml
-
1server/config/config.go
-
21server/config/db_list.go
-
22server/global/global.go
-
24server/initialize/db_list.go
-
21server/initialize/gorm_mysql.go
-
20server/initialize/gorm_pgsql.go
-
1server/main.go
@ -0,0 +1,21 @@ |
|||||
|
package config |
||||
|
|
||||
|
type DB struct { |
||||
|
Disable bool `mapstructure:"disable" json:"disable" yaml:"disable"` |
||||
|
Type string `mapstructure:"type" json:"type" yaml:"type"` |
||||
|
AliasName string `mapstructure:"alias-name" json:"alias-name" yaml:"alias-name"` |
||||
|
Path string `mapstructure:"path" json:"path" yaml:"path"` // 服务器地址:端口
|
||||
|
Port string `mapstructure:"port" json:"port" yaml:"port"` //:端口
|
||||
|
Config string `mapstructure:"config" json:"config" yaml:"config"` // 高级配置
|
||||
|
Dbname string `mapstructure:"db-name" json:"dbname" yaml:"db-name"` // 数据库名
|
||||
|
Username string `mapstructure:"username" json:"username" yaml:"username"` // 数据库用户名
|
||||
|
Password string `mapstructure:"password" json:"password" yaml:"password"` // 数据库密码
|
||||
|
MaxIdleConns int `mapstructure:"max-idle-conns" json:"maxIdleConns" yaml:"max-idle-conns"` // 空闲中的最大连接数
|
||||
|
MaxOpenConns int `mapstructure:"max-open-conns" json:"maxOpenConns" yaml:"max-open-conns"` // 打开到数据库的最大连接数
|
||||
|
LogMode string `mapstructure:"log-mode" json:"logMode" yaml:"log-mode"` // 是否开启Gorm全局日志
|
||||
|
LogZap bool `mapstructure:"log-zap" json:"logZap" yaml:"log-zap"` |
||||
|
} |
||||
|
|
||||
|
func (m *DB) Dsn() string { |
||||
|
return m.Username + ":" + m.Password + "@tcp(" + m.Path + ":" + m.Port + ")/" + m.Dbname + "?" + m.Config |
||||
|
} |
@ -0,0 +1,24 @@ |
|||||
|
package initialize |
||||
|
|
||||
|
import ( |
||||
|
"github.com/flipped-aurora/gin-vue-admin/server/global" |
||||
|
"gorm.io/gorm" |
||||
|
) |
||||
|
|
||||
|
func DBList() { |
||||
|
dbMap := make(map[string]*gorm.DB) |
||||
|
for _, info := range global.GVA_CONFIG.DBList { |
||||
|
if info.Disable { |
||||
|
continue |
||||
|
} |
||||
|
switch info.Type { |
||||
|
case "mysql": |
||||
|
dbMap[info.Dbname] = GormMysqlByConfig(info) |
||||
|
case "pgsql": |
||||
|
dbMap[info.Dbname] = GormPgSqlByConfig(info) |
||||
|
default: |
||||
|
continue |
||||
|
} |
||||
|
} |
||||
|
global.GVA_DBList = dbMap |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue