-
Notifications
You must be signed in to change notification settings - Fork 4
/
config-reader.js
61 lines (56 loc) · 1.32 KB
/
config-reader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//TODO: make this a class instead of a singleton
var fs = require('fs'),
alarm = require('./alarm');
var CONFIG_LOAD_INTERVAL = 60*1000;
var config, timer;
function loadConfig(sync, callback) {
if (!callback) {
callback = function() {};
}
if (sync) {
try {
config = JSON.parse(fs.readFileSync('config.json'));
} catch(e) {
callback(e);
return;
}
callback(null);
} else {
fs.readFile('config.json', function(err, data) {
if (err) {
callback('error reading config.json', err);
} else {
try {
config = JSON.parse(data.toString());
} catch(e) {
callback(e);
return;
}
callback(null);
}
});
}
}
function getConfig(domain) {
if (config && config.domains && config.domains[domain]) {
alarm.debug('config' + domain + JSON.stringify(config));
return config.domains[domain];
} else {
alarm.debug('config not found' + domain + JSON.stringify(config));
return {};
}
}
module.exports.init = function(callback) {
loadConfig(true, function(err) {
if (err) {
callback(err);
} else {
timer = setInterval(loadConfig, CONFIG_LOAD_INTERVAL);
callback(null);
}
});
};
module.exports.exit = function() {
clearInterval(timer);
};
module.exports.getConfig = getConfig;