-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigPlatform.js
More file actions
201 lines (165 loc) · 5.03 KB
/
configPlatform.js
File metadata and controls
201 lines (165 loc) · 5.03 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var bodyParser = require('body-parser');
var redis = require('node-redis');
var client = redis.createClient();
var publisher = redis.createClient();
var jsonFile = require('jsonfile');
var configObj = {};
var testConfigObj = {};
configObj.configs = [];
testConfigObj.configs = [];
var file = 'LKGC.txt';
client.on('connect', function()
{
console.log('Connected to Redis!');
client.hvals('config', function(err, object)
{
if(err) console.log('\nError getting config from Redis: ' + err);
else
{
console.log('Retrieving and building config state');
console.log('----------------------------------------');
object.forEach(function(item)
{
console.log('\nRetrieved item from Redis: ' + item);
configObj.configs.push(JSON.parse(item));
});
}
console.log('Successfully built config object!\n');
});
client.hvals('testConfig', function(err, object)
{
if(err) console.log('\nError getting test config from Redis: ' + err);
else
{
console.log('Retrieving and building test config state');
console.log('----------------------------------------');
object.forEach(function(item)
{
console.log('\nRetrieved item from Redis: ' + item);
testConfigObj.configs.push(JSON.parse(item));
});
}
console.log('Successfully built test config object!');
});
});
// parse application/json
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }));
app.use('/', express.static(__dirname + '/public'));
app.post('/AddConfig', function(req, res)
{
// get the new config key/val
var objString = JSON.stringify(req.body);
var obj = JSON.parse(objString);
console.log('Request: ' + req.body);
// add it to the local list so we can make sure the LKGC is updated
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);
client.hdel('config', config.id);
}
});
client.hmset('config', obj.id, JSON.stringify(obj));
console.log('\nWriting object: ' + JSON.stringify(obj));
configObj.configs.push(obj);
console.log('\nPublishing message on master channel...');
publisher.publish('master', JSON.stringify(obj));
res.json(configObj);
});
app.post('/AddTestConfig', function(req, res)
{
console.log('Current test config state is :');
console.log(JSON.stringify(testConfigObj));
// get the new config key/val
var objString = JSON.stringify(req.body);
var obj = JSON.parse(objString);
// add it to the local list so we can make sure the LKGC is updated
testConfigObj.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 test config');
testConfigObj.configs.splice(index, 1);
client.hdel('testConfig', config.id);
}
});
client.hmset('testConfig', obj.id, JSON.stringify(obj));
console.log('\nWriting object: ' + JSON.stringify(obj));
testConfigObj.configs.push(obj);
res.json(testConfigObj);
});
app.get('/GetFullConfig', function(req, res)
{
if(configObj !== {})
{
configObj.selected = {};
console.log('\nSending config object:\n' + JSON.stringify(configObj));
res.json(configObj);
} else
{
console.log('\nERROR: no Redis connection!');
res.send('ERROR: no Redis connection!');
}
});
app.get('/GetTestConfig', function(req, res)
{
if(testConfigObj !== {})
{
testConfigObj.selected = {};
console.log('\nSending test config object:\n' + JSON.stringify(testConfigObj));
res.json(testConfigObj);
} else
{
console.log('\nERROR: no Redis connection!');
res.send('ERROR: no Redis connection!');
}
});
app.delete('/DeleteConfig/:id', function(req, res)
{
console.log('Deleting config with ID ' + req.params.id);
client.hdel('config', req.params.id, function(result)
{
configObj.configs.forEach(function(config, index)
{
if(config.id == req.params.id)
{
console.log('Removing config with ID ' + config.id);
var deleted = configObj.configs.splice(index, 1);
console.log('\nPublishing DELETE message master_DELETE channel...');
publisher.publish('master_DELETE', JSON.stringify(deleted[0]));
}
});
configObj.selected = {}
res.json(configObj);
});
});
app.delete('/DeleteTestConfig/:id', function(req, res)
{
console.log('Deleting test config with ID ' + req.params.id);
client.hdel('testConfig', req.params.id, function(result)
{
testConfigObj.configs.forEach(function(config, index)
{
if(config.id == req.params.id)
{
console.log('Removing test config with ID ' + config.id);
var deleted = testConfigObj.configs.splice(index, 1);
}
});
testConfigObj.selected = {}
res.json(testConfigObj);
});
});
server.listen(8000, function()
{
console.log("Server listening on: http://localhost:8000");
});