-
-
Save josefarias/b52c7016cff6ceae6b3a23c95702d488 to your computer and use it in GitHub Desktop.
This file contains 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
module AccountSlug | |
PATTERN = /(\d{7,})/ | |
FORMAT = '%07d' | |
def self.decode(slug) slug.to_i end | |
def self.encode(id) FORMAT % id end | |
# We're using account id prefixes in the URL path. Rather than namespace | |
# all our routes, we're "mounting" the Rails app at this URL prefix. | |
# | |
# The Extractor middleware yanks the prefix off PATH_INFO, moves it to | |
# SCRIPT_NAME, and drops the account id in env['bc3.account.queenbee_id']. | |
# | |
# Rails routes on PATH_INFO and builds URLs that respect SCRIPT_NAME, | |
# so the main app is none the wiser. We look up the current account using | |
# env['bc3.account.queenbee_id'] instead of request.subdomains.first | |
class Extractor | |
PATH_INFO_MATCH = /\A(\/#{AccountSlug::PATTERN})/ | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
request = ActionDispatch::Request.new(env) | |
# $1, $2, $' == script_name, slug, path_info | |
if request.path_info =~ PATH_INFO_MATCH | |
request.script_name = $1 | |
request.path_info = $'.empty? ? '/' : $' | |
# Stash the account's Queenbee ID. | |
env['bc3.account.queenbee_id'] = AccountSlug.decode($2) | |
end | |
@app.call env | |
end | |
end | |
# Limit session cookies to the slug path. | |
class LimitSessionToAccountSlugPath | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
env['rack.session.options'][:path] = env['SCRIPT_NAME'] if env['bc3.account.queenbee_id'] | |
@app.call env | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment