[cgiapp] [announce] OO MVC jumpstart/starter application

Ron Savage ron at savage.net.au
Thu Dec 4 23:37:35 EST 2008


Hi Mark

On Thu, 2008-12-04 at 22:21 -0500, Mark Rajcok wrote:
> On Thu, Dec 4, 2008 at 5:43 PM, Ron Savage <ron at savage.net.au> wrote:
> 
> > The first thing I'd say is about user.cgi:
> > 1) You talk about changing the value of $PRIVATE_HOME_DIR
> > I would use a config file, and have the 2 values of $PRIVATE_HOME_DIR in
> > that file.
> > Yes, I know switching something in the config file (or env) seems to be
> > the same as switching something in the code, but I prefer to keep the
> > code generic.
> 
> 
> Ron, thanks for the feedback.
> I use PRIVATE_HOME_DIR to find the config file... chicken and egg here?
> How would you suggest I rewrite it?   Use another config file just for the
> directory?

Either use FindBin or FindBin::Real, or here's how I do it in
Local::Contacts::Config:

sub new
{
	my($self) = @_;
	my($name) = '.htcontacts.conf';

	my($path);

	for (keys %INC)
	{
		next if ($_ !~ m|Local/Contacts/Config.pm|);

		($path = $INC{$_}) =~ s/Config.pm/$name/;
	}

	# Check [global].

	$$self{'config'}  = Config::IniFiles -> new(-file => $path);
...

So, .htcontacts.conf must be in the same dir as Config.pm.

> > 2) You use $PRIVATE_HOME_DIR = '../../app1_private';
> > OK, so you're not using mod_perl, and didn't mention Apache::Registry,
> > but that '../../' worries me.
> > I take every opportunity to warn beginners that they should be extremely
> > careful about assumptions as to which directory their script thinks is
> > the current directory when it starts. More here:
> >
> > http://perl.apache.org/docs/2.0/user/porting/compat.html#C_Apache__Registry___C_Apache__PerlRun__and_Friends
> >
> 
> So are you suggesting to simply hardcode the path from / instead of trying
> to use relative directory paths (../../)?
> I would like the application to run under mod_perl and Apache::Registry too,
> if possible, but I have little experience with the former, and no experience
> with the latter.

Here is .htcontacts.conf:

[global]

# host:
# o Is one of localhost || quadrahosting
# o Is case-sensitive

host=localhost
verbose=1

[localhost]

AutoCommit=1
database=contacts
driver=Pg
password=seekrit
RaiseError=1
username=boss

contact_tmpl_path=/home/ron/httpd/prefork/htdocs/assets/templates/local/contacts
form_action=/contacts
form_method=post
form_name=contacts
rows_per_page=20
upload_action=/uploads
upload_tmpl_path=/home/ron/httpd/prefork/htdocs/assets/templates/local/uploads
# Warning: yui_path is a URL path, not a disk directory path.
# It points to the directory within the docroot which holds
# the build/ directory of Yahoo's YUI package.
yui_path=/yui
yui_rows_per_page=3

[quadrahosting]

# TBA.

So, by changing the line which says host=localhost to host=quadrahosting
the code switches to a new set of config data.

In reality there would be 2 files witht the same name, one of my dev box
and one on my production box (at my web hosting company QuadraHosting).

The tokens localhost and quadrahosting can of course be anything.

The file name prefix .ht is chosen because of this (in a default Apache
httpd.conf):

<FilesMatch "^\.ht">
    Order allow,deny
    Deny from all
    Satisfy All
</FilesMatch>

which protects it even if it's not under Apache's docroot, but happens
to get copied into somewhere it shouldn't be.


Now, the rest of new() just does a simple validation:

	# Check [global].

	$$self{'config'}  = Config::IniFiles -> new(-file => $path);
	$$self{'section'} = 'global';

	if (! $$self{'config'} -> SectionExists($$self{'section'}) )
	{
		Carp::croak "Config file '$path' does not contain the section
[$$self{'section'}]";
	}

	# Check [x] where x is host=x within [global].

	$$self{'section'} = $$self{'config'} -> val($$self{'section'}, 'host');

	if (! $$self{'config'} -> SectionExists($$self{'section'}) )
	{
		Carp::croak "Config file '$path' does not contain the section
[$$self{'section'}]";
	}

} # End of new().

-- 
Ron Savage
ron at savage.net.au
http://savage.net.au/index.html




More information about the cgiapp mailing list