Documentation
¶
Index ¶
- Constants
- Variables
- func HasApplication() bool
- func SetApplication(app *Application)
- type Application
- func (a *Application) AutoDiscoverControllers(packagePaths ...string) error
- func (a *Application) Boot() error
- func (a *Application) Engine() *flow.Engine
- func (a *Application) Environment() *Environment
- func (a *Application) Logger() *logrus.Logger
- func (a *Application) OnAfterShutdown(name string, function func(), priority int)
- func (a *Application) OnAfterStart(name string, function func(), priority int)
- func (a *Application) OnBeforeShutdown(name string, function func(), priority int)
- func (a *Application) OnBeforeStart(name string, function func(), priority int)
- func (a *Application) RegisterControllers(constructors ...interface{}) error
- func (a *Application) RegisterHook(hookType HookType, name string, function func(), priority int)
- func (a *Application) RegisterProvider(provider ServiceProvider)
- func (a *Application) RegisterProviders(providers []ServiceProvider)
- func (a *Application) RegisterRoutes(routesFunc func(*flow.Engine))
- func (a *Application) Run(addr string) error
- func (a *Application) Shutdown(timeout time.Duration) error
- type BaseProvider
- type Controller
- type Environment
- type Hook
- type HookType
- type HooksManager
- func (hm *HooksManager) Execute(hookType HookType)
- func (hm *HooksManager) Register(hook Hook)
- func (hm *HooksManager) RegisterAfterShutdown(name string, function func(), priority int)
- func (hm *HooksManager) RegisterAfterStart(name string, function func(), priority int)
- func (hm *HooksManager) RegisterBeforeShutdown(name string, function func(), priority int)
- func (hm *HooksManager) RegisterBeforeStart(name string, function func(), priority int)
- type LifecycleManager
- type ProviderManager
- func (pm *ProviderManager) BootAll(app *Application) error
- func (pm *ProviderManager) BootProvider(provider ServiceProvider, app *Application) error
- func (pm *ProviderManager) GetProviders() []ServiceProvider
- func (pm *ProviderManager) IsBooted(name string) bool
- func (pm *ProviderManager) Register(provider ServiceProvider)
- func (pm *ProviderManager) RegisterAll(providers []ServiceProvider)
- func (pm *ProviderManager) RegisterAndBoot(provider ServiceProvider, app *Application) error
- type ServiceProvider
Constants ¶
const ( // StatusInit 初始化状态 StatusInit = iota // StatusStarting 正在启动 StatusStarting // StatusRunning 运行中 StatusRunning // StatusStopping 正在停止 StatusStopping // StatusStopped 已停止 StatusStopped )
应用状态常量
Variables ¶
var ( // ErrAppAlreadyRunning 应用已经在运行 ErrAppAlreadyRunning = errors.New("应用已经在运行中") // ErrAppNotRunning 应用未运行 ErrAppNotRunning = errors.New("应用尚未运行") // ErrShutdownTimeout 关闭超时错误 ErrShutdownTimeout = errors.New("应用关闭超时") )
错误定义
Functions ¶
Types ¶
type Application ¶
type Application struct {
// contains filtered or unexported fields
}
Application 是Flow应用容器
func (*Application) AutoDiscoverControllers ¶ added in v1.0.2
func (a *Application) AutoDiscoverControllers(packagePaths ...string) error
AutoDiscoverControllers 自动发现并注册控制器 遍历指定包中的所有类型,找到实现Controller接口的类型并注册
func (*Application) Environment ¶
func (a *Application) Environment() *Environment
Environment 获取环境信息
func (*Application) OnAfterShutdown ¶
func (a *Application) OnAfterShutdown(name string, function func(), priority int)
OnAfterShutdown 注册关闭后钩子
func (*Application) OnAfterStart ¶
func (a *Application) OnAfterStart(name string, function func(), priority int)
OnAfterStart 注册启动后钩子
func (*Application) OnBeforeShutdown ¶
func (a *Application) OnBeforeShutdown(name string, function func(), priority int)
OnBeforeShutdown 注册关闭前钩子
func (*Application) OnBeforeStart ¶
func (a *Application) OnBeforeStart(name string, function func(), priority int)
OnBeforeStart 注册启动前钩子
func (*Application) RegisterControllers ¶ added in v1.0.2
func (a *Application) RegisterControllers(constructors ...interface{}) error
RegisterControllers 批量注册控制器 接受一个构造函数的列表,每个构造函数应返回一个实现Controller接口的实例
func (*Application) RegisterHook ¶
func (a *Application) RegisterHook(hookType HookType, name string, function func(), priority int)
RegisterHook 注册应用钩子
func (*Application) RegisterProvider ¶
func (a *Application) RegisterProvider(provider ServiceProvider)
RegisterProvider 注册服务提供者
func (*Application) RegisterProviders ¶
func (a *Application) RegisterProviders(providers []ServiceProvider)
RegisterProviders 注册多个服务提供者
func (*Application) RegisterRoutes ¶ added in v1.0.2
func (a *Application) RegisterRoutes(routesFunc func(*flow.Engine))
RegisterRoutes 便捷方法,用于注册路由
type BaseProvider ¶
type BaseProvider struct {
// contains filtered or unexported fields
}
BaseProvider 基础服务提供者结构体,可作为自定义提供者的基类
func NewBaseProvider ¶
func NewBaseProvider(name string, priority int) *BaseProvider
NewBaseProvider 创建基础服务提供者
func (*BaseProvider) Register ¶
func (bp *BaseProvider) Register(app *Application) error
Register 注册服务(需要子类重写)
type Controller ¶ added in v1.0.2
type Controller interface {
// RegisterRoutes 注册控制器的路由
RegisterRoutes(router flow.RouterGroup)
}
控制器接口,用于自动注册路由
type Environment ¶
type Environment struct {
// 系统信息
GoVersion string // Go版本
GOOS string // 操作系统
GOARCH string // 系统架构
NumCPU int // CPU核心数
StartTime time.Time // 启动时间
Hostname string // 主机名
WorkingDir string // 工作目录
Executable string // 可执行文件路径
Environment map[string]string // 环境变量
// 应用配置
AppEnv string // 应用环境 (development, testing, production)
AppVersion string // 应用版本
AppName string // 应用名称
Debug bool // 是否为调试模式
}
Environment 环境信息结构体
func (*Environment) GetEnv ¶
func (e *Environment) GetEnv(key, defaultValue string) string
GetEnv 获取环境变量,如果不存在则返回默认值
func (*Environment) IsDevelopment ¶
func (e *Environment) IsDevelopment() bool
IsDevelopment 检查是否为开发环境
type Hook ¶
type Hook struct {
Name string // 钩子名称
Function func() // 钩子函数
Type HookType // 钩子类型
Priority int // 优先级,数值越小优先级越高
}
Hook 表示应用钩子函数
type HooksManager ¶
type HooksManager struct {
// contains filtered or unexported fields
}
HooksManager 钩子管理器
func (*HooksManager) Execute ¶
func (hm *HooksManager) Execute(hookType HookType)
Execute 执行指定类型的所有钩子
func (*HooksManager) RegisterAfterShutdown ¶
func (hm *HooksManager) RegisterAfterShutdown(name string, function func(), priority int)
RegisterAfterShutdown 注册关闭后钩子
func (*HooksManager) RegisterAfterStart ¶
func (hm *HooksManager) RegisterAfterStart(name string, function func(), priority int)
RegisterAfterStart 注册启动后钩子
func (*HooksManager) RegisterBeforeShutdown ¶
func (hm *HooksManager) RegisterBeforeShutdown(name string, function func(), priority int)
RegisterBeforeShutdown 注册关闭前钩子
func (*HooksManager) RegisterBeforeStart ¶
func (hm *HooksManager) RegisterBeforeStart(name string, function func(), priority int)
RegisterBeforeStart 注册启动前钩子
type LifecycleManager ¶
type LifecycleManager struct {
// contains filtered or unexported fields
}
LifecycleManager 应用生命周期管理器
func NewLifecycleManager ¶
func NewLifecycleManager(engine *flow.Engine) *LifecycleManager
NewLifecycleManager 创建新的生命周期管理器
func (*LifecycleManager) RegisterShutdownHook ¶
func (lm *LifecycleManager) RegisterShutdownHook(hook func())
RegisterShutdownHook 注册关闭钩子函数
func (*LifecycleManager) Shutdown ¶
func (lm *LifecycleManager) Shutdown(timeout time.Duration) error
Shutdown 优雅关闭应用
func (*LifecycleManager) StatusName ¶
func (lm *LifecycleManager) StatusName() string
StatusName 获取当前状态名称
type ProviderManager ¶
type ProviderManager struct {
// contains filtered or unexported fields
}
ProviderManager 提供者管理器
func (*ProviderManager) BootAll ¶
func (pm *ProviderManager) BootAll(app *Application) error
BootAll 启动所有注册的服务提供者
func (*ProviderManager) BootProvider ¶
func (pm *ProviderManager) BootProvider(provider ServiceProvider, app *Application) error
BootProvider 启动单个服务提供者
func (*ProviderManager) GetProviders ¶
func (pm *ProviderManager) GetProviders() []ServiceProvider
GetProviders 获取所有注册的服务提供者
func (*ProviderManager) IsBooted ¶
func (pm *ProviderManager) IsBooted(name string) bool
IsBooted 检查服务提供者是否已启动
func (*ProviderManager) Register ¶
func (pm *ProviderManager) Register(provider ServiceProvider)
Register 注册服务提供者
func (*ProviderManager) RegisterAll ¶
func (pm *ProviderManager) RegisterAll(providers []ServiceProvider)
RegisterAll 注册多个服务提供者
func (*ProviderManager) RegisterAndBoot ¶
func (pm *ProviderManager) RegisterAndBoot(provider ServiceProvider, app *Application) error
RegisterAndBoot 注册并启动服务提供者
type ServiceProvider ¶
type ServiceProvider interface {
// Register 向DI容器注册服务
Register(app *Application) error
// Boot 在所有服务都注册后启动服务
Boot(app *Application) error
// Name 获取提供者名称
Name() string
// Priority 获取提供者优先级,数值越小优先级越高
Priority() int
}
ServiceProvider 服务提供者接口