user viper

This commit is contained in:
张超
2025-04-15 16:47:00 +08:00
parent 33cb413a12
commit f52d5a698b
12 changed files with 329 additions and 214 deletions

View File

@@ -8,7 +8,7 @@ package models
import (
"fmt"
conf "go_blog/config"
"go_blog/config"
"go_blog/pkg/util"
"time"
@@ -19,7 +19,8 @@ import (
var DB *gorm.DB
func SetUp() {
func InitDatabase(conf *config.Config) {
conUri := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=True&loc=Local",
conf.DataBase.User,
conf.DataBase.Password,
@@ -27,20 +28,23 @@ func SetUp() {
conf.DataBase.Port,
conf.DataBase.DBName,
conf.DataBase.Charset)
// 2. 初始化数据库
db, err := gorm.Open(mysql.Open(conUri), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
TablePrefix: conf.DataBase.Prefix,
},
})
if err != nil {
panic(err)
panic("数据库连接失败: " + err.Error())
}
DB = db
DB = db
// 3. 自动迁移数据模型
DB.AutoMigrate(&Account{})
DB.AutoMigrate(&Content{})
// if err := db.AutoMigrate(&models.Article{}, &models.User{}); err != nil {
// panic("数据库迁移失败: " + err.Error())
// }
}
type BaseModel struct {