Class: Mbeditor::Rack::ResilientRouter
- Inherits:
-
Object
- Object
- Mbeditor::Rack::ResilientRouter
- Defined in:
- lib/mbeditor/rack/resilient_router.rb
Overview
Serves mbeditor's own traffic from a private, isolated route set so the editor stays reachable even when the host config/routes.rb is broken.
Sibling of HandlePendingMigrations: both intercept mbeditor requests when the normal path is blocked. This one sits above ActionDispatch::Reloader, so a prefix-matching request is dispatched before any route reload can raise. Requests that don't match the mount prefix pass through untouched.
The matched request is rewritten to mirror how Rails mounts an engine: the prefix is stripped from PATH_INFO and moved to SCRIPT_NAME, so the private set sees the un-prefixed path while the rendered base path stays correct. Dispatch still flows through the mbeditor controllers, so verify_mbeditor_client and resolve_path run unchanged.
Because this middleware sits above the Reloader — and therefore above the host's Cookies/Session/Flash middleware — a bare dispatch would leave resilient-routed requests without a session, breaking session-based authenticate_with. To keep parity with the normal mount, the private set is wrapped in the host app's own cookies/session/flash middleware. The crypto config (key generator, secret_key_base, serializer) is already in env, merged by Rails::Application#call before the stack runs.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ ResilientRouter
constructor
A new instance of ResilientRouter.
Constructor Details
#initialize(app) ⇒ ResilientRouter
Returns a new instance of ResilientRouter.
27 28 29 |
# File 'lib/mbeditor/rack/resilient_router.rb', line 27 def initialize(app) @app = app end |
Instance Method Details
#call(env) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/mbeditor/rack/resilient_router.rb', line 31 def call(env) prefix = Mbeditor::MountPath.resolve path = "#{env["SCRIPT_NAME"]}#{env["PATH_INFO"]}" return @app.call(env) unless matches_prefix?(path, prefix) # Only hijack the request when the host route table is actually broken # (a raising routes.rb wipes every registered route set, including the # engine's). On a healthy request, fall through to the normal engine # mount so the host stack handles it with the correct SCRIPT_NAME and a # working main_app proxy — dispatching healthy traffic through the # private set instead makes the host's own url helpers (e.g. an # authenticate_with redirect) inherit SCRIPT_NAME="/mbeditor" and # wrongly prepend the mount prefix. return @app.call(env) unless host_routes_broken? remainder = path[prefix.length..] || "" remainder = "/" if remainder.empty? env["SCRIPT_NAME"] = prefix env["PATH_INFO"] = remainder dispatch.call(env) end |