Skip to content

Fix initial page rendering - #4

Merged
vladiant merged 2 commits into
mainfrom
fix_main_page
Mar 14, 2026
Merged

vladiant merged 2 commits into
mainfrom
fix_main_page

Conversation

@vladiant

@vladiant vladiant commented Mar 14, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes Internal Server Error on Render when loading / and makes homepage template loading robust across source and packaged deployments.

This MR also updates packaging so the styled homepage template is always included in built artifacts (wheel/sdist), and keeps a single template source of truth at app/templates/index.html.

Problem

The root page (/) could fail in hosted environments (Render) with TemplateNotFound, causing HTTP 500.

Root Cause

Template path resolution was tied to repository layout assumptions. In packaged/runtime environments, template files may live under installed package paths, not the repo root.

What Changed

1. Resilient template directory resolution

  • Added _resolve_template_directories() in app/main.py to search multiple locations:
    • <repo>/templates
    • <site-packages>/app/templates
    • <cwd>/templates
  • Jinja2Templates now uses discovered existing directories.

2. Safe fallback for missing template

  • Wrapped root-page template rendering in try/except TemplateNotFound.
  • If template is unavailable, return a minimal HTML page with links to:
    • /docs
    • /redoc
    • /health
  • Prevents 500 on / in degraded template scenarios.

3. Packaging fixes for template inclusion

  • Updated pyproject.toml:
    • include-package-data = true
    • package discovery via [tool.setuptools.packages.find] include = ["app*"]
    • package data rule: app = ["templates/*.html"]
  • Ensures app/templates/index.html is included in wheel/sdist.

4. Single source of truth for homepage template

  • Moved template from templates/index.html to app/templates/index.html.
  • Removed duplicate root template file.

5. Regression test coverage

  • Added test to verify / still returns 200 when template loading raises TemplateNotFound.
  • Keeps behavior protected against future regressions.

Files Changed

  • app/main.py
  • pyproject.toml
  • app/templates/index.html (moved from templates/index.html)
  • tests/test_api.py

Validation

Executed targeted tests:

  • tests/test_api.py::test_home_page_endpoint
  • tests/test_api.py::test_home_page_endpoint_falls_back_when_template_missing

Result: both passed.

Also validated package artifact contents during development:

  • app/templates/index.html present in built wheel/sdist.
  • app subpackages included correctly via package discovery.

Impact

  • No API contract changes.
  • Improves deployment reliability for Render and other packaged environments.
  • Root endpoint degrades gracefully instead of failing with 500 when template files are missing.

Rollback Plan

  • Revert this MR to restore previous template loading behavior and packaging config.
  • No data migration or schema rollback required.

@vladiant
vladiant merged commit 9f964b8 into main Mar 14, 2026
1 check passed
@vladiant
vladiant deleted the fix_main_page branch March 14, 2026 14:59
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.

1 participant