Wednesday, May 23, 2012

Getting started with gofigure

gofigure

gofigure is a configuration tool for node to help in the gathering and monitoring of configuration files in node.

Installation

npm install gofigure

or

git clone git@github.com:Pollenware/gofigure.git

Getting Started

Loading configurations

gofigure currently handles the loading of JSON files for configurations.

To Get an instance of a configuration object use the gofigure method. The gofigure method takes an object that accepts the following options

  • locations : an array of directories that contain your configurations.
  • files : an array of files that contain your configurations.
  • monitor : set to true to monitor changes to configuration files.
  • ignoreMissing : By default gofigure will ignore missing directories. Set this to false to prevent the ignoring of missing configuration directories.
  • environment : By default will look for process.env.NODE_ENV if this is not set then gofigure will read all properties. If you wish to explicity set the environment then set this property.

var gofigure = require("gofigure");

//Loader for directory of configurations
var dirLoader = gofigure({
  locations : [__dirname + "/configs"]
});


//Loader for files of configurations
var dirLoader = gofigure({
  files : [process.env.HOME + "/configs/config1.json", __dirname + "/config1.json"]
});

You can load configurations asynchronously

loader.load(function(err, config){
    var PORT = config.port, HOST = config.host;
});

or synchronously

var gofigure = require("gofigure");

var loader = gofigure({locations : [__dirname + "/configs"]});
var config = loader.loadSync();

Directories of configurations

To load directories that contain configuration files in the options object provide locations property that is an array of directories than contain your configurations.


var gofigure = require("gofigure");

var loader = gofigure({locations : [__dirname + "/configs"]});
loader.load(function(err, config){
    var PORT = config.port, HOST = config.host;
});

The order of the locations matter as it defines a precedence for files. For example suppose you have a directory of default configuration files, and on production you want to override those configuration with environment specific configurations with out changing your module or source controlled files.

var gofigure = require("gofigure");

var loader = gofigure({locations : ["/prod/configs", __dirname + "/configs"]});
loader.load(function(err, config){
    var PORT = config.port, HOST = config.host;
});

Here any production configuration files found in /prod/configs will override the properties in __dirname + "/configs".

Another use case might be in development where you have default properties and instead of altering the source controlled files the developer can override them by putting them in their home directory.

var gofigure = require("gofigure");
var HOME = process.env.HOME;

var loader = gofigure({locations : [ HOME + "/yourApp/configs", __dirname + "/configs"]});
loader.load(function(err, config){
    var PORT = config.port, HOST = config.host;
});

Files

You may also load specific files rather than entire directories.

var gofigure = require("gofigure");

var loader = gofigure({files : ["/prod/configs/config1.json", __dirname + "/config.json"]});
loader.load(function(err, config){
    var PORT = config.port, HOST = config.host;
});

Again order matters /prod/configs/config1.json will override __dirname + "/config.json"

Monitoring

gofigure supports the monitoring of changes to configuration files.

All files

To enable monitoring you can specify monitor to true in the options.

var gofigure = require("gofigure");

var loader = gofigure({monitor : true, files : ["/prod/configs/config1.json", __dirname + "/config.json"]});
var config = loader.loadSync();

loading.on("my.cool.property", function(newValue){
  //...do something
});

Individual Files

To monitor certain files you can use the files property and with object that have a monitor : true KV pair.

var gofigure = require("gofigure");

var loader = gofigure({files : [
  { 
    file : "/prod/configs/config1.json", 
    monitor : true

  }, 
  __dirname + "/config.json"
]});
var config = loader.loadSync();

loading.on("my.cool.property", function(newValue){
  //...do something
});

Just config1.json will be monitored for changes.

Property topic syntax

To listen to all properties

loading.on(function(config){
  //...do something
});

loading.on(function(nameOfPropertyChanged, config){
  //...do something
});

loading.on(function(nameOfPropertyChanged, value, config){
  //...do something
});

To listen to specific properties

loading.on("my.cool.property", function(newValue){
  //...do something
});

loading.on("my.cool.property", function(newValue, config){
  //...do something
});

loading.on("my.cool.property", function(nameOfPropertyChanged, value, config){
  //...do something
});

Wild cards


//listen to any property changed on the my.cool object
loading.on("my.cool.*", function(propName, newValue){
  //...do something
});


//listen to the change of a property named 'property' on any object
//that is a member of my
loading.on("my.*.property", function(propName, newValue){
  //...do something
});

//listen to the change of a property named 'property' that is
//a member of a property called cool
loading.on("*.cool.property", function(propName, newValue){
  //...do something
});

