修复数据库对象为nil的情况
This commit is contained in:
@@ -52,63 +52,55 @@ type Application struct {
|
|||||||
// NewApplication 创建并返回一个新的核心应用实例
|
// NewApplication 创建并返回一个新的核心应用实例
|
||||||
// 初始化所有核心组件
|
// 初始化所有核心组件
|
||||||
func NewApplication(cfg *config.Config) *Application {
|
func NewApplication(cfg *config.Config) *Application {
|
||||||
// 从配置中获取数据库连接字符串
|
|
||||||
connectionString := cfg.GetDatabaseConnectionString()
|
|
||||||
|
|
||||||
// 从配置中获取连接池参数
|
|
||||||
maxOpenConns := cfg.Database.MaxOpenConns
|
|
||||||
maxIdleConns := cfg.Database.MaxIdleConns
|
|
||||||
connMaxLifetime := cfg.Database.ConnMaxLifetime
|
|
||||||
|
|
||||||
// 初始化存储组件
|
|
||||||
store := db.NewStorage(connectionString, maxOpenConns, maxIdleConns, connMaxLifetime)
|
|
||||||
|
|
||||||
// 初始化用户仓库
|
|
||||||
userRepo := repository.NewUserRepo(store.GetDB())
|
|
||||||
|
|
||||||
// 初始化操作历史仓库
|
|
||||||
operationHistoryRepo := repository.NewOperationHistoryRepo(store.GetDB())
|
|
||||||
|
|
||||||
// 初始化设备控制仓库
|
|
||||||
deviceControlRepo := repository.NewDeviceControlRepo(store.GetDB())
|
|
||||||
|
|
||||||
// 初始化设备仓库
|
|
||||||
deviceRepo := repository.NewDeviceRepo(store.GetDB())
|
|
||||||
|
|
||||||
// 初始化WebSocket服务
|
|
||||||
websocketService := service.NewWebSocketService()
|
|
||||||
// 设置WebSocket超时时间
|
|
||||||
websocketService.SetDefaultTimeout(cfg.GetWebSocketTimeout())
|
|
||||||
|
|
||||||
// 初始化API组件
|
|
||||||
apiInstance := api.NewAPI(cfg, userRepo, operationHistoryRepo, deviceControlRepo, deviceRepo, websocketService)
|
|
||||||
|
|
||||||
// 初始化任务执行器组件(使用5个工作协程)
|
|
||||||
taskExecutor := task.NewExecutor(5)
|
|
||||||
|
|
||||||
return &Application{
|
return &Application{
|
||||||
Storage: store,
|
Config: cfg,
|
||||||
API: apiInstance,
|
logger: logs.NewLogger(),
|
||||||
TaskExecutor: taskExecutor,
|
|
||||||
UserRepo: userRepo,
|
|
||||||
OperationHistoryRepo: operationHistoryRepo,
|
|
||||||
DeviceControlRepo: deviceControlRepo,
|
|
||||||
DeviceRepo: deviceRepo,
|
|
||||||
WebSocketService: websocketService,
|
|
||||||
Config: cfg,
|
|
||||||
logger: logs.NewLogger(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start 启动核心应用
|
// Start 启动核心应用
|
||||||
// 按正确顺序启动所有核心组件
|
// 按正确顺序启动所有核心组件
|
||||||
func (app *Application) Start() error {
|
func (app *Application) Start() error {
|
||||||
|
// 从配置中获取数据库连接字符串
|
||||||
|
connectionString := app.Config.GetDatabaseConnectionString()
|
||||||
|
|
||||||
|
// 从配置中获取连接池参数
|
||||||
|
maxOpenConns := app.Config.Database.MaxOpenConns
|
||||||
|
maxIdleConns := app.Config.Database.MaxIdleConns
|
||||||
|
connMaxLifetime := app.Config.Database.ConnMaxLifetime
|
||||||
|
|
||||||
|
// 初始化存储组件
|
||||||
|
app.Storage = db.NewStorage(connectionString, maxOpenConns, maxIdleConns, connMaxLifetime)
|
||||||
|
|
||||||
// 启动存储组件
|
// 启动存储组件
|
||||||
if err := app.Storage.Connect(); err != nil {
|
if err := app.Storage.Connect(); err != nil {
|
||||||
return fmt.Errorf("存储连接失败: %v", err)
|
return fmt.Errorf("存储连接失败: %v", err)
|
||||||
}
|
}
|
||||||
app.logger.Info("存储连接成功")
|
app.logger.Info("存储连接成功")
|
||||||
|
|
||||||
|
// 初始化用户仓库
|
||||||
|
app.UserRepo = repository.NewUserRepo(app.Storage.GetDB())
|
||||||
|
|
||||||
|
// 初始化操作历史仓库
|
||||||
|
app.OperationHistoryRepo = repository.NewOperationHistoryRepo(app.Storage.GetDB())
|
||||||
|
|
||||||
|
// 初始化设备控制仓库
|
||||||
|
app.DeviceControlRepo = repository.NewDeviceControlRepo(app.Storage.GetDB())
|
||||||
|
|
||||||
|
// 初始化设备仓库
|
||||||
|
app.DeviceRepo = repository.NewDeviceRepo(app.Storage.GetDB())
|
||||||
|
|
||||||
|
// 初始化WebSocket服务
|
||||||
|
app.WebSocketService = service.NewWebSocketService()
|
||||||
|
// 设置WebSocket超时时间
|
||||||
|
app.WebSocketService.SetDefaultTimeout(app.Config.GetWebSocketTimeout())
|
||||||
|
|
||||||
|
// 初始化API组件
|
||||||
|
app.API = api.NewAPI(app.Config, app.UserRepo, app.OperationHistoryRepo, app.DeviceControlRepo, app.DeviceRepo, app.WebSocketService)
|
||||||
|
|
||||||
|
// 初始化任务执行器组件(使用5个工作协程)
|
||||||
|
app.TaskExecutor = task.NewExecutor(5)
|
||||||
|
|
||||||
// 启动API组件
|
// 启动API组件
|
||||||
if err := app.API.Start(); err != nil {
|
if err := app.API.Start(); err != nil {
|
||||||
return fmt.Errorf("API启动失败: %v", err)
|
return fmt.Errorf("API启动失败: %v", err)
|
||||||
|
|||||||
Reference in New Issue
Block a user