- Refactored multiple service and repository methods to accept context as a parameter, enhancing consistency and enabling better control over request lifecycles. - Updated handlers to utilize context in method calls, improving error handling and performance. - Cleaned up Dockerfile by removing unnecessary whitespace.
96 lines
1.8 KiB
Go
96 lines
1.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"time"
|
|
|
|
"carrotskin/pkg/database"
|
|
"carrotskin/pkg/redis"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// HealthCheck 健康检查,检查依赖服务状态
|
|
func HealthCheck(c *gin.Context) {
|
|
ctx, cancel := context.WithTimeout(c.Request.Context(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
checks := make(map[string]string)
|
|
status := "ok"
|
|
|
|
// 检查数据库
|
|
if err := checkDatabase(ctx); err != nil {
|
|
checks["database"] = "unhealthy: " + err.Error()
|
|
status = "degraded"
|
|
} else {
|
|
checks["database"] = "healthy"
|
|
}
|
|
|
|
// 检查Redis
|
|
if err := checkRedis(ctx); err != nil {
|
|
checks["redis"] = "unhealthy: " + err.Error()
|
|
status = "degraded"
|
|
} else {
|
|
checks["redis"] = "healthy"
|
|
}
|
|
|
|
// 根据状态返回相应的HTTP状态码
|
|
httpStatus := http.StatusOK
|
|
if status == "degraded" {
|
|
httpStatus = http.StatusServiceUnavailable
|
|
}
|
|
|
|
c.JSON(httpStatus, gin.H{
|
|
"status": status,
|
|
"message": "CarrotSkin API health check",
|
|
"checks": checks,
|
|
"timestamp": time.Now().Unix(),
|
|
})
|
|
}
|
|
|
|
// checkDatabase 检查数据库连接
|
|
func checkDatabase(ctx context.Context) error {
|
|
db, err := database.GetDB()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
sqlDB, err := db.DB()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 使用Ping检查连接
|
|
if err := sqlDB.PingContext(ctx); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 执行简单查询验证
|
|
var result int
|
|
if err := db.WithContext(ctx).Raw("SELECT 1").Scan(&result).Error; err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// checkRedis 检查Redis连接
|
|
func checkRedis(ctx context.Context) error {
|
|
client, err := redis.GetClient()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if client == nil {
|
|
return errors.New("Redis客户端未初始化")
|
|
}
|
|
|
|
// 使用Ping检查连接
|
|
if err := client.Ping(ctx).Err(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|