Blog

Brad Wood

October 25, 2012

Spread the word


Share your thoughts

One of the most common server configurations is to have a production server and then 1 or more development or testing servers.  The trick with your "lower" environments is you typically want different settings for logging, error messages, data sources, or outgoing E-mails.  Manually switching settings when you move code is sketchy at best and setting up deployment scripts can be more work than you're willing to take on.
 
Enter ColdBox Environment Control.  ColdBox makes it easy to have different settings for each environment.  In your configuration CFC, you have a configure() method that creates several structs of setting variables.  Let's consider these our default production values.  Next, all you do is create a method for each additional environment such as development(), stage(), etc.  ColdBox will automatically call the appropriate environment override and you can add, remove, or override settings for that environment as you see fit.
 
In the mock example below, you can see that the main production settings are in the configure() method.  The "environments" setting struct declares a list of regular expressions to match against the URL to determine the environment.  When not in production, the appropriate development() or stage() method will be called where it can override or add settings as it sees fit.
 
ColdBox.cfc
component {
 
    function configure() {
        coldbox = {
            setting1 = 'value1',
            setting2 = 'value2',
            setting3 = 'value3'
        };
 
        environments = {
            development = "^dev.,^local.",
            stage = "^stage.,^test."
        };
    }
    
    function development() {
        coldbox.setting1 = 'devValue';
        arrayAppend(interceptors, {class="coldbox.system.interceptors.ColdboxSidebar} );
    }
    
    function stage() {
        coldbox.setting1 = 'stageValue';
    }
}
 
 
P.S. Don't want to use URL to determine your environment?  No problem.  Instead of an environments struct in your config, create a method called detectEnvironment() and simply have it return a string corresponding with the correct environment for that server.  You can base off the machine name, IP address, or even the location of the code on the file system.  It's up to you!

Add Your Comment

(2)

Oct 25, 2012 10:58:35 UTC

by Peter Boughton

Since it's a regex matched against http_host, you need to escape the dot - i.e. development = "^dev\.,^local\.", (Otherwise the dot will match any char in the hostname, so a domain such as "device.com" would incorrectly be considered development.)

Oct 25, 2012 15:16:41 UTC

by Brad Wood

Peter, funny you mentioned that. I've used it that way for years and never noticed, but it occurred to me last night while I was typing this up that the period needed escaped. I left it that way to match the docs, but you have a very good point. I think the saving grace, is that most people have a limited number of host names pointed at their apps.

Recent Entries

Ortus June 2024 Newsletter!

Ortus June 2024 Newsletter!

Welcome to the latest edition of the Ortus Newsletter! This month, we're excited to bring you highlights from our sessions at CFCamp and Open South Code, as well as a sneak peek into our upcoming events. Discover the latest developments in BoxLang, our dynamic new JVM language, and catch up on all the insightful presentations by our expert team. Let's dive in!

Maria Jose Herrera
Maria Jose Herrera
June 28, 2024
BoxLang June 2024 Newsletter!

BoxLang June 2024 Newsletter!

We're thrilled to bring you the latest updates and exciting developments from the world of BoxLang. This month, we're diving into the newest beta release, introducing a new podcast series, showcasing innovative integrations, and sharing insights from recent events. Whether you're a seasoned developer or just getting started, there's something here for everyone to explore and enjoy.

Maria Jose Herrera
Maria Jose Herrera
June 28, 2024
BoxLang 1.0.0 Beta 3 Launched

BoxLang 1.0.0 Beta 3 Launched

We are thrilled to announce the release of BoxLang 1.0.0-Beta 3! This latest beta version is packed with exciting new features and essential bug fixes, including robust encryption functionality, enhanced Java interoperability, and more efficient event handling. Key highlights include the introduction of query caching capabilities, seamless coercion of Java Single Abstract Method (SAM) interfaces from BoxLang functions, and support for virtual thread executors. So, let’s dive into the details of what’s new in BoxLang 1.0.0-Beta 3 and how you can start leveraging these updates today!

Luis Majano
Luis Majano
June 28, 2024