Skip to content

Instantly share code, notes, and snippets.

@julik
Created May 21, 2025 10:35
Show Gist options
  • Save julik/63ac60b67b42cde398fe0069a5af9f3c to your computer and use it in GitHub Desktop.
Save julik/63ac60b67b42cde398fe0069a5af9f3c to your computer and use it in GitHub Desktop.
def enter!(readonly: false)
Current.site = self
@inside = true
set_appsignal_tags!
@shardine = Shardine.new(connection_config_hash: database_config.merge(readonly:))
@shardine.enter!
end
class SwitchingMiddleware
def initialize(app)
@app = app
end
IDEMPOTENT_HTTP_VERBS = %w( GET HEAD OPTIONS QUERY )
def call(env)
site = Site.new(detect_sitename_from_request(env))
return Mappe::NoSiteApp.new.call(env) unless site.configured?
req = Rack::Request.new(env)
readonly = IDEMPOTENT_HTTP_VERBS.include?(req.request_method)
did_enter = site.enter!(readonly:)
status, header, upstream_body = @app.call(env)
body = Rack::BodyProxy.new(upstream_body) { site.leave! }
[status, header, body]
rescue
site.leave! if did_enter
raise
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment