fix:批量修复了一些问题
This commit is contained in:
+16
-3
@@ -10,6 +10,8 @@ from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from src.core.config import settings
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
|
||||
@@ -19,9 +21,11 @@ async def lifespan(app: FastAPI) -> AsyncIterator[None]:
|
||||
ensure_admin_password_hashed,
|
||||
migrate_plaintext_sensitive_settings,
|
||||
)
|
||||
from src.core.security_check import assert_security_on_startup
|
||||
await init_db() # 验证连接,不建表
|
||||
await migrate_plaintext_sensitive_settings() # 明文敏感配置 → 加密(幂等)
|
||||
await ensure_admin_password_hashed() # .env 明文密码 → scrypt 哈希(幂等)
|
||||
await assert_security_on_startup() # 启动安全校验(生产拒绝/开发警告)
|
||||
yield
|
||||
await close_client()
|
||||
|
||||
@@ -74,14 +78,23 @@ def create_app() -> FastAPI:
|
||||
|
||||
@app.get("/health/ready")
|
||||
async def health_ready():
|
||||
"""就绪检查: 验证数据库连接。"""
|
||||
"""就绪检查:验证数据库连接。
|
||||
|
||||
数据库不可达时返回 HTTP 503,而非 200 + not_ready ——
|
||||
这样 K8s/Compose 的 readinessProbe 才能正确判定「未就绪」并停止流量。
|
||||
"""
|
||||
from src.db.base import engine
|
||||
from fastapi.responses import JSONResponse
|
||||
try:
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(lambda conn: None)
|
||||
return {"status": "ready"}
|
||||
except Exception:
|
||||
return {"status": "not_ready"}
|
||||
except Exception as e:
|
||||
logger.warning("就绪检查失败(数据库不可达): %s", e)
|
||||
return JSONResponse(
|
||||
status_code=503,
|
||||
content={"status": "not_ready", "reason": "database_unreachable"},
|
||||
)
|
||||
|
||||
return app
|
||||
|
||||
|
||||
Reference in New Issue
Block a user