Skip to content

Instantly share code, notes, and snippets.

@speedmax
Created October 8, 2008 11:29
Show Gist options
  • Save speedmax/15495 to your computer and use it in GitHub Desktop.
Save speedmax/15495 to your computer and use it in GitHub Desktop.
cname lookup
class ApplicationController < ActionController::Base
before_filter :load_site
private
def load_site
if request.host =~ Site.domain_pattern
subdomain = current_subdomain
else
subdomain = find_cname(request.host)
end
@site ||= Site.find_by_subdomain(subdomain)
render :text => 'Site not found' unless @site
end
def find_cname(host)
Rails.cache.fetch("dns_map:#{host}") {
Net::DNS::Resolver.new.query(host).each_cname do |cname|
return $1 if cname.match Site.domain_pattern
end
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment