feat(app): 引入 uber-go/fx 重构 DI 装配层
This commit is contained in:
30
internal/app/module.go
Normal file
30
internal/app/module.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// Package app 提供 uber-go/fx 应用装配层。
|
||||
//
|
||||
// 该包用 fx 的 Provider/Lifecycle 取代了 pkg/*/manager.go 的全局单例和
|
||||
// internal/container 的手动 DI,是整个应用唯一的依赖装配入口。
|
||||
//
|
||||
// 使用方式:
|
||||
//
|
||||
// cfg, err := config.Load()
|
||||
// if err != nil { log.Fatal(err) }
|
||||
// fx.New(app.Module(cfg)).Run()
|
||||
package app
|
||||
|
||||
import (
|
||||
"carrotskin/pkg/config"
|
||||
|
||||
"go.uber.org/fx"
|
||||
)
|
||||
|
||||
// Module 返回应用的根 fx.Option,聚合所有子模块。
|
||||
// cfg 通过 fx.Supply 注入,供各 Provider 消费其子配置结构。
|
||||
//
|
||||
// 当前阶段:fx 管理 infra + Container 聚合 + 路由/服务器/任务生命周期。
|
||||
// Container 内部构造 Repository 与 Service(阶段6后将逐步解耦为 fx 直接管理 Service)。
|
||||
func Module(cfg *config.Config) fx.Option {
|
||||
return fx.Options(
|
||||
fx.Supply(cfg),
|
||||
InfraModule,
|
||||
BootstrapModule,
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user