first commit
Some checks failed
CI/CD Pipeline / Test (push) Failing after 22m19s
CI/CD Pipeline / Security Scan (push) Failing after 5m57s
CI/CD Pipeline / Build (amd64, darwin) (push) Has been skipped
CI/CD Pipeline / Build (amd64, linux) (push) Has been skipped
CI/CD Pipeline / Build (amd64, windows) (push) Has been skipped
CI/CD Pipeline / Build (arm64, darwin) (push) Has been skipped
CI/CD Pipeline / Build (arm64, linux) (push) Has been skipped
CI/CD Pipeline / Build Docker Image (push) Has been skipped
CI/CD Pipeline / Create Release (push) Has been skipped
Some checks failed
CI/CD Pipeline / Test (push) Failing after 22m19s
CI/CD Pipeline / Security Scan (push) Failing after 5m57s
CI/CD Pipeline / Build (amd64, darwin) (push) Has been skipped
CI/CD Pipeline / Build (amd64, linux) (push) Has been skipped
CI/CD Pipeline / Build (amd64, windows) (push) Has been skipped
CI/CD Pipeline / Build (arm64, darwin) (push) Has been skipped
CI/CD Pipeline / Build (arm64, linux) (push) Has been skipped
CI/CD Pipeline / Build Docker Image (push) Has been skipped
CI/CD Pipeline / Create Release (push) Has been skipped
This commit is contained in:
212
internal/handler/health.go
Normal file
212
internal/handler/health.go
Normal file
@@ -0,0 +1,212 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/subconverter-go/internal/config"
|
||||
"github.com/subconverter-go/internal/logging"
|
||||
)
|
||||
|
||||
// HealthHandler 健康检查处理器
|
||||
// 提供应用程序健康状态检查功能
|
||||
type HealthHandler struct {
|
||||
logger *logging.Logger
|
||||
configMgr *config.ConfigManager
|
||||
startTime time.Time
|
||||
}
|
||||
|
||||
// NewHealthHandler 创建新的健康检查处理器
|
||||
// 返回初始化好的HealthHandler实例
|
||||
func NewHealthHandler(logger *logging.Logger, configMgr *config.ConfigManager) *HealthHandler {
|
||||
return &HealthHandler{
|
||||
logger: logger,
|
||||
configMgr: configMgr,
|
||||
startTime: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
// HandleHealth 处理健康检查请求
|
||||
// 返回应用程序的健康状态信息
|
||||
func (h *HealthHandler) HandleHealth(c *fiber.Ctx) error {
|
||||
h.logger.Debug("Health check request received")
|
||||
|
||||
// 获取配置信息
|
||||
config := h.configMgr.GetConfig()
|
||||
|
||||
// 计算运行时间
|
||||
uptime := time.Since(h.startTime).String()
|
||||
|
||||
// 创建健康状态响应
|
||||
health := map[string]interface{}{
|
||||
"status": "healthy",
|
||||
"timestamp": time.Now().Format(time.RFC3339),
|
||||
"uptime": uptime,
|
||||
"version": "1.0.0",
|
||||
"service": "subconverter-go",
|
||||
"environment": "development", // 可以从配置中获取
|
||||
}
|
||||
|
||||
// 添加服务器信息
|
||||
health["server"] = map[string]interface{}{
|
||||
"host": config.Server.Host,
|
||||
"port": config.Server.Port,
|
||||
"read_timeout": config.Server.ReadTimeout,
|
||||
"write_timeout": config.Server.WriteTimeout,
|
||||
"max_request_size": config.Server.MaxRequestSize,
|
||||
}
|
||||
|
||||
// 添加配置信息
|
||||
health["config"] = map[string]interface{}{
|
||||
"default_target": config.Conversion.DefaultTarget,
|
||||
"supported_targets": config.Conversion.SupportedTargets,
|
||||
"rate_limit": config.Security.RateLimit,
|
||||
"cors_origins": config.Security.CorsOrigins,
|
||||
}
|
||||
|
||||
// 添加系统信息
|
||||
health["system"] = map[string]interface{}{
|
||||
"memory_usage": "N/A", // 可以添加内存使用情况
|
||||
"cpu_usage": "N/A", // 可以添加CPU使用情况
|
||||
"goroutines": "N/A", // 可以添加goroutine数量
|
||||
}
|
||||
|
||||
// 设置响应头
|
||||
c.Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
||||
c.Set("Pragma", "no-cache")
|
||||
c.Set("Expires", "0")
|
||||
|
||||
return c.JSON(health)
|
||||
}
|
||||
|
||||
// HandleReady 处理就绪检查请求
|
||||
// 用于Kubernetes等系统的就绪探针
|
||||
func (h *HealthHandler) HandleReady(c *fiber.Ctx) error {
|
||||
h.logger.Debug("Readiness check request received")
|
||||
|
||||
// 检查配置是否已加载
|
||||
config := h.configMgr.GetConfig()
|
||||
if config == nil {
|
||||
h.logger.Warn("Configuration not loaded for readiness check")
|
||||
return c.Status(fiber.StatusServiceUnavailable).JSON(map[string]interface{}{
|
||||
"status": "not ready",
|
||||
"message": "configuration not loaded",
|
||||
})
|
||||
}
|
||||
|
||||
// 检查必要的服务是否可用
|
||||
// 这里可以添加数据库连接、外部服务连接等检查
|
||||
|
||||
// 创建就绪状态响应
|
||||
ready := map[string]interface{}{
|
||||
"status": "ready",
|
||||
"timestamp": time.Now().Format(time.RFC3339),
|
||||
"service": "subconverter-go",
|
||||
"checks": map[string]interface{}{
|
||||
"configuration": "ok",
|
||||
"server": "ok",
|
||||
"dependencies": "ok",
|
||||
},
|
||||
}
|
||||
|
||||
return c.JSON(ready)
|
||||
}
|
||||
|
||||
// HandleLive 处理存活检查请求
|
||||
// 用于Kubernetes等系统的存活探针
|
||||
func (h *HealthHandler) HandleLive(c *fiber.Ctx) error {
|
||||
h.logger.Debug("Liveness check request received")
|
||||
|
||||
// 简单的存活检查,如果应用程序能响应就认为存活
|
||||
live := map[string]interface{}{
|
||||
"status": "alive",
|
||||
"timestamp": time.Now().Format(time.RFC3339),
|
||||
"service": "subconverter-go",
|
||||
}
|
||||
|
||||
return c.JSON(live)
|
||||
}
|
||||
|
||||
// HandleMetrics 处理指标请求
|
||||
// 返回应用程序的运行时指标
|
||||
func (h *HealthHandler) HandleMetrics(c *fiber.Ctx) error {
|
||||
h.logger.Debug("Metrics request received")
|
||||
|
||||
// 获取配置信息
|
||||
config := h.configMgr.GetConfig()
|
||||
|
||||
// 计算运行时间
|
||||
uptime := time.Since(h.startTime)
|
||||
|
||||
// 创建指标响应
|
||||
metrics := map[string]interface{}{
|
||||
"timestamp": time.Now().Format(time.RFC3339),
|
||||
"uptime_ms": uptime.Milliseconds(),
|
||||
"service": "subconverter-go",
|
||||
"version": "1.0.0",
|
||||
}
|
||||
|
||||
// 添加服务器指标
|
||||
metrics["server"] = map[string]interface{}{
|
||||
"host": config.Server.Host,
|
||||
"port": config.Server.Port,
|
||||
"config": map[string]interface{}{
|
||||
"read_timeout": config.Server.ReadTimeout,
|
||||
"write_timeout": config.Server.WriteTimeout,
|
||||
"max_request_size": config.Server.MaxRequestSize,
|
||||
},
|
||||
}
|
||||
|
||||
// 添加转换相关指标
|
||||
metrics["conversion"] = map[string]interface{}{
|
||||
"default_target": config.Conversion.DefaultTarget,
|
||||
"supported_targets": config.Conversion.SupportedTargets,
|
||||
"default_emoji": config.Conversion.DefaultEmoji,
|
||||
"default_udp": config.Conversion.DefaultUDP,
|
||||
"max_nodes": config.Conversion.MaxNodes,
|
||||
"cache_timeout": config.Conversion.CacheTimeout,
|
||||
}
|
||||
|
||||
// 添加安全相关指标
|
||||
metrics["security"] = map[string]interface{}{
|
||||
"rate_limit": config.Security.RateLimit,
|
||||
"timeout": config.Security.Timeout,
|
||||
"cors_origins_count": len(config.Security.CorsOrigins),
|
||||
"access_tokens_count": len(config.Security.AccessTokens),
|
||||
}
|
||||
|
||||
// 添加日志相关指标
|
||||
metrics["logging"] = map[string]interface{}{
|
||||
"level": config.Logging.Level,
|
||||
"format": config.Logging.Format,
|
||||
"output": config.Logging.Output,
|
||||
}
|
||||
|
||||
return c.JSON(metrics)
|
||||
}
|
||||
|
||||
// HandlePing 处理ping请求
|
||||
// 用于网络连通性测试
|
||||
func (h *HealthHandler) HandlePing(c *fiber.Ctx) error {
|
||||
h.logger.Debug("Ping request received")
|
||||
|
||||
// 简单的ping响应
|
||||
ping := map[string]interface{}{
|
||||
"message": "pong",
|
||||
"timestamp": time.Now().Format(time.RFC3339),
|
||||
"service": "subconverter-go",
|
||||
"version": "1.0.0",
|
||||
}
|
||||
|
||||
return c.JSON(ping)
|
||||
}
|
||||
|
||||
// GetUptime 获取应用程序运行时间
|
||||
func (h *HealthHandler) GetUptime() time.Duration {
|
||||
return time.Since(h.startTime)
|
||||
}
|
||||
|
||||
// GetStartTime 获取应用程序启动时间
|
||||
func (h *HealthHandler) GetStartTime() time.Time {
|
||||
return h.startTime
|
||||
}
|
||||
Reference in New Issue
Block a user