Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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 )=-


In reply to mod_perl2 Apache VirtualHost ENV settings [OT?] by graq

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-25 07:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found