π A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!
Vue awesomedashboarddockerhomelabhomepagemitnodejsorganizationproductivitypwaself-hostedstartpagevue
Got the save config route working in the node server
Alicia Sykes760c464c19df
diff --git a/server.js b/server.jsindex 3bd98f74..35183640 100644--- a/server.js+++ b/server.js@@ -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, () => {diff --git a/services/save-config.js b/services/save-config.jsindex 4ce8ab2c..bc74c201 100644--- a/services/save-config.js+++ b/services/save-config.js@@ -1,30 +1,12 @@-const fs = 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) => {+/**+ * 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;++module.exports = async (newConfig, render) => {// Define constants for the config fileconst 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 APIconst 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}`));+ });++ // 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}`));+ });- // 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!',- // }));- // });+ // If successful, then render hasn't yet been called- call it+ await render(getRenderMessage(true));};