81 lines
2.2 KiB
Python
81 lines
2.2 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from app.core.database import get_db
|
|
from app.core.config import settings
|
|
import redis
|
|
import time
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/health")
|
|
async def health_check():
|
|
"""基础健康检查"""
|
|
return {
|
|
"success": True,
|
|
"data": {
|
|
"status": "healthy",
|
|
"service": "cloud-drive-api",
|
|
"environment": settings.ENVIRONMENT,
|
|
"timestamp": int(time.time())
|
|
},
|
|
"message": "API服务运行正常"
|
|
}
|
|
|
|
@router.get("/ready")
|
|
async def readiness_check(db: Session = Depends(get_db)):
|
|
"""就绪检查 - 检查数据库和Redis连接"""
|
|
checks = {}
|
|
|
|
# 检查数据库连接
|
|
try:
|
|
db.execute("SELECT 1")
|
|
checks["database"] = {
|
|
"status": "healthy",
|
|
"message": "数据库连接正常"
|
|
}
|
|
except Exception as e:
|
|
checks["database"] = {
|
|
"status": "unhealthy",
|
|
"message": f"数据库连接失败: {str(e)}"
|
|
}
|
|
raise HTTPException(status_code=503, detail="数据库连接失败")
|
|
|
|
# 检查Redis连接
|
|
try:
|
|
r = redis.from_url(settings.REDIS_URL)
|
|
r.ping()
|
|
checks["redis"] = {
|
|
"status": "healthy",
|
|
"message": "Redis连接正常"
|
|
}
|
|
except Exception as e:
|
|
checks["redis"] = {
|
|
"status": "unhealthy",
|
|
"message": f"Redis连接失败: {str(e)}"
|
|
}
|
|
raise HTTPException(status_code=503, detail="Redis连接失败")
|
|
|
|
# 检查文件存储
|
|
try:
|
|
import os
|
|
os.makedirs(settings.UPLOAD_DIR, exist_ok=True)
|
|
checks["storage"] = {
|
|
"status": "healthy",
|
|
"message": "文件存储正常"
|
|
}
|
|
except Exception as e:
|
|
checks["storage"] = {
|
|
"status": "unhealthy",
|
|
"message": f"文件存储失败: {str(e)}"
|
|
}
|
|
raise HTTPException(status_code=503, detail="文件存储失败")
|
|
|
|
return {
|
|
"success": True,
|
|
"data": {
|
|
"status": "ready",
|
|
"checks": checks,
|
|
"timestamp": int(time.time())
|
|
},
|
|
"message": "所有服务已就绪"
|
|
} |