app

package
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// StatusInit 初始化状态
	StatusInit = iota
	// StatusStarting 正在启动
	StatusStarting
	// StatusRunning 运行中
	StatusRunning
	// StatusStopping 正在停止
	StatusStopping
	// StatusStopped 已停止
	StatusStopped
)

应用状态常量

Variables

View Source
var (
	// ErrAppAlreadyRunning 应用已经在运行
	ErrAppAlreadyRunning = errors.New("应用已经在运行中")
	// ErrAppNotRunning 应用未运行
	ErrAppNotRunning = errors.New("应用尚未运行")
	// ErrShutdownTimeout 关闭超时错误
	ErrShutdownTimeout = errors.New("应用关闭超时")
)

错误定义

Functions

func HasApplication

func HasApplication() bool

HasApplication 检查是否已设置全局应用实例

func SetApplication

func SetApplication(app *Application)

SetApplication 设置全局应用实例

Types

type Application

type Application struct {
	// contains filtered or unexported fields
}

Application 是Flow应用容器

func GetApplication

func GetApplication() *Application

GetApplication 获取全局应用实例

func New

func New(engine *flow.Engine) *Application

New 创建一个新的应用容器

func (*Application) AutoDiscoverControllers added in v1.0.2

func (a *Application) AutoDiscoverControllers(packagePaths ...string) error

AutoDiscoverControllers 自动发现并注册控制器 遍历指定包中的所有类型,找到实现Controller接口的类型并注册

func (*Application) Boot

func (a *Application) Boot() error

Boot 启动应用

func (*Application) Engine

func (a *Application) Engine() *flow.Engine

Engine 获取Flow引擎

func (*Application) Environment

func (a *Application) Environment() *Environment

Environment 获取环境信息

func (*Application) Logger

func (a *Application) Logger() *logrus.Logger

Logger 获取日志记录器

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 便捷方法,用于注册路由

func (*Application) Run

func (a *Application) Run(addr string) error

Run 运行应用

func (*Application) Shutdown

func (a *Application) Shutdown(timeout time.Duration) error

Shutdown 关闭应用

type BaseProvider

type BaseProvider struct {
	// contains filtered or unexported fields
}

BaseProvider 基础服务提供者结构体,可作为自定义提供者的基类

func NewBaseProvider

func NewBaseProvider(name string, priority int) *BaseProvider

NewBaseProvider 创建基础服务提供者

func (*BaseProvider) Boot

func (bp *BaseProvider) Boot(app *Application) error

Boot 启动服务(需要子类重写)

func (*BaseProvider) Name

func (bp *BaseProvider) Name() string

Name 获取提供者名称

func (*BaseProvider) Priority

func (bp *BaseProvider) Priority() int

Priority 获取提供者优先级

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 NewEnvironment

func NewEnvironment() *Environment

NewEnvironment 创建一个新的环境信息实例

func (*Environment) GetEnv

func (e *Environment) GetEnv(key, defaultValue string) string

GetEnv 获取环境变量,如果不存在则返回默认值

func (*Environment) IsDevelopment

func (e *Environment) IsDevelopment() bool

IsDevelopment 检查是否为开发环境

func (*Environment) IsProduction

func (e *Environment) IsProduction() bool

IsProduction 检查是否为生产环境

func (*Environment) IsTesting

func (e *Environment) IsTesting() bool

IsTesting 检查是否为测试环境

func (*Environment) Summary

func (e *Environment) Summary() string

Summary 获取环境摘要信息

func (*Environment) Uptime

func (e *Environment) Uptime() time.Duration

Uptime 获取应用运行时间

type Hook

type Hook struct {
	Name     string   // 钩子名称
	Function func()   // 钩子函数
	Type     HookType // 钩子类型
	Priority int      // 优先级,数值越小优先级越高
}

Hook 表示应用钩子函数

type HookType

type HookType int

HookType 定义钩子类型

const (
	// HookBeforeStart 启动前钩子
	HookBeforeStart HookType = iota
	// HookAfterStart 启动后钩子
	HookAfterStart
	// HookBeforeShutdown 关闭前钩子
	HookBeforeShutdown
	// HookAfterShutdown 关闭后钩子
	HookAfterShutdown
)

type HooksManager

type HooksManager struct {
	// contains filtered or unexported fields
}

HooksManager 钩子管理器

func NewHooksManager

func NewHooksManager() *HooksManager

NewHooksManager 创建新的钩子管理器

func (*HooksManager) Execute

func (hm *HooksManager) Execute(hookType HookType)

Execute 执行指定类型的所有钩子

func (*HooksManager) Register

func (hm *HooksManager) Register(hook Hook)

Register 注册钩子

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) Start

func (lm *LifecycleManager) Start(addr string) error

Start 启动应用

func (*LifecycleManager) Status

func (lm *LifecycleManager) Status() int

Status 获取当前状态

func (*LifecycleManager) StatusName

func (lm *LifecycleManager) StatusName() string

StatusName 获取当前状态名称

type ProviderManager

type ProviderManager struct {
	// contains filtered or unexported fields
}

ProviderManager 提供者管理器

func NewProviderManager

func NewProviderManager() *ProviderManager

NewProviderManager 创建提供者管理器

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 服务提供者接口

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL