""" app/tenancy/middleware.py ------------------------- Wires tenant resolution into the Flask request lifecycle. `init_tenancy(app)` registers a single app-level before_request handler that: * always clears g.tenant / g.tenant_engine (so downstream code can rely on them) * does NOTHING further when MULTI_TENANT_ENABLED is False → today's behaviour * bypasses static + configured exempt paths (health checks) * otherwise resolves the Host header to a tenant and selects its engine * returns a 404 page for an unknown / unverified / suspended host Flask-SQLAlchemy already removes the scoped session on app-context teardown, so each request rebinds via RoutingSession.get_bind against the fresh g.tenant_engine — no teardown handler is needed here. """ from flask import g, request, current_app, Response from app.tenancy.resolver import resolve_tenant from app.tenancy.engine_cache import get_tenant_engine _UNKNOWN_TENANT_PAGE = ( "
" "" "This address isn’t linked to an active JQC workspace.
" "Check the URL, or contact your administrator.
" "