graq has asked for the wisdom of the Perl Monks concerning the following question:
I have a module that handles database connections. It is used by standard CGI scripts as well as mod_perl2 modules.
The server hosts multiple sites, set up using Apache's VirtualHost configuration.
Using SetEnv (or PerlSetEnv), polutes across to other Apache requests, as %ENV is treated much like a mass global variable for all Apache's requests under MP2
Here is an example sample of code that does not work:
package MYDBCONFIG; use Apache2::RequestUtil; my %db = ( 'production' => { 'db1' => { 'host' => 'host1', 'name' => 'dbname1', 'user' => 'user1', 'pass' => 'pass1' }, 'db2' => { 'host' => 'host2', 'name' => 'dbname2', 'user' => 'user2', 'pass' => 'pass2' }, }, 'qa' => { 'db1' => { 'host' => 'host3', 'name' => 'dbname3', 'user' => 'user3', 'pass' => 'pass3' }, }, ); our ( $MYDBCONFIG ); # Used elsewhere for other 'magical' backwards co +de compatiblity sub new() { my $class = shift; my $dbname = shift; # Sanity checks. die( "No database name supplied") unless ($dbname); if( $ENV{'MOD_PERL'} ){ my $r = Apache2::RequestUtil->request(); $MYDBCONFIG = $r->subprocess_env('MYDB'); $r->log_error(__PACKAGE__.":[MP]: MYDB: $MYDBCONFIG"); } else { $MYDBCONFIG = $ENV{'MYDB'}; print STDERR __PACKAGE__.":[notMP]: MYDB: $MYDBCONFIG"; } my $dbhash = $db{$MYDBCONFIG}{$dbname}; die( "DB '$dbname' not defined / permitted") unless $dbhash; bless $dbhash, $class; }
This then allows MP2 and scripts alike to connect via ORMs (Class::DBI & DBIx::Class) to which ever DB is appropriate
The aim being to then configure apache for www.mysite.com and qa.mysite.com to use exactly the same code, but restrict access to db2, and use a different set of data for QA (allowing them to CRUD as much as they like).
-=( Graq )=-
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: mod_perl2 Apache VirtualHost ENV settings [OT?]
by perrin (Chancellor) on Feb 06, 2008 at 19:27 UTC | |
by graq (Curate) on Feb 07, 2008 at 09:03 UTC | |
by perrin (Chancellor) on Feb 07, 2008 at 20:48 UTC | |
Re: mod_perl2 Apache VirtualHost ENV settings [OT?]
by redhotpenguin (Deacon) on Feb 06, 2008 at 19:32 UTC |