health.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. """
  2. 健康检查接口
  3. 提供轻量级的服务状态检查
  4. """
  5. from fastapi import APIRouter, Depends
  6. from sqlalchemy.orm import Session
  7. from sqlalchemy import text
  8. from app.database import get_db
  9. from app.utils.timezone import beijing_now
  10. from datetime import datetime
  11. router = APIRouter()
  12. @router.get("/")
  13. async def health_check(db: Session = Depends(get_db)):
  14. """
  15. 健康检查接口
  16. 返回服务状态和基本信息
  17. """
  18. try:
  19. # 检查数据库连接
  20. db.execute(text("SELECT 1"))
  21. return {
  22. "code": 200,
  23. "msg": "服务正常",
  24. "data": {
  25. "status": "healthy",
  26. "timestamp": beijing_now().isoformat(),
  27. "version": "1.0.0"
  28. }
  29. }
  30. except Exception as e:
  31. return {
  32. "code": 500,
  33. "msg": "服务异常",
  34. "data": {
  35. "status": "unhealthy",
  36. "timestamp": beijing_now().isoformat(),
  37. "error": str(e)
  38. }
  39. }