Add regression corpus and opt-in catalog test route
This commit is contained in:
1 parent
476cbb0116
commit
c7b19d1310
7 files changed
+491
-13
No files matched your search
+42
-11
@@ -4,6 +4,7 @@ import os
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from app.matcher import CatalogMatcher
|
||||
from app.models import MatchRequest, MatchResponse
|
||||
@@ -11,18 +12,48 @@ from app.models import MatchRequest, MatchResponse
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
CATALOG_PATH = Path(os.environ.get("CATALOG_PATH", BASE_DIR / "catalog_excel.csv"))
|
||||
MATCHER = CatalogMatcher(CATALOG_PATH)
|
||||
|
||||
# The task asks for one application endpoint. OpenAPI/Swagger routes are disabled
|
||||
# so the running service exposes only POST /match.
|
||||
app = FastAPI(
|
||||
title="Offline catalog matcher",
|
||||
version="0.2.0",
|
||||
docs_url=None,
|
||||
redoc_url=None,
|
||||
openapi_url=None,
|
||||
)
|
||||
TEST_INFO_ROUTE_ENV = "ENABLE_TEST_INFO_ROUTE"
|
||||
|
||||
|
||||
@app.post("/match", response_model=MatchResponse)
|
||||
def match(request: MatchRequest) -> MatchResponse:
|
||||
return MatchResponse(results=[MATCHER.match(message) for message in request.messages])
|
||||
|
||||
|
||||
def create_app(*, enable_test_info_route: bool = False) -> FastAPI:
|
||||
# OpenAPI/Swagger routes are disabled so the production application exposes
|
||||
# exactly the single endpoint required by the task.
|
||||
application = FastAPI(
|
||||
title="Offline catalog matcher",
|
||||
version="0.2.0",
|
||||
docs_url=None,
|
||||
redoc_url=None,
|
||||
openapi_url=None,
|
||||
)
|
||||
application.add_api_route(
|
||||
"/match", match, methods=["POST"], response_model=MatchResponse
|
||||
)
|
||||
|
||||
if enable_test_info_route:
|
||||
from app.dev_routes import register_test_info_route
|
||||
|
||||
application.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_methods=["GET", "POST"],
|
||||
allow_headers=["Content-Type"],
|
||||
)
|
||||
register_test_info_route(application, MATCHER)
|
||||
|
||||
return application
|
||||
|
||||
|
||||
def _test_info_route_enabled() -> bool:
|
||||
return os.environ.get(TEST_INFO_ROUTE_ENV, "").strip().lower() in {
|
||||
"1",
|
||||
"true",
|
||||
"yes",
|
||||
"on",
|
||||
}
|
||||
|
||||
|
||||
app = create_app(enable_test_info_route=_test_info_route_enabled())
|
||||
Reference in new issue
Block a user