Created
August 11, 2009 23:52
-
-
Save speedmax/166195 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
require 'net/dns/resolver' | |
# Custom Domain | |
# | |
# Require net-dns gem | |
# | |
# A Rack middleware to to resolve the custom domain to original subdomain | |
# for your multi telent application. | |
# | |
# It's all transperant to your application, it performs cname lookup and | |
# overwrite HTTP_HOST if needed | |
# | |
# www.example.org => example.myapp.com | |
# | |
# Credit: Inspired by http://codetunes.com/2009/04/17/dynamic-cookie-domains-with-racks-middleware/ | |
class CustomDomain | |
@@cache = {} | |
def initialize(app, default_domain) | |
@app = app | |
@default_domain = default_domain | |
end | |
def call(env) | |
host, port = env["HTTP_HOST"].split(':') | |
# modify Environment variable HTTP_HOST if custom domain is found | |
if custom_domain?(host) | |
domain = cname_lookup(host) | |
env['HTTP_X_FORWARDED_HOST'] = [host, domain].join(', ') | |
logger.info("CustomDomain: mapped #{host} => #{domain}") | |
end | |
@app.call(env) | |
end | |
def custom_domain?(host) | |
host !~ /#{@default_domain.sub(/^\./, '')}/i | |
end | |
def cname_lookup(host) | |
unless @@cache[host] | |
Net::DNS::Resolver.new.query(host).each_cname do |cname| | |
@@cache[host] = cname if cname.include?(@default_domain) | |
end | |
end | |
@@cache[host] | |
end | |
private | |
def logger | |
RAILS_DEFAULT_LOGGER || Logger.new(STDOUT) | |
end | |
end |
3 years later.. :)
you have to handle all the URL helper and redirection yourself.. redirect_to(:host => domain)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wonder if there is any way of handling redirects after using this middleware? Once you tricked rails to think they are on 'example.myapp.com' redtiect_to method will always redirect user from example.com to example.myapp.com once you use it.