Class: Mbeditor::Rack::ResilientRouter

Inherits:
Object
  • Object
show all
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

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
# 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)

  remainder = path[prefix.length..] || ""
  remainder = "/" if remainder.empty?
  env["SCRIPT_NAME"] = prefix
  env["PATH_INFO"] = remainder

  dispatch.call(env)
end