URL-based environment detection

7 views
Skip to first unread message

Gabriel Birke

unread,
Apr 8, 2010, 3:42:51 PM4/8/10
to jquery-claypool
Hi,

I wanted to select my environment on the client side based on the URL
path. For this, I have written the following EnvironmentDetector class
for Claypool, I'd like to share:

(function($, $$) {


$$.EnvironmentDetector = function(options) {
$.extend(true, this, options);
}

$.extend($$.EnvironmentDetector.prototype, {
detect:function(pattern) {
var fieldmatch, config, e, currentUrl;
fieldmatch = "client.clienturl"
if(arguments.length == 2) {
fieldmatch = arguments[0];
}
config = $.config("env");
for(e in config) {
if(e == 'default') {
continue;
}
currentUrl = $.config('env.' + e + '.' + fieldmatch);
if(currentUrl && typeof(currentUrl) == "string" &&
currentUrl.match(pattern)) {
return e;
}
}
return null;
}
});

})(jQuery, Claypool);

In my boot/client.js file I have the following lines:

try{
var d = new Claypool.EnvironmentDetector();
var envname = d.detect(location.href);
$.env('defaults', envname + ".client");
}catch(e){

When I now set the client.clienturl property in my environments, the
environment gets detected. This is nice because I can copy my source
code around without modifying client.js

I hope I haven't duplicated existing functionality. Feel free to
incorporate this into the main Claypool source. Maybe the code can be
modified so it works on the server side too. Comments on how to
improve the code are also appreciated.

Greetings,

Gabriel

chris thatcher

unread,
Apr 8, 2010, 3:54:21 PM4/8/10
to jquery-...@googlegroups.com
Gabriel this is awesome, I get tired of modifying my client.js and server.js boot files when I deploy.  I'll incorporate this for sure.


--
You received this message because you are subscribed to the Google Groups "jquery-claypool" group.
To post to this group, send email to jquery-...@googlegroups.com.
To unsubscribe from this group, send email to jquery-claypo...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jquery-claypool?hl=en.




--
Christopher Thatcher

chris thatcher

unread,
May 18, 2010, 9:44:35 AM5/18/10
to jquery-...@googlegroups.com
Gabriel, I wanted to let you know jquery.claypool 1.1.4 now supports auto detection of environment based on window.location.  I credited you in the commit and in the source code and I have to say it was an excellent insight and has reduced my deployment differences to zero, truly push button.

The implementation I used was a little different and only took about 5 lines in src/core/plugins.js ($.env function).

Now in your projects configs/environments.js you can just do:

(function($){

    $.env({
        automap:{
            'file:///opt':          'dev.server',
            'file:///base':         'appengine.server',
            'http://localhost':     'dev.client',
            'http://www.envjs':     'prod.client'
        },
        defaults:{
            //..
        },
        appengine:{
            server:{
                //..
            }
        },...

And in your boot/client.js or boot/server.js all you need to do is:

jQuery.noConflict();
(function($){
   
    //A static logger for any initialization routines we might add here
    var log = $.logger("EnvJS");
   
    //The environments are described in environments.js
    try{
        //auto select environment based on window.location
        $.env();
     }catch(e){
        log.error("Environmental selection is invalid!").exception(e);
     }
...

Great idea and using in all of my projects now.

Thanks,
Thatcher
--
Christopher Thatcher

Jonathan Cook

unread,
May 18, 2010, 10:08:25 AM5/18/10
to jquery-...@googlegroups.com
Awesome guys!

Sent from my iPhone
Reply all
Reply to author
Forward
0 new messages