60 lines
1.7 KiB
Python
60 lines
1.7 KiB
Python
from __future__ import annotations
|
|
|
|
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
|
|
|
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
|
CATALOG_PATH = Path(os.environ.get("CATALOG_PATH", BASE_DIR / "catalog_excel.csv"))
|
|
MATCHER = CatalogMatcher(CATALOG_PATH)
|
|
TEST_INFO_ROUTE_ENV = "ENABLE_TEST_INFO_ROUTE"
|
|
|
|
|
|
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())
|