core

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: May 28, 2026 License: MIT Imports: 34 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BeforeInit  = lifecycle.BeforeInit
	AfterInit   = lifecycle.AfterInit
	BeforeClose = lifecycle.BeforeClose
	AfterClose  = lifecycle.AfterClose
)

服务级钩子阶段常量

View Source
const (
	AppBeforeInit     = lifecycle.AppBeforeInit
	AppAfterInit      = lifecycle.AppAfterInit
	AppOnReady        = lifecycle.AppOnReady
	AppBeforeShutdown = lifecycle.AppBeforeShutdown
	AppAfterShutdown  = lifecycle.AppAfterShutdown
	AppOnInitFailed   = lifecycle.AppOnInitFailed
)

应用级钩子阶段常量

View Source
const (
	StateUninitialized = lifecycle.StateUninitialized
	StateInitializing  = lifecycle.StateInitializing
	StateReady         = lifecycle.StateReady
	StateFailed        = lifecycle.StateFailed
	StateClosed        = lifecycle.StateClosed
)

服务状态常量

Variables

View Source
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 AddSchedule

func AddSchedule(schedule config.ScheduleInfo)

AddSchedule 添加定时任务配置

func InitCustomConfig

func InitCustomConfig(conf any)

InitCustomConfig 初始化自定义配置 将用户自定义的配置结构体存储到全局变量中 参数 conf: 用户自定义的配置结构体指针

func MethodNotAllowed

func MethodNotAllowed(ctx *gin.Context)

MethodNotAllowed 处理405错误(方法不允许) 当请求的HTTP方法不被支持时,Gin框架会调用此函数 例如:路由只支持GET请求,但客户端发送了POST请求

参数 ctx: Gin上下文,包含请求和响应信息

响应内容: - HTTP状态码:405 - 响应体:纯文本格式的"Method Not Allowed" - 日志:包含请求的详细信息,便于问题排查

func NotFound

func NotFound(ctx *gin.Context)

NotFound 处理404错误(页面不存在) 当请求的路由不存在时,Gin框架会调用此函数 返回标准的HTTP 404响应,并记录详细的错误日志

参数 ctx: Gin上下文,包含请求和响应信息

响应内容: - HTTP状态码:404 - 响应体:纯文本格式的"Not Found" - 日志:包含请求的详细信息,便于问题排查

func OnAfterInit

func OnAfterInit(fn func(ctx context.Context) error)

OnAfterInit 注册应用初始化后钩子 在所有服务初始化完成、HTTP 监听之前执行

func OnAfterShutdown

func OnAfterShutdown(fn func(ctx context.Context) error)

OnAfterShutdown 注册应用关闭后钩子 在所有服务关闭完成、进程退出前执行

func OnBeforeInit

func OnBeforeInit(fn func(ctx context.Context) error)

OnBeforeInit 注册应用初始化前钩子 在 loadConfig 之后、initService 之前执行

func OnBeforeShutdown

func OnBeforeShutdown(fn func(ctx context.Context) error)

OnBeforeShutdown 注册应用关闭前钩子 在收到关闭信号后、CloseServices 之前执行 替代原 core.Start(exitfunctions ...) 的退出回调机制

func OnReady

func OnReady(fn func(ctx context.Context) error)

OnReady 注册服务就绪钩子 在 HTTP 服务开始监听后触发,适用于缓存预热等逻辑

func RegisterAppHook

func RegisterAppHook(hook lifecycle.AppHook)

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 RegisterService

func RegisterService(service Service) error

RegisterService 注册服务到全局注册中心

func RegisterServiceHook

func RegisterServiceHook(serviceName string, hook Hook)

RegisterServiceHook 注册钩子到全局注册中心

func SetInitConfig

func SetInitConfig(cfg InitConfig)

SetInitConfig 设置全局初始化配置

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 AppHook

type AppHook = lifecycle.AppHook

AppHook 应用级生命周期钩子

type AppHookPhase

type AppHookPhase = lifecycle.AppHookPhase

AppHookPhase 应用级钩子执行阶段

type CmdArgs

type CmdArgs struct {
	Env       string // 运行环境标识,如 dev、test、prod
	Config    string // 配置文件目录路径
	CipherKey string // 配置文件加密密钥,用于解密 CIPHER() 包裹的加密值
}

CmdArgs 命令行参数结构体 存储从命令行解析的运行时参数,用于控制应用的运行环境和配置加载方式

type HealthChecker

type HealthChecker = lifecycle.HealthChecker

HealthChecker 健康检查接口

type Hook

type Hook = lifecycle.Hook

Hook 初始化钩子

type HookPhase

type HookPhase = lifecycle.HookPhase

HookPhase 钩子执行阶段

type InitConfig

type InitConfig = lifecycle.InitConfig

InitConfig 初始化配置

type Service

type Service = lifecycle.Service

Service 服务接口

type ServiceState

type ServiceState = lifecycle.ServiceState

ServiceState 服务状态

func GetServiceState

func GetServiceState(name string) ServiceState

GetServiceState 获取服务状态

Directories

Path Synopsis
Package services 提供框架内置服务的实现 所有服务都实现了 core.Service 接口,支持自动依赖解析和并行初始化
Package services 提供框架内置服务的实现 所有服务都实现了 core.Service 接口,支持自动依赖解析和并行初始化

Jump to

Keyboard shortcuts

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