Externalizing Configurations in Drupal, Node.js and Grails

In this post, we cover how configurations can be externalize for platform Drupal and frameworks Node.js, Grails. By externalizing configurations, it becomes easer to deploy and move around applications and libraries.

Drupal

There are two approaches
1. Configurations in file
In the sites/default/settings.php add the configurations as follows:

$conf['global_driver_url'] = 'localhost';
$conf['global_driver_port'] = '3000';

The $conf is Drupals global variable array as described here
Then to access it anywhere as follows:

global $conf;
var driverPort = $conf['global_driver_port'];
var driverUrl = $conf['global_driver_url'];

So, here you reference the global variable $conf and then access any variables you wish

2. Configurations from User
We create a content type that contains the configurations. For example, we have content-type name ‘Interface Configurations’ with machine name ‘interface_configurations’

Then in preprocessor in template.php file, we load it as follows:

$nid_config = db_select('node', 'n')
        ->fields('n', array('nid'))
        ->fields('n', array('type'))
        ->condition('n.type', 'interface_configurations')
        ->execute()
        ->fetchCol();
    //load the configurations
    $configurationNode = node_load($nid_config);
    $interfaceConfig = array();
    $interfaceConfig['driver_url'] = $configurationNode->field_driver_url['und'][0]['value'];
    $interfaceConfig['driver_port'] = $configurationNode->field_driver_port['und'][0]['value'];
    $vars['interfaceConfig'] = $interfaceConfig;

Here, we first retrieve the Id of the configuration node containing the configurations. Afterward, it is loaded and values retrieved. At last, it is put in $vars array, so it is available in the html template

Node.js

There are several ways doing it in Node.js and there are some modules such as nconf for the that purpose. In the following, is one way of externalizing configurations in Node.js framework:
1. we create config.js that contains configurations and place it in root folder as follows:

var config = {}

config.controller_url = process.env.CONTROLLER_URL || '/minnehaha';
config.controller_host = process.env.CONTROLLER_HOST || 'localhost';
config.controller_port = process.env.CONTROLLER_PORT || 8010;

module.exports = config;

Here we first declare variable config that will hold the configurations. Then assign our configurations. The ‘process’ contains the command line Node.js variables. So, if you like to have environment based configurations such as dev, prod, etc, then you will have to if/switch condition on ‘process.env.NODE_env’ variable.

Important to note, that the ‘process.env’ queries all of the command environment variables(i.e. PATH,GRAILS_HOME,etc.), so you have access to all of them.

At last, we make or configurations available anywhere in Node.js applications in the next step

2. To make these configurations available, we import the module and then access the configurations as follows:

var config = require('./../config');
...
   var options = {
        host: config.controller_host,
....

Here, the config module is imported and assigned to ‘config’ variable after which the configurations are accessed

Grails

For external configurations in Grails framework,please, see post Externalizing Configurations Per Grails Application

Useful Links:

  • http://stackoverflow.com/questions/5869216/how-to-store-node-js-deployment-settings-configuration-files
  • http://devincarlson.ca/blog/configuring-drupal-use-proxy-server-when-making-external-http-requests

Leave a Reply

Your email address will not be published. Required fields are marked *