-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathconfig.js
65 lines (59 loc) · 1.44 KB
/
config.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
62
63
64
65
var config = {};
// Db
config.db = {
host: 'localhost',
user: 'root',
pass: '',
name: 'nodervisor',
};
// Application
config.port = 3000;
config.env = 'production';
config.sessionSecret = '1234567890ABCDEF';
// Read and write settings
config.readHosts = function(db, callback){
var query = db('hosts')
.join('groups', 'hosts.idGroup', '=', 'groups.idGroup', 'left')
.select('hosts.idHost', 'hosts.Name', 'hosts.Url', 'groups.Name AS GroupName');
query.exec(function(err, data){
var hosts = {};
for (var host in data) {
hosts[data[host].idHost] = data[host];
}
config.hosts = hosts;
// Call the callback passed
if (callback) {
callback();
}
});
};
config.writeHosts = function(db, newHosts, callback){
var async = require('async');
var hostIds = [];
async.each(newHosts, function(host, callback){
if (host.idHost == '0') {
db('hosts').insert({Name: host.Name, Url: host.Url}).exec(function(err, id){
hostIds.push(Number(id));
return callback(err);
});
} else {
db('hosts').where('idHost', host.idHost).update({Name: host.Name, Url: host.Url}).exec(function(err){
hostIds.push(parseInt(host.idHost, 10));
return callback(err);
});
}
}, function(err){
if (err) {
callback(err);
} else {
db('hosts').whereNotIn('idHost', hostIds).delete().exec(function(err){
if (err) {
callback(err);
} else {
config.readHosts(db, callback);
}
});
}
});
};
module.exports = config;