Created
February 20, 2017 17:00
-
-
Save agustik/c09d2bdfa0b8aa6c426c7e68775cb7ca to your computer and use it in GitHub Desktop.
Load config, and reload on SIGHUP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| '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