chore: 初始化项目结构与登录鉴权模块
后端 FastAPI 六层结构(api/services/repositories/models/schemas/core), 统一响应信封、全局异常、日志脱敏;6 张表经 Alembic 迁移建库,SQL 留存 db/migration;登录鉴权含图形验证码(Redis GETDEL 防重放)、JWT、登录限流、 登出黑名单(TTL=令牌剩余有效期)、登录日志。 前端 Vue3 + TS + Element Plus 骨架,含登录页、路由守卫、请求封装与 Vite 代理。 工程配置:uv 锁定 Python 3.12、ruff 检查与格式化、editorconfig、gitattributes。 测试 184 项(单元 155 / 集成 29)全绿,浏览器端到端 8 项通过。 Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
commit
273ebf4657
101 files changed
+8725
No files matched your search
@@ -0,0 +1,52 @@
|
||||
"""健康检查接口。
|
||||
|
||||
路由写 `def`(同步)而非 `async def`:FastAPI 会把同步路由放入线程池执行,
|
||||
因此这里的数据库与 Redis 探测不会阻塞事件循环。
|
||||
"""
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter
|
||||
from sqlalchemy import text
|
||||
|
||||
from app.core.db import get_session_factory
|
||||
from app.core.redis_client import get_redis
|
||||
from app.core.response import ApiResponse, ok
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(tags=["系统"])
|
||||
|
||||
|
||||
def _check_db() -> bool:
|
||||
"""探测数据库连通性。失败只记日志并返回 False,不抛出。"""
|
||||
try:
|
||||
factory = get_session_factory()
|
||||
with factory() as session:
|
||||
session.execute(text("SELECT 1"))
|
||||
return True
|
||||
except Exception:
|
||||
logger.warning("数据库健康检查未通过", exc_info=True)
|
||||
return False
|
||||
|
||||
|
||||
def _check_redis() -> bool:
|
||||
"""探测 Redis 连通性。失败只记日志并返回 False,不抛出。"""
|
||||
try:
|
||||
return bool(get_redis().ping())
|
||||
except Exception:
|
||||
logger.warning("Redis 健康检查未通过", exc_info=True)
|
||||
return False
|
||||
|
||||
|
||||
@router.get("/health", response_model=ApiResponse[dict[str, Any]], summary="健康检查")
|
||||
def health() -> ApiResponse[dict[str, Any]]:
|
||||
"""探活接口:上报应用、数据库、Redis 三项状态。
|
||||
|
||||
即使依赖不可用,本接口本身仍返回 200 并在 data 中标记 false ——
|
||||
这样运维能区分「服务没起来」与「服务起来了但依赖挂了」两种情况。
|
||||
|
||||
注意:生产部署应限制本接口仅内网可达,避免对外暴露依赖拓扑。
|
||||
"""
|
||||
return ok({"app": True, "db": _check_db(), "redis": _check_redis()})
|
||||
Reference in new issue
Block a user