feat: 实现数据库连接和健康检查端点

This commit is contained in:
yangzhaohan
2026-07-09 17:04:56 +08:00
parent 262c8377b9
commit 8ef79eb5f8
4 changed files with 77 additions and 4 deletions
+26
View File
@@ -0,0 +1,26 @@
package handler
import (
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
type HealthHandler struct {
Database *gorm.DB
}
// Check 健康检查服务
func (handler *HealthHandler) Check(context *gin.Context) {
db, err := handler.Database.DB()
if err != nil {
context.JSON(500, gin.H{"error": "database connect error"})
return
}
err = db.Ping()
if err != nil {
context.JSON(500, gin.H{"error": "database ping error"})
return
}
context.JSON(200, gin.H{"status": "healthy"})
}