Wednesday, September 10, 2008

Programming web apps - Part 1

I've had a lot of experience in designing web apps over the past few years, and one of the biggest things that I've learned is the value of environments. I learned bits/pieces of this concept many years ago, but now I've put it into full practice on the applications that I design for. Right now I carry a Linux based laptop (a Dell Inspiron 1100) and it's a great little on the road warrior. (That was the subject of my recent Bluetooth tethering to phone issue.) Many years ago when my first child was on the way I was working at home for myself. In the process of the pregnancy, we needed to be at the hospital for monitoring. I found that was my most productive time as I was able to take my application in development and simply code on the road. I had to setup my configuration files though to allow for differences in how PHP worked on the Windows platform (my old laptop platform) and how my server(s) were configured.

The art of CVS/SVN taught me a few things when I worked for another employer. The problem that we ran into was keeping track of what files were supposed to go where. We circumvented this when we made files that looked at where they were living to determine what configurations were supposed to be in play. You can see an example of this below:



// Just add to this to determine the live URL's for the site.
$live_domain_array = array("tempo","www.mylivesite.com");
// Just add to this to determine the test URL's for the site.
$testing_domain_array = array("tempodev","tempotest");

switch(true)
{
// ***************************************************
// ** D E V E L O P M E N T S E R V E R **
// ***************************************************
// First see if we're on on the development server
case in_array(strtolower($_SERVER['HTTP_HOST']),$testing_domain_array):
{
$application_folder = "application/tempo_dev";
$GLOBALS['server'] = 'test';
$GLOBALS['profiler'] = TRUE;
break;
}
// ***************************************************
// ** T E S T S E R V E R **
// ***************************************************
// First see if we're on on the test server
case in_array(strtolower($_SERVER['HTTP_HOST']),$testing_domain_array):
{
$application_folder = "application/tempo_test";
$GLOBALS['server'] = 'test';
$GLOBALS['profiler'] = TRUE;
break;
}
// ***************************************************
// ** P R O D U C T I O N S E R V E R **
// ***************************************************
// First see if we're on on the live server
default:
{
$application_folder = "application/tempo_prod";
$GLOBALS['server'] = 'production';
}



This approach is very easy to implement through my local hosts file on whatever computer I'm using:

192.168.1.64 tempo tempodev tempotest tempoadmin

Then in my Apache configuration file I can do this:



DocumentRoot /home/sites/tempo
DirectoryIndex index.php
ServerAlias tempo tempodev tempotest tempoadmin
#ServerName www.mylivesite.com



So . . . then when I copy the files to whatever environment I'm in, by virtue of the right hosts file entry, it knows what site to serve up. I'm currently working with CodeIgniter, so this is a little peek into a file like the /controller/database.php:


$db['default']['hostname'] = "localhost";
$db['default']['username'] = "username";
$db['default']['password'] = "password";

switch ($GLOBALS['server'])
{
case 'live':
case 'production':
$db['default']['hostname'] = "192.168.1.124"; // Production database server
$db['default']['username'] = "username";
$db['default']['password'] = "password";
$db['default']['database'] = "production_database"; //
break;
case 'test':
$db['default']['database'] = "test_database"; // local copy

break;
}


This is just some of the things I've learned . . .I'd be interested in what other developers do also. This is mainly related to my day to day job, but I'm using the same concepts on our Heartland Church web site was well as the child check-in system I'm designing.

1 comment:

Unknown said...

HA! That's so interesting to find you using a switch solution in your CI database config file. I just finished up an "HR Online Application" application in CI. I did much of the work from the office but a little bit was from home and some from, oddly enough, the hospital where my little preemie daughter currently resides. Talk about funny stuff. It's great to find someone else using a solution I came across. Typically I'll do something and then wonder if it's a good solution or not - but I was pretty confident about this little one.