Skip to content

Commit f3ac8e6

Browse files
author
Johnny Bouder
committed
Validate templates at startup.
1 parent 912b674 commit f3ac8e6

1 file changed

Lines changed: 19 additions & 16 deletions

File tree

app/main.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,32 @@
3535
Base.metadata.create_all(bind=engine)
3636
logger.info("Database tables created")
3737

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
4148
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
4658

4759

4860
# Root endpoint with API documentation links
4961
@app.get("/", response_class=HTMLResponse, include_in_schema=False)
5062
def root():
5163
"""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-
)
6164
return _landing_page_template
6265

6366

0 commit comments

Comments
 (0)