From af1ee0ce7173b6506c767841179812d665dbbe26 Mon Sep 17 00:00:00 2001 From: WorldTeacher Date: Tue, 25 Nov 2025 09:15:54 +0100 Subject: [PATCH] test: lazy initiation of catalogue --- api_service.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/api_service.py b/api_service.py index 3f36b08..c074c57 100644 --- a/api_service.py +++ b/api_service.py @@ -1,5 +1,4 @@ -""" -Lightweight Python API service for signature validation +"""Lightweight Python API service for signature validation This can run independently to support the PHP application """ @@ -22,15 +21,22 @@ app.add_middleware( allow_headers=["*"], ) -# Initialize catalogue for signature validation -cat = catalogue.Catalogue() +# Catalogue is expensive to initialize at import time; instantiate lazily +cat = None + + +def _get_catalogue(): + global cat + if cat is None: + cat = catalogue.Catalogue() + return cat @app.get("/api/validate-signature") async def validate_signature(signature: str = Query(...)): """Validate a book signature and return total pages""" try: - book_result = cat.get_book_with_data(signature) + book_result = _get_catalogue().get_book_with_data(signature) if book_result and hasattr(book_result, "pages") and book_result.pages: # Try to extract numeric page count pages_str = str(book_result.pages) @@ -39,7 +45,7 @@ async def validate_signature(signature: str = Query(...)): if match: total_pages = int(match.group(1)) return JSONResponse( - {"valid": True, "total_pages": total_pages, "signature": signature} + {"valid": True, "total_pages": total_pages, "signature": signature}, ) return JSONResponse( @@ -47,13 +53,13 @@ async def validate_signature(signature: str = Query(...)): "valid": False, "error": "Signatur nicht gefunden oder keine Seitenzahl verfügbar", "signature": signature, - } + }, ) except Exception as e: return JSONResponse( { "valid": False, - "error": f"Fehler bei der Validierung: {str(e)}", + "error": f"Fehler bei der Validierung: {e!s}", "signature": signature, }, status_code=500,