1
0
mirror of https://github.com/KubaPro010/fm-dx-webserver.git synced 2026-02-27 06:23:53 +01:00

hotfix for config saving

This commit is contained in:
Marek Farkaš
2025-01-11 21:58:52 +01:00
parent 55a57912d1
commit b356f8eee5
3 changed files with 345 additions and 158 deletions

View File

@@ -272,22 +272,12 @@ router.post('/saveData', (req, res) => {
if(configExists() === false) {
firstSetup = true;
}
/* TODO: Refactor to server_config.js */
// Save data to a JSON file
fs.writeFile(configPath, JSON.stringify(serverConfig, null, 2), (err) => {
if (err) {
logError(err);
res.status(500).send('Internal Server Error');
} else {
logInfo('Server config changed successfully.');
if(firstSetup === true) {
res.status(200).send('Data saved successfully!\nPlease, restart the server to load your configuration.');
} else {
res.status(200).send('Data saved successfully!\nSome settings may need a server restart to apply.');
}
}
});
logInfo('Server config changed successfully.');
if(firstSetup === true) {
res.status(200).send('Data saved successfully!\nPlease, restart the server to load your configuration.');
} else {
res.status(200).send('Data saved successfully!\nSome settings may need a server restart to apply.');
}
}
});

View File

@@ -100,22 +100,40 @@ let serverConfig = {
autoShutdown: false,
enableDefaultFreq: false,
defaultFreq: "87.5",
TestTest: "tesst"
};
function deepMerge(target, source) {
// Function to add missing fields without overwriting existing values
function addMissingFields(target, source) {
Object.keys(source).forEach(function(key) {
if (typeof source[key] === 'object' && source[key] !== null) {
if (!target[key]) target[key] = {}; // Create missing object
deepMerge(target[key], source[key]); // Recursively merge
if (typeof source[key] === 'object' && source[key] !== null && !Array.isArray(source[key])) {
if (!target[key]) {
target[key] = {}; // Create missing object
}
addMissingFields(target[key], source[key]); // Recursively add missing fields
} else {
if (target[key] === undefined) {
target[key] = source[key]; // Add missing fields
target[key] = source[key]; // Add missing fields only
}
}
});
configSave();
}
// Function to merge new configuration, overwriting existing values
function deepMerge(target, source) {
Object.keys(source).forEach(function(key) {
if (typeof source[key] === 'object' && source[key] !== null && !Array.isArray(source[key])) {
if (!target[key] || typeof target[key] !== 'object') {
target[key] = {}; // Ensure target[key] is an object before merging
}
deepMerge(target[key], source[key]); // Recursively merge objects
} else {
target[key] = source[key]; // Overwrite or add the value
}
});
}
// Function to update the configuration at runtime
function configUpdate(newConfig) {
if (newConfig.webserver && (newConfig.webserver.banlist !== undefined || newConfig.plugins !== undefined)) {
serverConfig.webserver.banlist = newConfig.webserver.banlist;
@@ -123,9 +141,11 @@ function configUpdate(newConfig) {
delete newConfig.webserver.banlist;
}
deepMerge(serverConfig, newConfig);
deepMerge(serverConfig, newConfig); // Overwrite with newConfig values
configSave();
}
// Function to save the configuration to the file system
function configSave() {
try {
fs.writeFileSync(configPath, JSON.stringify(serverConfig, null, 2));
@@ -135,21 +155,25 @@ function configSave() {
}
}
// Function to check if the configuration file exists
function configExists() {
return fs.existsSync(configPath);
}
// On startup, check for missing fields and add them if necessary
if (configExists()) {
const configFileContents = fs.readFileSync(configPath, 'utf8');
try {
const configFile = JSON.parse(configFileContents);
deepMerge(configFile, serverConfig);
serverConfig = configFile;
addMissingFields(configFile, serverConfig); // Add only missing fields from serverConfig
serverConfig = configFile; // Use the updated configFile as the new serverConfig
configSave(); // Save the merged config back to the file
} catch (err) {
logError('Error parsing config file:', err);
}
}
module.exports = {
configName, serverConfig, configUpdate, configSave, configExists, configPath
};
};