实现健康路由
This commit is contained in:
47
internal/app/controller/health/health_controller.go
Normal file
47
internal/app/controller/health/health_controller.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package health
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// Controller 结构体定义了健康检查控制器及其依赖。
|
||||
type Controller struct {
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
// NewController 创建并返回一个新的健康检查控制器实例。
|
||||
func NewController(ctx context.Context) *Controller {
|
||||
return &Controller{
|
||||
ctx: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
// Healthz 是一个简单的健康检查端点,用于存活探针 (Liveness Probe)。
|
||||
// 它也适用于 Docker 的 HEALTHCHECK 指令。
|
||||
// @Summary 服务存活检查
|
||||
// @Description 检查服务进程是否运行正常,只要服务能响应就返回 200 OK。
|
||||
// @Tags Health
|
||||
// @Produce json
|
||||
// @Success 200 {object} controller.Response "服务存活"
|
||||
// @Router /healthz [get]
|
||||
func (c *Controller) Healthz(ctx echo.Context) error {
|
||||
// 使用项目统一的响应函数,不记录日志
|
||||
return controller.SendResponse(ctx, controller.CodeSuccess, "服务存活", nil)
|
||||
}
|
||||
|
||||
// Readyz 是一个简单的就绪检查端点,用于就绪探针 (Readiness Probe)。
|
||||
// @Summary 服务就绪检查
|
||||
// @Description 检查服务是否已准备好接收流量。当前实现为只要服务能响应即代表就绪。
|
||||
// @Tags Health
|
||||
// @Produce json
|
||||
// @Success 200 {object} controller.Response "服务已就绪"
|
||||
// @Router /readyz [get]
|
||||
func (c *Controller) Readyz(ctx echo.Context) error {
|
||||
// TODO: 在未来,这里应该检查所有关键依赖(如数据库、外部服务)的可用性。
|
||||
// 使用项目统一的响应函数,不记录日志
|
||||
return controller.SendResponse(ctx, controller.CodeSuccess, "服务已就绪", nil)
|
||||
}
|
||||
Reference in New Issue
Block a user