Files
semapform_api/api_service.py
WorldTeacher 7a74ead335
Some checks failed
PR tests / build-and-smoke (pull_request) Failing after 1m19s
chore: new service check using signature
2025-11-25 09:41:11 +01:00

83 lines
2.4 KiB
Python

"""Lightweight Python API service for signature validation
This can run independently to support the PHP application
"""
import os
import re
# Avoid importing heavy modules at top-level to keep `import api_service` lightweight
from fastapi import FastAPI, Query
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
app = FastAPI(title="Signature Validation API")
# Allow PHP application to call this API
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # In production, restrict to your PHP server domain
allow_credentials=True,
allow_methods=["GET"],
allow_headers=["*"],
)
# Catalogue is expensive to initialize at import time; instantiate lazily
cat = None
def _get_catalogue():
global cat
if cat is None:
# import inside function to avoid expensive work during module import
from bibapi import catalogue as _catalogue
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 = _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)
# Extract first number from pages string (e.g., "245 S." -> 245)
match = re.search(r"(\d+)", pages_str)
if match:
total_pages = int(match.group(1))
return JSONResponse(
{"valid": True, "total_pages": total_pages, "signature": signature},
)
return JSONResponse(
{
"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: {e!s}",
"signature": signature,
},
status_code=500,
)
@app.get("/health")
async def health_check():
"""Health check endpoint"""
return {"status": "ok", "service": "signature-validation"}
if __name__ == "__main__":
import uvicorn
port = int(os.getenv("API_PORT", "8001"))
uvicorn.run(app, host="0.0.0.0", port=port)