mirror of
https://github.com/Lissy93/dashy.git
synced 2024-11-27 00:28:09 +03:00
Got the save config route working in the node server
This commit is contained in:
parent
106103a7df
commit
760c464c19
10
server.js
10
server.js
@ -63,13 +63,15 @@ try {
|
|||||||
printWarning(`Error running ping check for ${req.url}\n`, e);
|
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
|
// POST Endpoint used to save config, by writing conf.yml to disk
|
||||||
.use('/api/save', method('POST', (req, res) => {
|
.use('/api/save', method('POST', (req, res) => {
|
||||||
saveConfig(req.body, async (results) => {
|
try {
|
||||||
await res.end(results);
|
saveConfig(req.body, (results) => {
|
||||||
|
res.end(results);
|
||||||
});
|
});
|
||||||
// res.end('Will Save');
|
} catch (e) {
|
||||||
|
res.end(JSON.stringify({ success: false, message: e }));
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
// Finally, initialize the server then print welcome message
|
// Finally, initialize the server then print welcome message
|
||||||
.listen(port, () => {
|
.listen(port, () => {
|
||||||
|
@ -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 */
|
module.exports = async (newConfig, render) => {
|
||||||
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) => {
|
|
||||||
// Define constants for the config file
|
// Define constants for the config file
|
||||||
const settings = {
|
const settings = {
|
||||||
defaultLocation: './public/',
|
defaultLocation: './public/',
|
||||||
@ -44,34 +26,27 @@ module.exports = (newConfig, render) => {
|
|||||||
const getSuccessMessage = () => `Successfully backed up ${settings.defaultFile} to`
|
const getSuccessMessage = () => `Successfully backed up ${settings.defaultFile} to`
|
||||||
+ ` ${backupFilePath}, and updated the contents of ${defaultFilePath}`;
|
+ ` ${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
|
// Prepare the response returned by the API
|
||||||
const getRenderMessage = (success, errorMsg) => JSON.stringify({
|
const getRenderMessage = (success, errorMsg) => JSON.stringify({
|
||||||
success,
|
success,
|
||||||
message: !success ? errorMsg : getSuccessMessage(),
|
message: !success ? errorMsg : getSuccessMessage(),
|
||||||
});
|
});
|
||||||
|
|
||||||
// Backs up the config, then writes new content to the existing config, and returns
|
// Makes a backup of the existing config file
|
||||||
backupConfig(defaultFilePath, backupFilePath, (backupResult) => {
|
await fsPromises.copyFile(defaultFilePath, backupFilePath)
|
||||||
if (!backupResult.success) {
|
.catch((error) => {
|
||||||
render(getRenderMessage(false, backupResult.message));
|
render(getRenderMessage(false, `Unable to backup conf.yml: ${error}`));
|
||||||
} else {
|
|
||||||
saveNewConfig(defaultFilePath, newConfig.config, (copyResult) => {
|
|
||||||
if (copyResult.failed) render(getRenderMessage(false, copyResult.message));
|
|
||||||
render(getRenderMessage(true));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Promise.resolve().then(() => {
|
// Writes the new content to the conf.yml file
|
||||||
// backupConfig(defaultFilePath, backupFilePath)
|
await fsPromises.writeFile(defaultFilePath, newConfig.config.toString(), writeFileOptions)
|
||||||
// .catch(error => thereWasAnError(error));
|
.catch((error) => {
|
||||||
// }).then(() => {
|
render(getRenderMessage(false, `Unable to write changes to conf.yml: ${error}`));
|
||||||
// saveNewConfig(defaultFilePath, newConfig)
|
});
|
||||||
// .catch(error => thereWasAnError(error));
|
|
||||||
// }).then(() => {
|
// If successful, then render hasn't yet been called- call it
|
||||||
// render(JSON.stringify({
|
await render(getRenderMessage(true));
|
||||||
// success: !failed,
|
|
||||||
// message: failed ? errorMessage : 'Success!',
|
|
||||||
// }));
|
|
||||||
// });
|
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user