Skip to content

Optimize root endpoint: add template caching and error handling#20

Merged
jbouder merged 2 commits intoroot-landingfrom
copilot/sub-pr-19
Nov 6, 2025
Merged

Optimize root endpoint: add template caching and error handling#20
jbouder merged 2 commits intoroot-landingfrom
copilot/sub-pr-19

Conversation

Copy link
Contributor

Copilot AI commented Nov 6, 2025

Description

Addresses code review feedback from PR #19 by implementing performance and reliability improvements to the root endpoint:

  • Template caching: Load HTML template once at startup instead of on every request
  • Synchronous handler: Changed root() from async to sync (no async I/O performed)
  • Error handling: Graceful degradation with 500 error if template fails to load

Before:

@app.get("/", response_class=HTMLResponse, include_in_schema=False)
async def root():
    html_path = Path(__file__).parent / "templates" / "index.html"
    return html_path.read_text()  # Disk I/O on every request, no error handling

After:

# Template loaded and cached at startup
_landing_page_template: str | None = None
try:
    html_path = Path(__file__).parent / "templates" / "index.html"
    _landing_page_template = html_path.read_text()
except Exception as e:
    logger.error("Failed to load landing page template from %s: %s", html_path, e)

@app.get("/", response_class=HTMLResponse, include_in_schema=False)
def root():
    if _landing_page_template is None:
        return HTMLResponse(
            content="Error: Unable to load the homepage template. Please contact the administrator.",
            status_code=500,
        )
    return _landing_page_template  # Serve from memory

Related Issue

Follow-up to PR #19 (review comments)

Motivation and Context

Original implementation had inefficient I/O patterns and lacked error handling. Template caching eliminates redundant disk reads; error handling prevents unhandled exceptions from missing/corrupted template files.

How Has This Been Tested?

  • Application startup with successful template load
  • Root endpoint returns cached HTML content
  • Ruff linting passes

Screenshots (if appropriate):

Landing page functionality unchanged:

Landing page


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…e sync

Co-authored-by: jbouder <61591423+jbouder@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 6, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • astral.sh
    • Triggering command: curl -LsSf REDACTED (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title [WIP] Add root landing page Optimize root endpoint: add template caching and error handling Nov 6, 2025
Copilot AI requested a review from jbouder November 6, 2025 01:01
@jbouder jbouder marked this pull request as ready for review November 6, 2025 01:13
@jbouder jbouder merged commit ba9a0e6 into root-landing Nov 6, 2025
@jbouder jbouder deleted the copilot/sub-pr-19 branch November 6, 2025 01:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants