Skip to content

Commit b0d5b63

Browse files
committed
Adds an endpoint for rebuilding the app, so that it can be triggered from the UI
1 parent 6337e5d commit b0d5b63

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

server.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ const dns = require('dns');
1313
const os = require('os');
1414
const bodyParser = require('body-parser');
1515

16-
/* Include helper functions */
16+
/* Include helper functions and route handlers */
1717
const pingUrl = require('./services/ping'); // Used by the status check feature, to ping services
1818
const saveConfig = require('./services/save-config'); // Saves users new conf.yml to file-system
1919
const printMessage = require('./services/print-message'); // Function to print welcome msg on start
20+
const rebuild = require('./services/rebuild-app'); // A script to programmatically trigger a build
2021
require('./src/utils/ConfigValidator'); // Include and kicks off the config file validation script
2122

2223
/* Checks if app is running within a container, from env var */
@@ -39,6 +40,7 @@ const printWelcomeMessage = () => {
3940
});
4041
};
4142

43+
/* Just console.warns an error */
4244
const printWarning = (msg, error) => {
4345
console.warn(`\x1b[103m\x1b[34m${msg}\x1b[0m\n`, error || ''); // eslint-disable-line no-console
4446
};
@@ -64,7 +66,7 @@ try {
6466
}
6567
})
6668
// POST Endpoint used to save config, by writing conf.yml to disk
67-
.use('/api/save', method('POST', (req, res) => {
69+
.use('/config-manager/save', method('POST', (req, res) => {
6870
try {
6971
saveConfig(req.body, (results) => {
7072
res.end(results);
@@ -73,10 +75,18 @@ try {
7375
res.end(JSON.stringify({ success: false, message: e }));
7476
}
7577
}))
78+
// GET endpoint to trigger a build, and respond with success status and output
79+
.use('/config-manager/rebuild', (req, res) => {
80+
rebuild().then((response) => {
81+
res.end(JSON.stringify(response));
82+
}).catch((response) => {
83+
res.end(JSON.stringify(response));
84+
});
85+
})
7686
// Finally, initialize the server then print welcome message
7787
.listen(port, () => {
7888
try { printWelcomeMessage(); } catch (e) { printWarning('Dashy is Starting...'); }
7989
});
8090
} catch (error) {
81-
printWarning('Sorry, an error occurred ', error);
91+
printWarning('Sorry, a critical error occurred ', error);
8292
}

services/rebuild-app.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* This script programmatically triggers a production build
3+
* and responds with the status, message and full output
4+
*/
5+
const { exec } = require('child_process');
6+
7+
module.exports = () => new Promise((resolve, reject) => {
8+
const buildProcess = exec('npm run build');
9+
10+
let output = '';
11+
12+
buildProcess.stdout.on('data', (data) => {
13+
process.stdout.write(data);
14+
output += data;
15+
});
16+
17+
buildProcess.on('error', (error) => {
18+
reject(Error({
19+
success: false,
20+
error,
21+
output,
22+
}));
23+
});
24+
25+
buildProcess.on('exit', (response) => {
26+
const success = response === 0;
27+
const message = `Build process exited with ${response}: `
28+
+ `${success ? 'Success' : 'Possible Error'}`;
29+
resolve({ success, message, output });
30+
});
31+
});

0 commit comments

Comments
 (0)