Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Configuration Best Practices for Web Apps

by perrin (Chancellor)
on Jan 24, 2006 at 17:55 UTC ( #525265=note: print w/replies, xml ) Need Help??

in reply to Configuration Best Practices for Web Apps

A deceptively simple question.

One piece of advice, before I suggest an approach: using a massively complicated config module that can do all of this is probably less useful than using a simple one and having some wrappers that cause the specific behavior you want on your system.

I suggest an OO API more like this:

use My::Config; my $config = My::Config->new(project => 'GuestBook'); my $db_password = $config->get('db_password');

Why an OO API? Because it avoids issues with re-using globals in persistent environments. What if you want to use config variables for more than one app in a single script (or handle requests for multiple apps in one mod_perl process)? You can take care to reset your globals, etc., but it's a pain.

Using an OO API means you have to deal with getting the config object frequently, but it should be pretty fast if you keep the parsed files in memory.

In terms of how to set up your actual config files, I'd suggest either having multiple files (e.g. system.conf, app1.conf, app2.conf) and merging them as hashes in memory (so app1.conf overrides system.conf when project eq 'app1') or using a config module with built-in support for overrides. We currenctly use Config::ApacheFormat, which lets you do things like this:

DBPassword overr1de_me <App Falcon> DBPassword chewb@cca </App> <App DeathStar> DBPassword shut_them_@11_d0wn </App>

This will give you a different value for DBPassword depending on the current setting of App. There are other modules that do this too, and of course a simple Perl config does this beautifully.

Where do the smarts about concatenating paths based on projects go? In your config class, which will look at the parsed config data and figure out the answer when you call a method that needs it.

I don't think your session question makes sense here. Sessions are not config data. There's no reason they should use the same mechanism. I suggest making that a separate question with more information. I can't tell what the problem is from what you've said so far.

Replies are listed 'Best First'.
Re^2: Configuration Best Practices for Web Apps
by cbrandtbuffalo (Deacon) on Jan 30, 2006 at 19:09 UTC

    I'm pretty sold on the OO approach, but there are a few additional wrinkles I neglected to mention. I agree that having a perl-based config file is very handy and easy to use. When we do so, we typically want to use info from the machine config inside the app-specific config file. So...

    # In my cgi script... use My::Config; my $config = My::Config->new(project => 'GuestBook'); require $config->get('config_path') . ""; # Now, inside, what's the best way to get at the config # object if I want to use some of the methods? # Just create another new object? It seems wrong to do it again, # but I suppose the overhead # is probably trivial. # Inside package My::App; use My::Config; our $config = My::Config->new(project => 'GuestBook'); if ( $config->get('env_dev') ){ # We're in dev, so set the dev passwords. } # etc.

    Now, how do you feel about then using the $config object as a package variable to access it in other code that is part of the framework but outside the scope of the original script? Using the code above, I could then do this, right?

    package My::App::Page; my $db_password = $My::App::config->get('db_password');
      I would use a class method rather than a package variable. No need to expose that.

      For handling the application scoped and machine scoped configs, I would make it possible to access both through a single object. You might make an application config class that subclasses the machine config class, or you might just have a class that always reads the machine config file in addition to the app config file and merges them internally (i.e. there is no API for accessing the machine config outside of an application context).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://525265]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2020-10-21 16:56 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (222 votes). Check out past polls.