Documentation
¶
Index ¶
- Constants
- Variables
- func AddMessageQueueConsumer(messageQueue *config.MessageQueue)
- func AddMessageQueueProducer(messageQueue *config.MessageQueue)
- func AddOptionFunc(optionFunc ...gin.OptionFunc)
- func AddSchedule(schedule config.ScheduleInfo)
- func InitCustomConfig(conf any)
- func MethodNotAllowed(ctx *gin.Context)
- func NotFound(ctx *gin.Context)
- func OnAfterInit(fn func(ctx context.Context) error)
- func OnAfterShutdown(fn func(ctx context.Context) error)
- func OnBeforeInit(fn func(ctx context.Context) error)
- func OnBeforeShutdown(fn func(ctx context.Context) error)
- func OnReady(fn func(ctx context.Context) error)
- func RegisterAppHook(hook lifecycle.AppHook)
- func RegisterMiddleware(name string, handlerFunc func() gin.HandlerFunc) error
- func RegisterService(service Service) error
- func RegisterServiceHook(serviceName string, hook Hook)
- func SetInitConfig(cfg InitConfig)
- func Start()
- type AppHook
- type AppHookPhase
- type CmdArgs
- type HealthChecker
- type Hook
- type HookPhase
- type InitConfig
- type Service
- type ServiceState
Constants ¶
const ( BeforeInit = lifecycle.BeforeInit AfterInit = lifecycle.AfterInit BeforeClose = lifecycle.BeforeClose AfterClose = lifecycle.AfterClose )
服务级钩子阶段常量
const ( AppBeforeInit = lifecycle.AppBeforeInit AppAfterInit = lifecycle.AppAfterInit AppOnReady = lifecycle.AppOnReady AppBeforeShutdown = lifecycle.AppBeforeShutdown AppAfterShutdown = lifecycle.AppAfterShutdown AppOnInitFailed = lifecycle.AppOnInitFailed )
应用级钩子阶段常量
const ( StateUninitialized = lifecycle.StateUninitialized StateInitializing = lifecycle.StateInitializing StateReady = lifecycle.StateReady StateFailed = lifecycle.StateFailed StateClosed = lifecycle.StateClosed )
服务状态常量
Variables ¶
var DefaultInitConfig = lifecycle.DefaultInitConfig
DefaultInitConfig 默认初始化配置
Functions ¶
func AddMessageQueueConsumer ¶
func AddMessageQueueConsumer(messageQueue *config.MessageQueue)
AddMessageQueueConsumer 添加消息队列消费者配置
func AddMessageQueueProducer ¶
func AddMessageQueueProducer(messageQueue *config.MessageQueue)
AddMessageQueueProducer 添加消息队列发送者配置
func AddOptionFunc ¶
func AddOptionFunc(optionFunc ...gin.OptionFunc)
AddOptionFunc 添加路由选项函数 允许用户注册自定义的路由配置函数,这些函数会在引擎初始化时被调用 支持传入多个函数,函数会按照添加顺序执行 该函数是线程安全的,可以在多个 goroutine 中并发调用 参数 optionFunc: 一个或多个 gin.OptionFunc 类型的路由配置函数
使用示例:
AddOptionFunc(func(e *gin.Engine) {
r := e.Group("/api")
r.GET("/users", getUsersHandler)
})
func InitCustomConfig ¶
func InitCustomConfig(conf any)
InitCustomConfig 初始化自定义配置 将用户自定义的配置结构体存储到全局变量中 参数 conf: 用户自定义的配置结构体指针
func MethodNotAllowed ¶
MethodNotAllowed 处理405错误(方法不允许) 当请求的HTTP方法不被支持时,Gin框架会调用此函数 例如:路由只支持GET请求,但客户端发送了POST请求
参数 ctx: Gin上下文,包含请求和响应信息
响应内容: - HTTP状态码:405 - 响应体:纯文本格式的"Method Not Allowed" - 日志:包含请求的详细信息,便于问题排查
func NotFound ¶
NotFound 处理404错误(页面不存在) 当请求的路由不存在时,Gin框架会调用此函数 返回标准的HTTP 404响应,并记录详细的错误日志
参数 ctx: Gin上下文,包含请求和响应信息
响应内容: - HTTP状态码:404 - 响应体:纯文本格式的"Not Found" - 日志:包含请求的详细信息,便于问题排查
func OnAfterInit ¶
OnAfterInit 注册应用初始化后钩子 在所有服务初始化完成、HTTP 监听之前执行
func OnAfterShutdown ¶
OnAfterShutdown 注册应用关闭后钩子 在所有服务关闭完成、进程退出前执行
func OnBeforeInit ¶
OnBeforeInit 注册应用初始化前钩子 在 loadConfig 之后、initService 之前执行
func OnBeforeShutdown ¶
OnBeforeShutdown 注册应用关闭前钩子 在收到关闭信号后、CloseServices 之前执行 替代原 core.Start(exitfunctions ...) 的退出回调机制
func RegisterAppHook ¶
RegisterAppHook 注册应用级生命周期钩子 支持完整的钩子配置(阶段、优先级、名称、执行函数)
func RegisterMiddleware ¶
func RegisterMiddleware(name string, handlerFunc func() gin.HandlerFunc) error
RegisterMiddleware 注册中间件到映射表 将中间件处理函数注册到全局中间件映射表中,使其可以通过名称引用 这是中间件系统的核心注册函数,支持插件化的中间件管理
参数:
- name: 中间件名称,必须唯一,用于在配置文件中引用
- handlerFunc: 中间件工厂函数,返回gin.HandlerFunc类型的处理函数
返回值:
- error: 如果中间件名称已存在则返回错误,否则返回nil
使用示例:
err := RegisterMiddleware("cors", func() gin.HandlerFunc {
return gin.HandlerFunc(func(c *gin.Context) {
// CORS处理逻辑
c.Next()
})
})
func RegisterServiceHook ¶
RegisterServiceHook 注册钩子到全局注册中心
func Start ¶
func Start()
Start 启动 Web 服务器 这是应用程序的主入口函数,负责完整的服务器启动流程(钩子驱动): 1. overrideValidator — 自定义验证器 2. loadConfig — 加载配置 3. ExecuteAppHooks(AppBeforeInit) — 应用初始化前钩子 4. initMiddleware — 初始化中间件 5. initService — 初始化服务组件
- 成功 → ExecuteAppHooks(AppAfterInit)
- 失败 → ExecuteAppHooks(AppOnInitFailed),然后 panic
6. 创建 HTTP Server 7. net.Listen 绑定端口,确认监听就绪 8. ExecuteAppHooks(AppOnReady)(在独立 goroutine 中,端口绑定成功后触发) 9. server.Serve(listener)
关闭流程: 9. 收到 SIGINT/SIGTERM 10. ExecuteAppHooks(AppBeforeShutdown) 11. lifecycle.CloseServices() 12. server.Shutdown(shutdownTimeout) 13. ExecuteAppHooks(AppAfterShutdown)
服务器特性: - 支持优雅关闭(接收 SIGINT/SIGTERM 信号) - 优雅关闭超时可配置(ServiceInfo.ShutdownTimeout) - 应用级生命周期钩子驱动 - 集成 pprof 性能分析工具
Types ¶
type CmdArgs ¶
type CmdArgs struct {
Env string // 运行环境标识,如 dev、test、prod
Config string // 配置文件目录路径
CipherKey string // 配置文件加密密钥,用于解密 CIPHER() 包裹的加密值
}
CmdArgs 命令行参数结构体 存储从命令行解析的运行时参数,用于控制应用的运行环境和配置加载方式