完成基础脚手架

This commit is contained in:
LeeX
2022-05-09 09:00:14 +08:00
parent 632c76dfa6
commit 5fd8e0a694
37 changed files with 2867 additions and 0 deletions

31
client/mysql.go Normal file
View File

@@ -0,0 +1,31 @@
package client
import (
"ginDemo/config"
"gitee.ltd/lxh/logger"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var MySQL *gorm.DB
func InitMySQLClient() {
// 创建连接对象
mysqlConfig := mysql.Config{
DSN: config.Scd.MySQL.GetDSN(),
DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式
DontSupportRenameColumn: true, // 用 `change` 重命名列
}
conn, err := gorm.Open(mysql.New(mysqlConfig), &gorm.Config{Logger: logger.DefaultGormLogger()})
if err != nil {
logger.Say.Panic("初始化MySQL连接失败, 错误信息: %v", err)
} else {
logger.Say.Debug("MySQL连接成功")
}
MySQL = conn
//db, err := conn.DB()
//db.SetMaxIdleConns(10) // 用于设置连接池中空闲连接的最大数量
//db.SetMaxOpenConns(100) // 用于设置数据库连接的最大打开数量
//db.SetConnMaxLifetime(time.Hour) // 设置连接的最大存活时间
}

26
client/redis.go Normal file
View File

@@ -0,0 +1,26 @@
package client
import (
"context"
"ginDemo/config"
"gitee.ltd/lxh/logger"
"github.com/go-redis/redis/v8"
)
var Redis *redis.Client
func InitRedisClient() {
conf := config.Scd.Redis
// 初始化连接
conn := redis.NewClient(&redis.Options{
Addr: conf.GetDSN(),
Password: conf.Password,
DB: conf.Db,
})
if err := conn.Ping(context.Background()).Err(); err != nil {
logger.Say.Panicf("Redis连接初始化失败: %v", err)
} else {
logger.Say.Debug("Redis连接初始化成功")
}
Redis = conn
}