Skip to content

Commit

Permalink
Improved state-less server code (#4645)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ylianst committed Oct 24, 2022
1 parent ab02ffa commit cc7670c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 86 deletions.
99 changes: 14 additions & 85 deletions db.js
Original file line number Diff line number Diff line change
Expand Up @@ -1542,20 +1542,6 @@ module.exports.CreateDB = function (parent, func) {
// List all configuration files
obj.listConfigFiles = function (func) { sqlDbQuery('SELECT doc FROM main WHERE type = "cfile" ORDER BY id', func); }

// Get all configuration files (TODO: This is not SQL)
obj.getAllConfigFiles = function (password, func) {
obj.file.find({ type: 'cfile' }).toArray(function (err, docs) {
if (err != null) { func(null); return; }
var r = null;
for (var i = 0; i < docs.length; i++) {
var name = docs[i]._id.split('/')[1];
var data = obj.decryptData(password, docs[i].data);
if (data != null) { if (r == null) { r = {}; } r[name] = data; }
}
func(r);
});
}

// Get database information (TODO: Complete this)
obj.getDbStats = function (func) {
obj.stats = { c: 4 };
Expand Down Expand Up @@ -1796,21 +1782,6 @@ module.exports.CreateDB = function (parent, func) {
});
}

// Get all configuration files
obj.getAllConfigFiles = function (password, func) {
obj.file.query('meshcentral').filter('type', '==', 'cfile').sort('_id').get(function (snapshots) {
const docs = [];
for (var i in snapshots) { docs.push(snapshots[i].val()); }
var r = null;
for (var i = 0; i < docs.length; i++) {
var name = docs[i]._id.split('/')[1];
var data = obj.decryptData(password, docs[i].data);
if (data != null) { if (r == null) { r = {}; } r[name] = data; }
}
func(r);
});
}

// Get database information
obj.getDbStats = function (func) {
obj.stats = { c: 5 };
Expand Down Expand Up @@ -1983,20 +1954,6 @@ module.exports.CreateDB = function (parent, func) {
// List all configuration files
obj.listConfigFiles = function (func) { sqlDbQuery('SELECT doc FROM main WHERE type = "cfile" ORDER BY id', func); }

// Get all configuration files
obj.getAllConfigFiles = function (password, func) {
obj.file.find({ type: 'cfile' }).toArray(function (err, docs) {
if (err != null) { func(null); return; }
var r = null;
for (var i = 0; i < docs.length; i++) {
var name = docs[i]._id.split('/')[1];
var data = obj.decryptData(password, docs[i].data);
if (data != null) { if (r == null) { r = {}; } r[name] = data; }
}
func(r);
});
}

// Get database information (TODO: Complete this)
obj.getDbStats = function (func) {
obj.stats = { c: 4 };
Expand Down Expand Up @@ -2166,20 +2123,6 @@ module.exports.CreateDB = function (parent, func) {

// List all configuration files
obj.listConfigFiles = function (func) { sqlDbQuery('SELECT doc FROM main WHERE type = "cfile" ORDER BY id', func); }

// Get all configuration files
obj.getAllConfigFiles = function (password, func) {
obj.file.find({ type: 'cfile' }).toArray(function (err, docs) {
if (err != null) { func(null); return; }
var r = null;
for (var i = 0; i < docs.length; i++) {
var name = docs[i]._id.split('/')[1];
var data = obj.decryptData(password, docs[i].data);
if (data != null) { if (r == null) { r = {}; } r[name] = data; }
}
func(r);
});
}

// Get database information (TODO: Complete this)
obj.getDbStats = function (func) {
Expand Down Expand Up @@ -2451,20 +2394,6 @@ module.exports.CreateDB = function (parent, func) {
// List all configuration files
obj.listConfigFiles = function (func) { obj.file.find({ type: 'cfile' }).sort({ _id: 1 }).toArray(func); }

// Get all configuration files
obj.getAllConfigFiles = function (password, func) {
obj.file.find({ type: 'cfile' }).toArray(function (err, docs) {
if (err != null) { func(null); return; }
var r = null;
for (var i = 0; i < docs.length; i++) {
var name = docs[i]._id.split('/')[1];
var data = obj.decryptData(password, docs[i].data);
if (data != null) { if (r == null) { r = {}; } r[name] = data; }
}
func(r);
});
}

// Get database information
obj.getDbStats = function (func) {
obj.stats = { c: 6 };
Expand Down Expand Up @@ -2635,20 +2564,6 @@ module.exports.CreateDB = function (parent, func) {
// List all configuration files
obj.listConfigFiles = function (func) { obj.file.find({ type: 'cfile' }).sort({ _id: 1 }).exec(func); }

// Get all configuration files
obj.getAllConfigFiles = function (password, func) {
obj.file.find({ type: 'cfile' }, function (err, docs) {
if (err != null) { func(null); return; }
var r = null;
for (var i = 0; i < docs.length; i++) {
var name = docs[i]._id.split('/')[1];
var data = obj.decryptData(password, docs[i].data);
if (data != null) { if (r == null) { r = {}; } r[name] = data; }
}
func(r);
});
}

// Get database information
obj.getDbStats = function (func) {
obj.stats = { c: 5 };
Expand Down Expand Up @@ -2682,6 +2597,20 @@ module.exports.CreateDB = function (parent, func) {

}

// Get all configuration files
obj.getAllConfigFiles = function (password, func) {
obj.GetAllType('cfile', function (err, docs) {
if (err != null) { func(null); return; }
var r = null;
for (var i = 0; i < docs.length; i++) {
var name = docs[i]._id.split('/')[1];
var data = obj.decryptData(password, docs[i].data);
if (data != null) { if (r == null) { r = {}; } r[name] = data; }
}
func(r);
});
}

func(obj); // Completed function setup
}

Expand Down
4 changes: 3 additions & 1 deletion meshcentral.js
Original file line number Diff line number Diff line change
Expand Up @@ -1189,11 +1189,13 @@ function CreateMeshCentralServer(config, args) {
obj.db.getAllConfigFiles(key, function (configFiles) {
if (configFiles == null) { console.log("Error, no configuration files found or invalid configkey."); process.exit(); return; }
if (!configFiles['config.json']) { console.log("Error, could not file config.json from database."); process.exit(); return; }
if (typeof configFiles['config.json'] == 'object') { configFiles['config.json'] = configFiles['config.json'].toString(); }
if (configFiles['config.json'].charCodeAt(0) == 65279) { configFiles['config.json'] = configFiles['config.json'].substring(1); }
obj.configurationFiles = configFiles;

// Parse the new configuration file
var config2 = null;
try { config2 = JSON.parse(configFiles['config.json']); } catch (ex) { console.log('Error, unable to parse config.json from database.'); process.exit(); return; }
try { config2 = JSON.parse(configFiles['config.json']); } catch (ex) { console.log('Error, unable to parse config.json from database.', ex); process.exit(); return; }

// Set the command line arguments to the config file if they are not present
if (!config2.settings) { config2.settings = {}; }
Expand Down

0 comments on commit cc7670c

Please sign in to comment.