1 Star 2 Fork 3

kristas/booting-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
gorm_facade_client.go 1.05 KB
一键复制 编辑 原始数据 按行查看 历史
kristas 提交于 2021-03-23 17:36 . feat: add cache framework
package mysql
import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
. "gorm.io/gorm/logger"
"strings"
"time"
)
type GormInstance struct {
DB *gorm.DB
Config DataSource
}
func InitGormDatasource(datasource []DataSource, isUseSSH bool, loggingLevel string) []GormInstance {
dbCh := make(chan DBInstance, len(datasource))
InitDatasource(datasource, isUseSSH, dbCh)
var gormInstance []GormInstance
for ins := range dbCh {
ins.DB.SetConnMaxLifetime(time.Second * 120)
gormDB, err := gorm.Open(mysql.New(mysql.Config{
Conn: ins.DB,
}), &gorm.Config{
Logger: Default.LogMode(gormLogLevelAdapter(loggingLevel)),
})
if err != nil {
log.Error("Failed to facade db to gormDB, caused by:", err)
}else {
gormInstance = append(gormInstance, GormInstance{
DB: gormDB,
Config: ins.Config,
})
}
}
return gormInstance
}
func gormLogLevelAdapter(level string) LogLevel {
switch strings.ToLower(level) {
case "panic":
return Silent
case "fatal", "error":
return Error
case "warn", "warning":
return Warn
default:
return Info
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/kristas/booting-go.git
git@gitee.com:kristas/booting-go.git
kristas
booting-go
booting-go
v1.1.3

搜索帮助