So I have this idea to reduce code replication in our rails setup.
Currently we have our rails app serving two distinct sites, soon to be
three. At the moment we check out a separate set of code for each site,
and configure it by adjusting the database setting and some config files
that pull in separate style sheets.
To remove this replication I’d like to have a single codebase, but
adjust the databases and config settings based on what site the user is
trying to view, e.g.
http://virtualhost1.us.com ==> use virtualhost1 db & stylesheets
http://virtualhost2.us.com ==> use virtualhost2 db & stylesheets
Currently the two virtual hosts map to two different directories that
both include the same version of our application, just with slightly
different configurations.
It seems fairly straightforward to make the config settings dynamic,
since we can use request.host to work out which host the user is
hitting, and switch style sheets etc. based on that. [note that ENV
can’t be used in this fashion since it appears to only pick up
enviroment variables set for the fcgid process in general]
In principle we could do the same for the database by adjusting
database.yml like so:
development:
adapter: mysql
database: <%= request.env[‘DEV_DATABASE’] %>
host: localhost
username: <%= request.env[‘DEV_DATABASE_USERNAME’]
password: <%= request.env[‘DEV_DATABASE_PASSWORD’]
The idea being to have a virtual host config like:
ServerName virtualhost1.us.com SetEnv DEV_DATABASE virtualhost1 SetEnv DEV_DATABASE_USERNAME virtualhost1_username SetEnv DEV_DATABASE_PASSWORD virtualhost1_password DocumentRoot "/var/www/our_rails_app" Options ExecCGI FollowSymLinks AllowOverride all Allow from all Order allow,deny AddHandler cgi-script .cgi AddHandler fastcgi-script .fcgiHowever the request variable is not available for use in database.yml,
and while the ENV variable is, the following:
development:
adapter: mysql
database: <%= ENV[‘database’] %>
host: localhost
username: <%= ENV[‘database_username’]
password: <%= ENV[‘database_password’]
won’t work because the ENV settings under fcgid don’t pick up anything
set in the virtual host settings, or any other SetEnv directive.
Currently ENV under fcgid appears to only have variables set as part of
the fcgid config which are global for all processes, so this won’t allow
us to dynamically switch the database based on user request.
request.env appears to pick up environment variables set up the Apache
SetEnv directive, and I’ve been trying to work out how this happens in
actionpack, but experiments like the following:
<% cgi = CGI.new %>
<%= debug cgi.send(:env_table)%>
appear to produce access to ENV, i.e. only the global variables, and not
the specific ones to the current virtual host request.
I would be very grateful for any input on how to resolve this problem,
or any information that might help me better understand the difference
between ENV and request.env in rails.
Many thanks in advance.
CHEERS> SAM