Files
junhong_cmp_fiber/internal/bootstrap/bootstrap.go
huang 03a0960c4d
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m2s
refactor: 数据权限过滤从 GORM Callback 改为 Store 层显式调用
- 移除 RegisterDataPermissionCallback 和 SkipDataPermission 机制
- 在 Auth 中间件预计算 SubordinateShopIDs 并注入 Context
- 新增 ApplyShopFilter/ApplyEnterpriseFilter/ApplyOwnerShopFilter 等 Helper 函数
- 所有 Store 层查询方法显式调用数据权限过滤函数
- 权限检查函数 CanManageShop/CanManageEnterprise 改为从 Context 获取数据

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-26 16:38:52 +08:00

71 lines
2.1 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"
"go.uber.org/zap"
)
// BootstrapResult Bootstrap 初始化结果
type BootstrapResult struct {
Handlers *Handlers
Middlewares *Middlewares
}
// Bootstrap 初始化所有业务组件并返回 Handlers 和 Middlewares
// 这是应用启动时的主入口,负责编排所有组件的初始化流程
//
// 初始化顺序:
// 1. 初始化 Store 层(数据访问)
// 2. 注册 GORM Callbacks数据权限过滤等- 需要 AccountStore
// 3. 初始化 Service 层(业务逻辑)
// 4. 初始化默认超级管理员(如果不存在)
// 5. 初始化 Middleware 层(中间件)
// 6. 初始化 Handler 层HTTP 处理)
//
// 参数:
// - deps: 基础依赖DB, Redis, Logger
//
// 返回:
// - *BootstrapResult: 包含 Handlers 和 Middlewares
// - error: 初始化错误
func Bootstrap(deps *Dependencies) (*BootstrapResult, 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, deps)
// 4. 初始化默认超级管理员(降级处理:失败不中断启动)
if err := initDefaultAdmin(deps, services); err != nil {
deps.Logger.Error("初始化默认超级管理员失败", zap.Error(err))
}
// 5. 初始化 Middleware 层(传入 ShopStore 以支持预计算下级店铺 ID
middlewares := initMiddlewares(deps, stores)
// 6. 初始化 Handler 层
handlers := initHandlers(services, deps)
return &BootstrapResult{
Handlers: handlers,
Middlewares: middlewares,
}, nil
}
// registerGORMCallbacks 注册 GORM Callbacks
func registerGORMCallbacks(deps *Dependencies, stores *stores) error {
// 注册自动添加创建&更新人 Callback
if err := pkgGorm.RegisterSetCreatorUpdaterCallback(deps.DB); err != nil {
return err
}
// 数据权限过滤已移至 Store 层显式调用 ApplyXxxFilter 函数
return nil
}