|
1 | 1 | import logging |
| 2 | +from pathlib import Path |
2 | 3 |
|
3 | 4 | from fastapi import FastAPI |
4 | 5 | from fastapi.middleware.cors import CORSMiddleware |
| 6 | +from fastapi.responses import HTMLResponse |
5 | 7 |
|
6 | 8 | from app.admin.router import router as admin_router |
7 | 9 | from app.applicants.router import router as applicants_router |
|
33 | 35 | Base.metadata.create_all(bind=engine) |
34 | 36 | logger.info("Database tables created") |
35 | 37 |
|
| 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 |
| 48 | + html_path = Path(__file__).parent / "templates" / "index.html" |
| 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 |
| 58 | + |
| 59 | + |
| 60 | +# Root endpoint with API documentation links |
| 61 | +@app.get("/", response_class=HTMLResponse, include_in_schema=False) |
| 62 | +def root(): |
| 63 | + """Render an HTML page with links to API documentation.""" |
| 64 | + return _landing_page_template |
| 65 | + |
| 66 | + |
36 | 67 | # Add routes |
37 | 68 | app.include_router(cases_router) |
38 | 69 | app.include_router(applicants_router) |
|
0 commit comments