Improved state-less server code (#4645)

This commit is contained in:
Ylian Saint-Hilaire 2022-10-24 16:58:05 -07:00
parent ab02ffad4d
commit cc7670cc31
2 changed files with 17 additions and 86 deletions

99
db.js
View File

@ -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 };
@ -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 };
@ -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 };
@ -2167,20 +2124,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 };
@ -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 };
@ -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 };
@ -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
}

View File

@ -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 = {}; }