423baff73b
- Docker bridge 网络隔离(8000 端口封死) - Gunicorn 4 Worker 多进程 - Alembic 数据库迁移基线 - 日志轮转 20m×3 - JWT 密钥 + DB 密码 + CORS 收紧 - 3-2-1 备份链路(NAS + R740-B 冷备) - 连接池 pool_pre_ping + pool_recycle=3600
32 lines
948 B
Python
32 lines
948 B
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
健康检查端点
|
|
用于 Nginx/LB 探活和数据库连接状态探测。
|
|
"""
|
|
|
|
from fastapi import APIRouter, Depends
|
|
from sqlalchemy import text
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.database import get_db
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/health", summary="健康检查", tags=["系统"])
|
|
async def health_check(db: AsyncSession = Depends(get_db)):
|
|
"""
|
|
探测服务与数据库连接是否存活。
|
|
- 数据库可达 → {"status": "healthy", "database": "connected"}
|
|
- 数据库不可达 → {"status": "degraded", "database": "disconnected", "detail": "..."}
|
|
"""
|
|
try:
|
|
await db.execute(text("SELECT 1"))
|
|
return {"status": "healthy", "database": "connected"}
|
|
except Exception as e:
|
|
return {
|
|
"status": "degraded",
|
|
"database": "disconnected",
|
|
"detail": str(e),
|
|
}
|