Documentation
¶
Index ¶
- type Host
- type Kernel
- type Lifecycle
- type Module
- type Option
- func Import(m *Module) Option
- func Load(m *Module) Option
- func Provide(target any, opts ...di.Option) Option
- func ProvideConfig[T any](key string, defaultVal *T) Option
- func ProvideMonitor[T any](key string, defaultVal *T) Option
- func ProvideValue[T any](val T, opts ...di.Option) Option
- func Runner(target any, opts ...di.Option) Option
- func WithConfig(file string) Option
- type Runnable
- type State
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Host ¶
type Host struct {
// contains filtered or unexported fields
}
Host 是应用的通用宿主,负责组合 Kernel 和编排多个 Runnable
func (*Host) InjectAuto ¶
InjectAuto 从容器中解析服务并填充到 target 指针中
type Kernel ¶
type Kernel interface {
// GetContainer 获取依赖注入容器,用于注册或解析服务
GetContainer() di.Container
// GetLifecycle 获取生命周期管理器,用于注册启动/停止钩子
GetLifecycle() Lifecycle
// Boot 启动内核
// 1. 锁定容器(不再允许注册)
// 2. 构建依赖图
// 3. 执行所有 OnStart 钩子
Boot() error
// Shutdown 停止内核
// 1. 执行所有 OnStop 钩子(倒序)
// 2. 释放资源
Shutdown() error
// State 返回当前状态
State() State
}
Kernel 定义了应用核心的最小能力集 它是无状态的(相对于业务逻辑),负责资源管理和生命周期管理
type Lifecycle ¶
type Lifecycle interface {
OnStart(fn func(context.Context) error)
OnStop(fn func(context.Context) error)
Start(ctx context.Context, container di.Container) error
Stop(ctx context.Context) error
}
Lifecycle 管理应用程序的生命周期
type Module ¶
type Module struct {
// contains filtered or unexported fields
}
Module 定义了一个功能模块
type Option ¶
type Option func(*Host)
Option 配置 Host 的函数选项
func ProvideConfig ¶
ProvideConfig 注册一个静态配置对象到 DI 容器。 返回的是 *T,不支持热重载。
示例:
ego.ProvideConfig("server", &ServerConfig{Port: 8080})
func ProvideMonitor ¶
ProvideMonitor 注册一个动态配置引用 (Monitor[T])。 返回 config.Monitor[T],支持热重载。当配置文件变更时,Monitor.Value() 会自动更新。
示例:
ego.ProvideMonitor("feature_flags", &Flags{EnableX: false})
// Use: monitor.Value().EnableX
func ProvideValue ¶
ProvideValue 注册一个静态值到 DI 容器。 它本质上是 Provide(func() T { return val }) 的语法糖。
func WithConfig ¶
WithConfig 加载配置文件。 file: 完整路径,如 "config.yaml"。 如果 file 为空,默认尝试加载 "config.yaml"。 如果文件不存在,将降级为仅使用环境变量。
type Runnable ¶
type Runnable interface {
// Start 启动服务
// 该方法应在新的 goroutine 中执行长驻任务,或者在执行完毕后返回
// 如果是长驻服务 (如 HTTP Server),Start 通常是阻塞的
// Host 会在 goroutine 中调用 Start
Start(ctx context.Context) error
// Stop 停止服务
// 该方法用于通知服务停止,并释放资源
Stop(ctx context.Context) error
// Name 返回组件名称,用于日志和调试
Name() string
}
Runnable 代表一个可以独立启动和停止的服务单元 (如 WebServer, GrpcServer, JobWorker)
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples/container_instance
command
|
|
|
examples/inject_pointer
command
|
|
|
examples/simple
command
|
|
|
example
|
|
|
auth
command
|
|
|
config_bind
command
|
|
|
grpc_simple
command
|
|
|
modular
command
|
|
|
simple
command
|
|
|
web_docs
command
|
|
|
web_features
command
|
|
|
ext
|
|
Click to show internal directories.
Click to hide internal directories.