Got the save config route working in the node server

This commit is contained in:
Alicia Sykes 2021-06-19 13:47:10 +01:00
parent 106103a7df
commit 760c464c19
2 changed files with 31 additions and 54 deletions

View File

@ -63,13 +63,15 @@ try {
printWarning(`Error running ping check for ${req.url}\n`, e);
}
})
.use('/api', method('GET', (req, res) => res.end('hi!')))
// POST Endpoint used to save config, by writing conf.yml to disk
.use('/api/save', method('POST', (req, res) => {
saveConfig(req.body, async (results) => {
await res.end(results);
});
// res.end('Will Save');
try {
saveConfig(req.body, (results) => {
res.end(results);
});
} catch (e) {
res.end(JSON.stringify({ success: false, message: e }));
}
}))
// Finally, initialize the server then print welcome message
.listen(port, () => {

View File

@ -1,30 +1,12 @@
const fs = require('fs').promises;
/**
* This file exports a function, used by the write config endpoint.
* It will make a backup of the users conf.yml file
* and then write their new config into the main conf.yml file.
* Finally, it will call a function with the status message
*/
const fsPromises = require('fs').promises;
/* Copies an existing file to a new file */
async function backupConfig(fromPath, toPath, done) {
try {
fs.copyFile(fromPath, toPath, done({ success: true }));
} catch (error) {
done({
success: false,
message: `Error backing up config file: ${error.message}`,
});
}
}
/* Creates a new file and writes content to it */
async function saveNewConfig(writePath, fileContents, done) {
try {
fs.writeFile(writePath, fileContents, done({ success: true }));
} catch (error) {
done({
success: false,
message: `Error writing changes to config file: ${error.message}`,
});
}
}
module.exports = (newConfig, render) => {
module.exports = async (newConfig, render) => {
// Define constants for the config file
const settings = {
defaultLocation: './public/',
@ -44,34 +26,27 @@ module.exports = (newConfig, render) => {
const getSuccessMessage = () => `Successfully backed up ${settings.defaultFile} to`
+ ` ${backupFilePath}, and updated the contents of ${defaultFilePath}`;
// Encoding options for writing to conf file
const writeFileOptions = { encoding: 'utf8' };
// Prepare the response returned by the API
const getRenderMessage = (success, errorMsg) => JSON.stringify({
success,
message: !success ? errorMsg : getSuccessMessage(),
});
// Backs up the config, then writes new content to the existing config, and returns
backupConfig(defaultFilePath, backupFilePath, (backupResult) => {
if (!backupResult.success) {
render(getRenderMessage(false, backupResult.message));
} else {
saveNewConfig(defaultFilePath, newConfig.config, (copyResult) => {
if (copyResult.failed) render(getRenderMessage(false, copyResult.message));
render(getRenderMessage(true));
});
}
});
// Makes a backup of the existing config file
await fsPromises.copyFile(defaultFilePath, backupFilePath)
.catch((error) => {
render(getRenderMessage(false, `Unable to backup conf.yml: ${error}`));
});
// Promise.resolve().then(() => {
// backupConfig(defaultFilePath, backupFilePath)
// .catch(error => thereWasAnError(error));
// }).then(() => {
// saveNewConfig(defaultFilePath, newConfig)
// .catch(error => thereWasAnError(error));
// }).then(() => {
// render(JSON.stringify({
// success: !failed,
// message: failed ? errorMessage : 'Success!',
// }));
// });
// Writes the new content to the conf.yml file
await fsPromises.writeFile(defaultFilePath, newConfig.config.toString(), writeFileOptions)
.catch((error) => {
render(getRenderMessage(false, `Unable to write changes to conf.yml: ${error}`));
});
// If successful, then render hasn't yet been called- call it
await render(getRenderMessage(true));
};