Files
junhong_cmp_fiber/internal/bootstrap/bootstrap.go
2025-12-15 14:37:34 +08:00

56 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package bootstrap
import (
pkgGorm "github.com/break/junhong_cmp_fiber/pkg/gorm"
)
// Bootstrap 初始化所有业务组件并返回 Handlers
// 这是应用启动时的主入口,负责编排所有组件的初始化流程
//
// 初始化顺序:
// 1. 初始化 Store 层(数据访问)
// 2. 注册 GORM Callbacks数据权限过滤等- 需要 AccountStore
// 3. 初始化 Service 层(业务逻辑)
// 4. 初始化 Handler 层HTTP 处理)
//
// 参数:
// - deps: 基础依赖DB, Redis, Logger
//
// 返回:
// - *Handlers: 所有 HTTP 处理器
// - error: 初始化错误
func Bootstrap(deps *Dependencies) (*Handlers, error) {
// 1. 初始化 Store 层
stores := initStores(deps)
// 2. 注册 GORM Callbacks需要 AccountStore 来查询下级 ID
if err := registerGORMCallbacks(deps, stores); err != nil {
return nil, err
}
// 3. 初始化 Service 层
services := initServices(stores)
// 4. 初始化 Handler 层
handlers := initHandlers(services)
return handlers, nil
}
// registerGORMCallbacks 注册 GORM Callbacks
func registerGORMCallbacks(deps *Dependencies, stores *stores) error {
// 注册数据权限过滤 Callback
if err := pkgGorm.RegisterDataPermissionCallback(deps.DB, stores.Account); err != nil {
return err
}
//注册自动添加创建&更新人 Clalback
if err := pkgGorm.RegisterSetCreatorUpdaterCallback(deps.DB); err != nil {
return err
}
// TODO: 在此添加其他 GORM Callbacks
return nil
}