-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigMon.js
More file actions
84 lines (69 loc) · 2.28 KB
/
Copy pathconfigMon.js
File metadata and controls
84 lines (69 loc) · 2.28 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var redis = require('node-redis');
var subscriber = redis.createClient();
var publisher = redis.createClient();
var jsonFile = require('jsonfile');
var request = require('request');
var configObj = {};
configObj.configs = [];
var file = 'LKGC.txt';
subscriber.on('connect', function()
{
console.log('ConfigMon Redis connection established!');
});
// on startup, load the entire config from the config platform
request('http://localhost:8000/GetFullConfig', function (error, response, body)
{
if (!error && response.statusCode == 200)
{
console.log('\nGetting full config from ConfigPlatform...');
configObj = JSON.parse(body);
WriteLKGC();
}
});
subscriber.on('message', function(channel, message)
{
// parse the JSON string as an object so we can work with it
var obj = JSON.parse(message);
if(channel == 'master')
{
console.log('\nAdding/editing config key with ID ' + obj.id + '...');
// overwrite an existing config if it's an edit
configObj.configs.forEach(function(config, index)
{
if((config.id == obj.id) || ((config.key.toLowerCase() == obj.key.toLowerCase()) &&
config.application.toLowerCase() == obj.application.toLowerCase()))
{
console.log('Replacing config...');
configObj.configs.splice(index, 1);
}
});
configObj.configs.push(obj);
console.log('\nPublishing message on channel ' + obj.application.toLowerCase() + '...');
publisher.publish(obj.application.toLowerCase(), JSON.stringify(obj));
} else if(channel == 'master_DELETE')
{
console.log('\nDeleting config key with ID ' + obj.id + '...');
// find the existing config key and delete it from the object
configObj.configs.forEach(function(config, index)
{
if(config.id == obj.id)
{
console.log('Removing config...');
configObj.configs.splice(index, 1);
}
});
console.log('\nPublishing DELETE message on channel ' + obj.application.toLowerCase() + '...');
publisher.publish(obj.application.toLowerCase() + '_DELETE', JSON.stringify(obj));
}
WriteLKGC();
});
subscriber.subscribe('master');
subscriber.subscribe('master_DELETE');
var WriteLKGC = function()
{
console.log('\nWriting LKGC file with contents:\n' + JSON.stringify(configObj));
jsonFile.writeFile(file, configObj, function (err)
{
if(err) console.error(err);
});
}