Files
smart-project-pricing/后端服务/app/routers/health.py
2026-01-31 21:33:06 +08:00

45 lines
1.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""健康检查路由
提供 /health 端点用于 Docker 健康检查
遵循瑞小美部署规范30s interval, 10s timeout, 3 retries
"""
from datetime import datetime
from fastapi import APIRouter, Depends
from sqlalchemy import text
from sqlalchemy.ext.asyncio import AsyncSession
from app.config import settings
from app.database import get_db
router = APIRouter()
@router.get("/health")
async def health_check(db: AsyncSession = Depends(get_db)):
"""健康检查端点
检查内容:
- 应用运行状态
- 数据库连接状态
- 当前时间戳
"""
# 检查数据库连接
db_status = "connected"
try:
await db.execute(text("SELECT 1"))
except Exception as e:
db_status = f"error: {str(e)}"
return {
"code": 0,
"message": "success",
"data": {
"status": "healthy" if db_status == "connected" else "unhealthy",
"version": settings.APP_VERSION,
"database": db_status,
"timestamp": datetime.now().isoformat()
}
}