Created
May 21, 2025 10:35
-
-
Save julik/63ac60b67b42cde398fe0069a5af9f3c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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