This commit is contained in:
2026-09-29 23:33:19 +03:00
parent da32158789
commit e47a45dc93
7 changed files with 667 additions and 0 deletions
+26
View File
@@ -8,7 +8,9 @@ import logging
import time
import uuid
from contextlib import asynccontextmanager
from pathlib import Path
import yaml
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
@@ -24,6 +26,11 @@ logger = logging.getLogger("opencv")
# для /features: сама image-часть проверяется точно после разбора.
_FEATURES_BODY_SLACK_BYTES = 1024 * 1024
# Спецификация — рукописный файл по контракту docs/05: автогенерация FastAPI
# не видит multipart-части, разбираемые вручную. Отдаётся на /openapi.json и
# рендерится в /docs.
_OPENAPI_SPEC_PATH = Path(__file__).resolve().parent.parent / "openapi.yaml"
def create_app(config: Config | None = None) -> FastAPI:
config = config or Config.from_env()
@@ -45,6 +52,7 @@ def create_app(config: Config | None = None) -> FastAPI:
app.state.config = config
app.state.cv = CvService(config)
app.state.ready = False
_install_openapi(app)
app.include_router(router)
app.include_router(health_router)
@@ -97,6 +105,24 @@ def create_app(config: Config | None = None) -> FastAPI:
return app
def _install_openapi(app: FastAPI) -> None:
"""Отдаёт /openapi.json и /docs из openapi.yaml.
Multipart-контракт разбирается вручную (starlette request.form), поэтому
автогенерация FastAPI пуста — спецификация поддерживается как файл.
"""
if not _OPENAPI_SPEC_PATH.exists():
logger.warning("openapi.yaml не найден: /docs отдаст пустую автогенерацию")
return
with _OPENAPI_SPEC_PATH.open(encoding="utf-8") as fh:
spec = yaml.safe_load(fh)
def custom_openapi():
return spec
app.openapi = custom_openapi # type: ignore[method-assign]
def _body_limit(path: str, config: Config) -> int | None:
if path == "/api/v1/verify":
return config.verify_max_body_bytes