From be0341806b90c0bfdbcd9853bfe8ee52d77febe6 Mon Sep 17 00:00:00 2001 From: Johnny Bouder Date: Wed, 5 Nov 2025 19:44:32 -0500 Subject: [PATCH 1/4] Add root html template. Update to render template on root. --- app/main.py | 11 ++++++ app/templates/index.html | 76 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 app/templates/index.html diff --git a/app/main.py b/app/main.py index d7eefed..c0ed2cc 100644 --- a/app/main.py +++ b/app/main.py @@ -1,7 +1,9 @@ import logging +from pathlib import Path from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware +from fastapi.responses import HTMLResponse from app.admin.router import router as admin_router from app.applicants.router import router as applicants_router @@ -33,6 +35,15 @@ Base.metadata.create_all(bind=engine) logger.info("Database tables created") + +# Root endpoint with API documentation links +@app.get("/", response_class=HTMLResponse, include_in_schema=False) +async def root(): + """Render an HTML page with links to API documentation.""" + html_path = Path(__file__).parent / "templates" / "index.html" + return html_path.read_text() + + # Add routes app.include_router(cases_router) app.include_router(applicants_router) diff --git a/app/templates/index.html b/app/templates/index.html new file mode 100644 index 0000000..a492578 --- /dev/null +++ b/app/templates/index.html @@ -0,0 +1,76 @@ + + + + + + Comet API + + + + +
+

🚀 Welcome to the Comet API!

+

Choose your preferred documentation format:

+ +
+ + From a63048d3131b54dcf3b45e5730a2e52449300e00 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 6 Nov 2025 00:53:32 +0000 Subject: [PATCH 2/4] Initial plan From 457a98875f4e11b5abbbe868172854f5f1c0f38f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 6 Nov 2025 00:59:46 +0000 Subject: [PATCH 3/4] Address code review feedback: cache template, add error handling, make sync Co-authored-by: jbouder <61591423+jbouder@users.noreply.github.com> --- app/main.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index c0ed2cc..7c086e3 100644 --- a/app/main.py +++ b/app/main.py @@ -35,13 +35,30 @@ Base.metadata.create_all(bind=engine) logger.info("Database tables created") +# Load and cache the landing page template at startup +_landing_page_template: str | None = None +try: + html_path = Path(__file__).parent / "templates" / "index.html" + _landing_page_template = html_path.read_text() + logger.info("Landing page template loaded successfully") +except Exception as e: + logger.error("Failed to load landing page template from %s: %s", html_path, e) + # Root endpoint with API documentation links @app.get("/", response_class=HTMLResponse, include_in_schema=False) -async def root(): +def root(): """Render an HTML page with links to API documentation.""" - html_path = Path(__file__).parent / "templates" / "index.html" - return html_path.read_text() + if _landing_page_template is None: + logger.error("Landing page template not available") + return HTMLResponse( + content=( + "Error: Unable to load the homepage template. " + "Please contact the administrator." + ), + status_code=500, + ) + return _landing_page_template # Add routes From f3ac8e660e2c2111ddc9d3788191386a33c2bda8 Mon Sep 17 00:00:00 2001 From: Johnny Bouder Date: Thu, 6 Nov 2025 10:53:41 -0500 Subject: [PATCH 4/4] Validate templates at startup. --- app/main.py | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/app/main.py b/app/main.py index 7c086e3..3656678 100644 --- a/app/main.py +++ b/app/main.py @@ -35,29 +35,32 @@ Base.metadata.create_all(bind=engine) logger.info("Database tables created") -# Load and cache the landing page template at startup -_landing_page_template: str | None = None -try: +# Cache for landing page template +_landing_page_template: str = "" + + +@app.on_event("startup") +async def startup_event(): + """Validate application configuration and load required resources.""" + global _landing_page_template + + # Load landing page template - fail fast if not available html_path = Path(__file__).parent / "templates" / "index.html" - _landing_page_template = html_path.read_text() - logger.info("Landing page template loaded successfully") -except Exception as e: - logger.error("Failed to load landing page template from %s: %s", html_path, e) + try: + _landing_page_template = html_path.read_text() + logger.info("Landing page template loaded successfully from %s", html_path) + except FileNotFoundError as exc: + logger.critical("Landing page template not found at %s", html_path) + raise RuntimeError(f"Required template file not found: {html_path}") from exc + except Exception as e: + logger.critical("Failed to load landing page template: %s", e) + raise RuntimeError(f"Failed to load required template: {e}") from e # Root endpoint with API documentation links @app.get("/", response_class=HTMLResponse, include_in_schema=False) def root(): """Render an HTML page with links to API documentation.""" - if _landing_page_template is None: - logger.error("Landing page template not available") - return HTMLResponse( - content=( - "Error: Unable to load the homepage template. " - "Please contact the administrator." - ), - status_code=500, - ) return _landing_page_template