|
35 | 35 | Base.metadata.create_all(bind=engine) |
36 | 36 | logger.info("Database tables created") |
37 | 37 |
|
38 | | -# Load and cache the landing page template at startup |
39 | | -_landing_page_template: str | None = None |
40 | | -try: |
| 38 | +# Cache for landing page template |
| 39 | +_landing_page_template: str = "" |
| 40 | + |
| 41 | + |
| 42 | +@app.on_event("startup") |
| 43 | +async def startup_event(): |
| 44 | + """Validate application configuration and load required resources.""" |
| 45 | + global _landing_page_template |
| 46 | + |
| 47 | + # Load landing page template - fail fast if not available |
41 | 48 | html_path = Path(__file__).parent / "templates" / "index.html" |
42 | | - _landing_page_template = html_path.read_text() |
43 | | - logger.info("Landing page template loaded successfully") |
44 | | -except Exception as e: |
45 | | - logger.error("Failed to load landing page template from %s: %s", html_path, e) |
| 49 | + try: |
| 50 | + _landing_page_template = html_path.read_text() |
| 51 | + logger.info("Landing page template loaded successfully from %s", html_path) |
| 52 | + except FileNotFoundError as exc: |
| 53 | + logger.critical("Landing page template not found at %s", html_path) |
| 54 | + raise RuntimeError(f"Required template file not found: {html_path}") from exc |
| 55 | + except Exception as e: |
| 56 | + logger.critical("Failed to load landing page template: %s", e) |
| 57 | + raise RuntimeError(f"Failed to load required template: {e}") from e |
46 | 58 |
|
47 | 59 |
|
48 | 60 | # Root endpoint with API documentation links |
49 | 61 | @app.get("/", response_class=HTMLResponse, include_in_schema=False) |
50 | 62 | def root(): |
51 | 63 | """Render an HTML page with links to API documentation.""" |
52 | | - if _landing_page_template is None: |
53 | | - logger.error("Landing page template not available") |
54 | | - return HTMLResponse( |
55 | | - content=( |
56 | | - "Error: Unable to load the homepage template. " |
57 | | - "Please contact the administrator." |
58 | | - ), |
59 | | - status_code=500, |
60 | | - ) |
61 | 64 | return _landing_page_template |
62 | 65 |
|
63 | 66 |
|
|
0 commit comments