test: lazy initiation of catalogue
Some checks failed
PR tests / build-and-smoke (pull_request) Failing after 2m5s
Some checks failed
PR tests / build-and-smoke (pull_request) Failing after 2m5s
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user