Is it possible to use the .powrc file to pass session variables to Rails based on the URL being accessed?

StackOverflow https://stackoverflow.com/questions/10954945

  •  13-06-2021
  •  | 
  •  

Question

I have a Rails 2 app that serves multiple domains. That is, http://domainA.com and http://domainB.com are both served by the same Rails app. When I launch these manually, I specify which site I want to see by passing a site variable: site=domainB ruby script/server.

I'd like to use Pow so that I can access both sites via http://domainA.myapp.dev and http://domainB.myapp.dev (I'd also be happy with http://domainA.dev and http://domainB.dev if that's easier).

I can do this manually by adding export site="domainB" to my .powrc file, and editing that by hand (then doing touch tmp/restart.txt) each time I want to switch sites ... I'd prefer something a bit more automatic, though. I'm thinking something like the equivalent of subdomain == domainA ? export site="domainA" : export site="domainB" within the .powrc file.

Was it helpful?

Solution 2

I figured out how to do this, and wrote a blog post about it here. This is the gist of how it's done...

I configured my before_filter to get the domain that's being accessed, and to use that throughout the Rails app. If the site is being accessed via the standard Rails app, it won't have a domain (it would just be localhost). In that case, the before_filter looks for the site variable being passed at the command line (and if that's not passed, then it uses a default site).

def set_site
  if RAILS_ENV == "development"
    session[:site] = case request.domain
      when "domainA.dev" then "domainA"
      when "domainB.dev" then "domainB"
      else ENV['site'] || "domainA"
    end
  else session[:site].blank?
    if RAILS_ENV == "staging"
      session[:site] = case request.subdomains.last # *.yourstagingdomain.com
        when "domainA" then "domainA"
        when "domainB" then "domainB"
      end
    elsif RAILS_ENV == "production"
      session[:site] = case request.domain
        when "domainA.com" then "domainA"
        when "domainB.com" then "domainB"
        else "domainA"
      end
    else
      session[:site] = "domainA" # default
    end
  end
  if @site.nil?
    @site ||= Site.find_by_name(session[:site])
  end
end

The whole thing is actually done within Rails itself, and the only involvement that Pow has is that there has to be a symlink for each site being served by the Rails app.

The symlinks must also match the request.domain that's being checked in the before_filter. So, in this example there would be two symlinks - domainA and domainB.

OTHER TIPS

I've written the following rake task to switch sites with rake pow[SITENAME] until I can find a more automated solution. This code is also available as a Gist.

desc "Switches site that Pow serves"
task :pow, :site_name do |t, args|
  pow_config = "#{Rails.root}/.powrc"
  args.with_defaults(:site_name => "domainA")

  # Overwrite .powrc file with new site name
  file = File.open(pow_config, 'w')
  file.write "if [ -f \"$rvm_path/scripts/rvm\" ] && [ -f \".rvmrc\" ]; then
source \"$rvm_path/scripts/rvm\"
source \".rvmrc\"
fi

export site=#{args.site_name}"
  file.close

  # Restart Pow
  FileUtils.touch "#{Rails.root}/tmp/restart.txt"

  # Announce site change
  puts "Switched to #{args.site_name}"
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top