Skip to content

Instantly share code, notes, and snippets.

@ntalbott
Created March 6, 2013 15:24
Show Gist options
  • Select an option

  • Save ntalbott/5100053 to your computer and use it in GitHub Desktop.

Select an option

Save ntalbott/5100053 to your computer and use it in GitHub Desktop.
if ENV["STITCH"] == "1"
require "rack-proxy"
class Stitcher < Rack::Proxy
EXACT = %w(/ /terms /privacy)
PREFIX = %w(/pricing /about /support /gateways /assets)
def initialize(app)
@app = app
end
def call(env)
original_host = env["HTTP_HOST"]
rewrite_env(env)
if env["HTTP_HOST"] != original_host
rewrite_response(perform_request(env))
else
@app.call(env)
end
end
def rewrite_env(env)
request = Rack::Request.new(env)
return env unless(request.host == ENV["PUBLIC_FULL_DOMAIN"])
unless(
EXACT.include?(request.path) ||
EXACT.include?(request.path + "/") ||
PREFIX.include?(request.path) ||
PREFIX.any?{|prefix| request.path.starts_with?(prefix + "/")})
env["HTTP_HOST"] = ENV["SUBSCRIPTIONS_INTERNAL_DOMAIN"]
end
env
end
def extract_http_request_headers(env)
headers = super
headers["Host"] = ENV["PUBLIC_FULL_DOMAIN"]
headers
end
end
Rails.application.middleware.insert_before ActionDispatch::Static, Stitcher
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment