Setting up your ColdBox 3.0 application for development and production

This is nothing new to ColdBox 3.0 and has been in the framework for a few versions now, but I wanted to show how you can have the same application for both Development, staging, testing and production without changing any configurations. Well at least not once it has been setup anyway.

The file we are going to look at is the configuration file Config.cfc, in which we are going to make the very first assumption, which is every thing that we are going to configure is for production. Then we will make a small addition to the config file that will enable other environments.

So lets look at some of the very basic items you might setup for production.

debugMode = false,
reinitPassword = "ProductionPassword",
handlersIndexAutoReload = false,
configAutoReload = false,
handlerCaching = true,
eventCaching = true,

modules = {
   autoReload = false,
   include = [],
   exclude = []
};

So we have basically said here is that we don't want to be in debug mode, we are setting the framework reinit with the password Production Password, and the config will not auto reload, we are going to cache the handlers and last but not least events will be cached as well.

I have included the modules section here as well and if you are not using modules you can ignore this, however if you are going to use modules we have set it up so these are not auto reloaded.

This is pretty much the standard information that you will need for production, and should then ignore from changing any more.

So if we are in development, how do we then know that we might want to have the debugMode, or even have the config auto reload. Then we need to add the following to the Config.cfc file, which will give us some control on the machine it is running on.

What we will do now is add the following to the bottom of the config file.

string function detectEnvironment() {
   return replace(createObject( 'java', 'java.net.InetAddress' ).getLocalHost().getHostName(),"-","","all" );
}

What we have said here is that when we start the Application we can detect the machine that we are running on, now this is only a snippet of what I use. But you could change the replace to a regex or something that you need to clean up the string, and the reason behind this is that we are going to need to have something that can be used as a method. Which means if you have a machine name that is say home-PC or maybe dev-01, we clean it up so that it is either homePC or dev01.

With that in mind we are now going to add the following code.

void function dev01() {
   coldbox.debugMode = true;
   modules.autoReload = false;
}

In this code we have said that we whish to be in debugMode and that the modules will be autoloaded, and that's pretty much it for this machine.

This is pretty straight forward to setup and use, and the only thing that you need to seriously keep in mind that the method defined, will be the a cleaned up version of the machines name.

And that is pretty much it, and is the way that I do this. When you adopt this in your Config.cfc, you can then tune each machine to your desires as you go along.


RT @cfbloggers: Setting up your ColdBox 3.0 application for development and production - http://goo.gl/2NVRC Apr 15, 2011
Setting up your ColdBox 3.0 application for development and production - http://goo.gl/2NVRC Apr 15, 2011

  • Richard Herbert's Gravatar I follow your approach of considering the default settings as those for Production and overwrite them on a per-environment basis.

    What surprises me is that you don't use the environments setting in Coldbox.cfc?

    With some cute regex you can control the function to be called per-environment and overwrtie the settings as required.

    http://wiki.coldbox.org/wiki/ConfigurationCFC.cfm#...
    # Posted By Richard Herbert | 4/15/11 9:24 AM
  • Andrew Scott's Gravatar You can still use that method and if you read further you will see the detecttEnviornment option as well.

    This is how you use the detectEnviornment.
    # Posted By Andrew Scott | 4/15/11 4:59 PM
  • Richard Herbert's Gravatar True. I guess my environments have always been simply identifiable enough that regex has been fine so I've never needed to use detectEnvironment() :-)

    Either way, this is one of the most useful, out-of-the-box, features of ColdBox which I think sets it apart from some of the more minimalistic frameworks.
    # Posted By Richard Herbert | 4/16/11 7:09 AM
  • Andrew Scott's Gravatar That statement is something that one can't argue with.

    I would also like to point out that there is another reason that one might not think about when using this, if you have a team of developers. Another team member can make changes to do what they want to run the application as for development, that differs to the next team member.
    # Posted By Andrew Scott | 4/16/11 1:56 PM