- 新增用户与上传错误码文件及 Recovery 中间件 - 修复 router.go 导入与中间件链,注册用户路由并补 handler 错误码导入 - 补充用户状态常量与 UserGetByWechatOpenID 查询,修订刷新令牌 dto - 移除 upload 包 OTel 调用,新增 FileUploadCos 上传封装 - 实现 UserService 微信登录/刷新令牌/我的信息/改资料/上传头像 - 裁剪 main.go 配置段并移除 OTel 桥接,补启动与优雅退出 - 新增 configs 示例配置、global 配置结构及 go.mod 依赖
55 lines
1.7 KiB
Go
55 lines
1.7 KiB
Go
package mysqlmodel
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"git.lzcwy.cn/zhengchiliu/zt-online-game-backend/pkg/setting"
|
|
"gorm.io/driver/mysql"
|
|
"gorm.io/gorm"
|
|
"gorm.io/plugin/soft_delete"
|
|
)
|
|
|
|
type Model struct {
|
|
ID int64 `gorm:"column:id;primaryKey"` // 主键ID
|
|
CreatedAt int64 `gorm:"column:created_at"` // 创建时间
|
|
UpdatedAt int64 `gorm:"column:updated_at"` // 更新时间
|
|
DeletedAt soft_delete.DeletedAt `gorm:"column:deleted_at"` // 删除时间
|
|
CreatedBy int64 `gorm:"column:created_by"` // 创建人
|
|
UpdatedBy int64 `gorm:"column:updated_by"` // 更新人
|
|
DeletedBy int64 `gorm:"column:deleted_by"` // 删除人
|
|
}
|
|
|
|
// NewMysqlEngine 根据配置创建 GORM MySQL 连接并完成连接池初始化。
|
|
func NewMysqlEngine(mysqlSetting *setting.MysqlSettingS) (*gorm.DB, error) {
|
|
s := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local&timeout=10s&readTimeout=10s&writeTimeout=10s",
|
|
mysqlSetting.UserName,
|
|
mysqlSetting.Password,
|
|
mysqlSetting.Host,
|
|
mysqlSetting.Port,
|
|
mysqlSetting.DBName,
|
|
)
|
|
db, err := gorm.Open(mysql.Open(s), &gorm.Config{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
sqlDB, err := db.DB()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
sqlDB.SetMaxIdleConns(mysqlSetting.MaxIdleConns)
|
|
sqlDB.SetMaxOpenConns(mysqlSetting.MaxOpenConns)
|
|
sqlDB.SetConnMaxLifetime(time.Duration(mysqlSetting.ConnMaxLifeTime) * time.Second)
|
|
sqlDB.SetConnMaxIdleTime(time.Duration(mysqlSetting.ConnMaxIdleTime) * time.Second)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
if err := sqlDB.PingContext(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
return db, nil
|
|
}
|