Skip to content

refactor(engine): split asset routes and analytics middleware out of main#115

Merged
x3ek merged 4 commits into
mainfrom
refactor/111-split-main
Jul 3, 2026
Merged

refactor(engine): split asset routes and analytics middleware out of main#115
x3ek merged 4 commits into
mainfrom
refactor/111-split-main

Conversation

@x3ek

@x3ek x3ek commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Closes #111

Pure move refactor, no behavior change:

  • New routers/assets.py: favicon, /pygments.css, /static/user, and theme static routes moved verbatim, registered before the pages catch-all
  • New services/analytics_middleware.py: bot UA detection, track_page_view, and the page-view middleware; registered via BaseHTTPMiddleware with dispatch, which is exactly what @app.middleware("http") expands to, so middleware ordering is unchanged
  • main.py is now pure app factory and wiring (lifespan, exception handlers, session middleware, /health, / redirect, livereload)
  • Test patch targets repointed to the moved symbols

Checks: format, lint, pytest (328), pyright all green.

🤖 Generated with Claude Code

Move favicon, /pygments.css, and static file routes into
routers/assets.py, and the page-view tracking middleware, bot UA
detection, and track_page_view into services/analytics_middleware.py.
main.py is now a pure app factory and wiring module. No behavior,
route, header, or middleware-ordering changes.

Update moved-symbol imports in tests.

Refs #111

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@x3ek x3ek added this to the SquishMark 1.0 milestone Jul 3, 2026
@x3ek x3ek requested a review from Copilot July 3, 2026 12:48

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the FastAPI app wiring by moving asset-related routes and analytics page-view middleware out of main.py into dedicated modules, aiming to keep main.py focused on app construction and router/middleware registration.

Changes:

  • Moved favicon, /pygments.css, user static, and theme static routes into src/squishmark/routers/assets.py and registered the router before the pages catch-all.
  • Extracted bot UA detection + page-view tracking middleware into src/squishmark/services/analytics_middleware.py and registered it from main.py.
  • Updated tests to patch/match the new symbol locations.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
tests/test_pygments_css.py Updates patch target to the moved assets router module.
tests/test_analytics_filtering.py Updates imports/patch targets for moved analytics middleware symbols.
src/squishmark/services/analytics_middleware.py New module for bot UA detection + page-view tracking middleware registration.
src/squishmark/routers/assets.py New router module containing the moved asset/static routes.
src/squishmark/main.py Removes inlined assets routes + analytics middleware and wires the new router/middleware modules.
Comments suppressed due to low confidence (1)

src/squishmark/main.py:141

  • The linked issue’s acceptance criteria says “No route logic in main.py beyond registration”, but main.py still defines route handlers (/health, / redirect, and debug LiveReload endpoints). If the acceptance text is meant to be strict, these should move into routers (or the acceptance criteria should be clarified/updated).

— GitHub Copilot

    # Middleware to track page views (non-blocking)
    register_analytics_middleware(app)

    # Health check endpoint
    @app.get("/health")
    async def health_check() -> dict[str, str]:

Comment thread src/squishmark/services/analytics_middleware.py Outdated
Comment thread src/squishmark/services/analytics_middleware.py Outdated
Comment thread src/squishmark/services/analytics_middleware.py Outdated
Comment thread src/squishmark/services/analytics_middleware.py Outdated
Comment thread src/squishmark/routers/assets.py
Comment thread src/squishmark/routers/assets.py
Comment thread src/squishmark/routers/assets.py
x3ek and others added 3 commits July 3, 2026 07:58
…mplete

Breaking out of the get_db_session loop closed the generator at the
yield point, so the post-yield commit never ran and every flushed page
view was rolled back. Remove the break (the generator yields exactly
once) and add a regression test that persists a view and reads it back
from a fresh session. Also log tracking failures with a traceback and
annotate the middleware signature.

Fixes #118

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The raw path went straight into get_binary_file, so in local content
mode a request like /static/user/..%2f..%2fx.css escaped the content
directory. Reject paths containing '..' or a backslash, or starting
with '/', with a 404 (matching the extension check), mirroring the
theme-static guard. Add tests for traversal rejection and normal
nested serving.

Fixes #119

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@x3ek x3ek merged commit af83c32 into main Jul 3, 2026
5 checks passed
@x3ek x3ek deleted the refactor/111-split-main branch July 3, 2026 13:07
@x3ek x3ek mentioned this pull request Jul 3, 2026
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.

Split asset routes and analytics middleware out of main.py

2 participants