//listen to the change of property or otherProperty on the my.cool object.
loading.on("my.cool.{property|otherProperty}", function(propName, newValue){
  //...do something
});

//listen to the change of property or otherProperty on the my cool or 
//notCool object.
loading.on("my.{cool|notCool}.{property|otherProperty}", function(propName, newValue){
  //...do something
});

Callback Arguments

The property change callback will pass in the following values depending on the arity of the callback.

If 1 argument is expected then just the callback invoked with the new value is a.


loading.on("my.cool.property", function(newValue){
  //...do something
});

If two arguments are expected then it is invoked with the property name and the new value.


loading.on("my.cool.property", function(propName, newValue){
  //...do something
});

Other wise the callback is invoked with the propertyName, newValue and the configuration object.


loading.on("my.cool.property", function(propName, newValue, configObject){
  //...do something
});

Environments

gofigure also supports environments, by default it will look for NODE_ENV and if it is set then it will use it.

The following is an example configuration file


{
    "development": {
        "logging":{
            "patio":{
                    "level":"DEBUG",
                    "appenders":[
                        {
                            "type":"RollingFileAppender",
                            "file":"/var/log/myApp/patio.log"
                        },                              
                        {
                            "type":"ConsoleAppender"
                        }
                    ]
                }
        },
        "app" : {
          "host" : "localhost",
          "port" : "8088"
        },
        "MYSQL_DB" : "mysql://test:testpass@localhost:3306/dev_db",
        "MONGO_DB" : "mongodb://test:testpass@localhost:27017/dev_db"
    },
    "production": {
        "logging":{
            "patio":{
                    "level":"ERROR",
                    "appenders":[
                        {
                            "type":"RollingFileAppender",
                            "file":"/var/log/myApp/patio.log"
                        }
                    ]
                }
        },
        "app" : {
          "host" : "prod.mydomain.com",
          "port" : "80"
        },
        "MYSQL_DB" : "mysql://test:testpass@prod.mydomain.com:3306/prod_db",
        "MONGO_DB" : "mongodb://test:testpass@prod.mydomain.com:27017/prd_db"        
    },
    "test": {
        "logging":{
            "patio":{
                    "level":"INFO",
                    "appenders":[
                        {
                            "type":"RollingFileAppender",
                            "file":"/var/log/myApp/patio.log"
                        }                                                       
                    ]
                }
        },
        "app" : {
          "host" : "test.mydomain.com",
          "port" : "80"
        },
        "MYSQL_DB" : "mysql://test:testpass@test.mydomain.com:3306/test_db",
        "MONGO_DB" : "mongodb://test:testpass@test.mydomain.com:27017/test_db"        
    }
}

To load just the development properties set the environment to development.


var gofigure = require("gofigure"),
    patio = require("patio"),
    mongoose = require("mongoose"),
    comb = require("comb"),
    DB, HOST, PORT;


var loader = gofigure({
  files : [__dirname + "/config-env.json"],
  environment : "development"
})
  .on("MYSQL_DB", function(uri){
    patio.connect(uri);
  })
  .on("MONGO_DB", function(uri){
    mongoose.connect(uri);
  })
  .on("logging", function(logging){
    new comb.logging.PropertyConfigurator().configure(logging);
    patio.configureLogging(logging);
  })
  .on("app", function(app){
    //...
  })
  .load(function(){
    //do something
  })

Wrap up

I hope this is useful is getting you started with gofigure and the potential uses of the library.

Please submit issues to github issues.

As always pull requests are welcome!

3 comments:

  1. 후불결제 건의 경우 세금계산서가 당월 말일자로 발행되어 익월 발송됩니다. 마닐라와 마카티의 비지니스 금융지역과 가까운 거리에 위치하고 있다. 260개의 객실을 보유하고 있으며 호텔내 레스토랑, 바, 수영장, 헬스, 비지니스 센터, 연회장등의 인터넷카지노 시설을 완비하고 있다.

    ReplyDelete
  2. At one finish is a wheel, with a notch in the table the place the vendor stands. The table is roofed with a felt structure with packing containers for the numbers 1 through 36 organized in three columns and 12 rows. At the tip of the portion of the structure closest to the vendor, above the numbers 1, 2, and three, are packing containers for zero and 00. Each of the numbers 1 through 36 is surrounded by both a purple or black oval or rectangle. This rectangular grid, with a box for each quantity, is used for wagers known as "inside bets." On the grand scale of American 카지노 사이트 on line casino video games, roulette has one of many smallest followings, with nowhere close to the recognition of slot machines, video poker, blackjack, or craps.

    ReplyDelete