feat: 初次启动
This commit is contained in:
@@ -6,58 +6,24 @@ package svc
|
||||
import (
|
||||
"sundynix-micro-go/app/file/rpc/fileservice"
|
||||
"sundynix-micro-go/app/plant/api/internal/config"
|
||||
plantModel "sundynix-micro-go/app/plant/model"
|
||||
"sundynix-micro-go/app/plant/rpc/plantservice"
|
||||
"sundynix-micro-go/app/user/rpc/userservice"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
DB *gorm.DB
|
||||
UserRpc userservice.UserService
|
||||
FileRpc fileservice.FileService
|
||||
Config config.Config
|
||||
PlantRpc plantservice.PlantService
|
||||
UserRpc userservice.UserService
|
||||
FileRpc fileservice.FileService
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
db, err := gorm.Open(mysql.Open(c.DB.DataSource), &gorm.Config{})
|
||||
if err != nil {
|
||||
logx.Errorf("连接数据库失败: %v", err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// 自动迁移
|
||||
if err := db.AutoMigrate(
|
||||
&plantModel.SundynixPlantUserProfile{},
|
||||
&plantModel.SundynixMyPlant{},
|
||||
&plantModel.SundynixCarePlan{},
|
||||
&plantModel.SundynixCareRecord{},
|
||||
&plantModel.SundynixCareTask{},
|
||||
&plantModel.SundynixGrowthRecord{},
|
||||
&plantModel.SundynixWiki{},
|
||||
&plantModel.SundynixWikiClass{},
|
||||
&plantModel.SundynixPost{},
|
||||
&plantModel.SundynixPostComment{},
|
||||
&plantModel.SundynixPostLike{},
|
||||
&plantModel.SundynixPostTopic{},
|
||||
&plantModel.SundynixUserStar{},
|
||||
&plantModel.SundynixExchangeItem{},
|
||||
&plantModel.SundynixExchangeOrder{},
|
||||
&plantModel.SundynixLevelConfig{},
|
||||
&plantModel.SundynixBadgeConfig{},
|
||||
&plantModel.SundynixUserBadge{},
|
||||
&plantModel.SundynixAiChatHistory{},
|
||||
); err != nil {
|
||||
logx.Errorf("数据库迁移失败: %v", err)
|
||||
}
|
||||
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
DB: db,
|
||||
UserRpc: userservice.NewUserService(zrpc.MustNewClient(c.UserRpc)),
|
||||
FileRpc: fileservice.NewFileService(zrpc.MustNewClient(c.FileRpc)),
|
||||
Config: c,
|
||||
PlantRpc: plantservice.NewPlantService(zrpc.MustNewClient(c.PlantRpc)),
|
||||
UserRpc: userservice.NewUserService(zrpc.MustNewClient(c.UserRpc)),
|
||||
FileRpc: fileservice.NewFileService(zrpc.MustNewClient(c.FileRpc)),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user