refactor: align framework cleanup with new bootstrap flow
Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
This commit is contained in:
50
internal/bootstrap/bootstrap.go
Normal file
50
internal/bootstrap/bootstrap.go
Normal file
@@ -0,0 +1,50 @@
|
||||
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
|
||||
}
|
||||
|
||||
// TODO: 在此添加其他 GORM Callbacks
|
||||
|
||||
return nil
|
||||
}
|
||||
15
internal/bootstrap/dependencies.go
Normal file
15
internal/bootstrap/dependencies.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Dependencies 封装所有基础依赖
|
||||
// 这些是应用启动时初始化的核心组件
|
||||
type Dependencies struct {
|
||||
DB *gorm.DB // PostgreSQL 数据库连接
|
||||
Redis *redis.Client // Redis 客户端
|
||||
Logger *zap.Logger // 应用日志器
|
||||
}
|
||||
15
internal/bootstrap/handlers.go
Normal file
15
internal/bootstrap/handlers.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"github.com/break/junhong_cmp_fiber/internal/handler"
|
||||
)
|
||||
|
||||
// initHandlers 初始化所有 Handler 实例
|
||||
func initHandlers(svc *services) *Handlers {
|
||||
return &Handlers{
|
||||
Account: handler.NewAccountHandler(svc.Account),
|
||||
Role: handler.NewRoleHandler(svc.Role),
|
||||
Permission: handler.NewPermissionHandler(svc.Permission),
|
||||
// TODO: 新增 Handler 在此初始化
|
||||
}
|
||||
}
|
||||
26
internal/bootstrap/services.go
Normal file
26
internal/bootstrap/services.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
accountSvc "github.com/break/junhong_cmp_fiber/internal/service/account"
|
||||
permissionSvc "github.com/break/junhong_cmp_fiber/internal/service/permission"
|
||||
roleSvc "github.com/break/junhong_cmp_fiber/internal/service/role"
|
||||
)
|
||||
|
||||
// services 封装所有 Service 实例
|
||||
// 注意:此结构体不导出,仅在 bootstrap 包内部使用
|
||||
type services struct {
|
||||
Account *accountSvc.Service
|
||||
Role *roleSvc.Service
|
||||
Permission *permissionSvc.Service
|
||||
// TODO: 新增 Service 在此添加字段
|
||||
}
|
||||
|
||||
// initServices 初始化所有 Service 实例
|
||||
func initServices(s *stores) *services {
|
||||
return &services{
|
||||
Account: accountSvc.New(s.Account, s.Role, s.AccountRole),
|
||||
Role: roleSvc.New(s.Role, s.Permission, s.RolePermission),
|
||||
Permission: permissionSvc.New(s.Permission),
|
||||
// TODO: 新增 Service 在此初始化
|
||||
}
|
||||
}
|
||||
28
internal/bootstrap/stores.go
Normal file
28
internal/bootstrap/stores.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
)
|
||||
|
||||
// stores 封装所有 Store 实例
|
||||
// 注意:此结构体不导出,仅在 bootstrap 包内部使用
|
||||
type stores struct {
|
||||
Account *postgres.AccountStore
|
||||
Role *postgres.RoleStore
|
||||
Permission *postgres.PermissionStore
|
||||
AccountRole *postgres.AccountRoleStore
|
||||
RolePermission *postgres.RolePermissionStore
|
||||
// TODO: 新增 Store 在此添加字段
|
||||
}
|
||||
|
||||
// initStores 初始化所有 Store 实例
|
||||
func initStores(deps *Dependencies) *stores {
|
||||
return &stores{
|
||||
Account: postgres.NewAccountStore(deps.DB, deps.Redis),
|
||||
Role: postgres.NewRoleStore(deps.DB),
|
||||
Permission: postgres.NewPermissionStore(deps.DB),
|
||||
AccountRole: postgres.NewAccountRoleStore(deps.DB),
|
||||
RolePermission: postgres.NewRolePermissionStore(deps.DB),
|
||||
// TODO: 新增 Store 在此初始化
|
||||
}
|
||||
}
|
||||
14
internal/bootstrap/types.go
Normal file
14
internal/bootstrap/types.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"github.com/break/junhong_cmp_fiber/internal/handler"
|
||||
)
|
||||
|
||||
// Handlers 封装所有 HTTP 处理器
|
||||
// 用于路由注册
|
||||
type Handlers struct {
|
||||
Account *handler.AccountHandler
|
||||
Role *handler.RoleHandler
|
||||
Permission *handler.PermissionHandler
|
||||
// TODO: 新增 Handler 在此添加字段
|
||||
}
|
||||
Reference in New Issue
Block a user