Skip to content

Instantly share code, notes, and snippets.

@agustik
Created February 20, 2017 17:00
Show Gist options
  • Select an option

  • Save agustik/c09d2bdfa0b8aa6c426c7e68775cb7ca to your computer and use it in GitHub Desktop.

Select an option

Save agustik/c09d2bdfa0b8aa6c426c7e68775cb7ca to your computer and use it in GitHub Desktop.
Load config, and reload on SIGHUP
'use strict'
const fs = require('fs');
const yaml = require('js-yaml');
const EventEmitter = require('events');
const ev = new EventEmitter();
var configLoader = function (configfile, callback){
callback = callback || function (){};
function readConfig(){
fs.readFile(configfile, 'utf8', function (err, content){
if (err){
ev.emit('error', err);
return callback(err);
}
var config = false;
try {
config = yaml.safeLoad(content);
} catch (e) {
e.emit('error', e);
return callback(e);
}
ev.emit('ready', config);
callback(null, config);
});
}
readConfig();
process.on('SIGHUP', function (){
ev.emit('reload');
readConfig();
});
return ev;
};
module.exports = configLoader;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment