2021-06-18 16:14:31 +03:00
|
|
|
/**
|
|
|
|
* Note: The app must first be built (yarn build) before this script is run
|
|
|
|
* This is the main entry point for the application, a simple server that
|
|
|
|
* runs some checks, and then serves up the app from the ./dist directory
|
|
|
|
* Also includes some routes for status checks/ ping and config saving
|
|
|
|
* */
|
2019-09-01 15:38:13 +03:00
|
|
|
|
2021-06-18 16:14:31 +03:00
|
|
|
/* Include required node dependencies */
|
|
|
|
const serveStatic = require('serve-static');
|
|
|
|
const connect = require('connect');
|
2021-06-04 21:57:43 +03:00
|
|
|
const util = require('util');
|
|
|
|
const dns = require('dns');
|
|
|
|
const os = require('os');
|
2021-06-18 20:01:42 +03:00
|
|
|
const bodyParser = require('body-parser');
|
2021-06-04 21:57:43 +03:00
|
|
|
|
2021-06-18 16:14:31 +03:00
|
|
|
/* Include helper functions */
|
|
|
|
const pingUrl = require('./services/ping'); // Used by the status check feature, to ping services
|
2021-06-18 20:01:42 +03:00
|
|
|
const saveConfig = require('./services/save-config'); // Saves users new conf.yml to file-system
|
2021-06-18 16:14:31 +03:00
|
|
|
const printMessage = require('./services/print-message'); // Function to print welcome msg on start
|
|
|
|
require('./src/utils/ConfigValidator'); // Include and kicks off the config file validation script
|
2021-06-14 22:42:02 +03:00
|
|
|
|
2021-06-18 16:14:31 +03:00
|
|
|
/* Checks if app is running within a container, from env var */
|
2021-06-10 21:46:46 +03:00
|
|
|
const isDocker = !!process.env.IS_DOCKER;
|
|
|
|
|
|
|
|
/* Checks env var for port. If undefined, will use Port 80 for Docker, or 4000 for metal */
|
2021-06-11 22:42:32 +03:00
|
|
|
const port = process.env.PORT || (isDocker ? 80 : 4000);
|
2019-09-01 15:38:13 +03:00
|
|
|
|
2021-06-18 16:14:31 +03:00
|
|
|
/* Attempts to get the users local IP, used as part of welcome message */
|
2021-06-04 21:57:43 +03:00
|
|
|
const getLocalIp = () => {
|
|
|
|
const dnsLookup = util.promisify(dns.lookup);
|
|
|
|
return dnsLookup(os.hostname());
|
2021-06-11 17:23:25 +03:00
|
|
|
};
|
2021-06-04 21:57:43 +03:00
|
|
|
|
2021-06-18 16:14:31 +03:00
|
|
|
/* Gets the users local IP and port, then calls to print welcome message */
|
2021-06-11 17:23:25 +03:00
|
|
|
const printWelcomeMessage = () => {
|
|
|
|
getLocalIp().then(({ address }) => {
|
|
|
|
const ip = address || 'localhost';
|
2021-06-18 16:14:31 +03:00
|
|
|
console.log(printMessage(ip, port, isDocker)); // eslint-disable-line no-console
|
2021-06-11 17:23:25 +03:00
|
|
|
});
|
|
|
|
};
|
2021-06-04 21:57:43 +03:00
|
|
|
|
2021-06-18 16:14:31 +03:00
|
|
|
const printWarning = (msg, error) => {
|
|
|
|
console.warn(`\x1b[103m\x1b[34m${msg}\x1b[0m\n`, error || ''); // eslint-disable-line no-console
|
|
|
|
};
|
|
|
|
|
2021-06-18 20:01:42 +03:00
|
|
|
/* A middleware function for Connect, that filters requests based on method type */
|
|
|
|
const method = (m, mw) => (req, res, next) => (req.method === m ? mw(req, res, next) : next());
|
|
|
|
|
2019-09-01 15:38:13 +03:00
|
|
|
try {
|
|
|
|
connect()
|
2021-06-18 20:01:42 +03:00
|
|
|
.use(bodyParser.json())
|
2021-06-18 16:14:31 +03:00
|
|
|
// Serves up the main built application to the root
|
|
|
|
.use(serveStatic(`${__dirname}/dist`))
|
|
|
|
// During build, a custom page will be served before the app is available
|
|
|
|
.use(serveStatic(`${__dirname}/public`, { index: 'default.html' }))
|
|
|
|
// This root returns the status of a given service - used for uptime monitoring
|
|
|
|
.use('/ping', (req, res) => {
|
2021-06-14 22:42:02 +03:00
|
|
|
try {
|
|
|
|
pingUrl(req.url, async (results) => {
|
|
|
|
await res.end(results);
|
|
|
|
});
|
2021-06-18 16:14:31 +03:00
|
|
|
} catch (e) {
|
|
|
|
printWarning(`Error running ping check for ${req.url}\n`, e);
|
|
|
|
}
|
2021-06-14 22:42:02 +03:00
|
|
|
})
|
2021-06-18 20:01:42 +03:00
|
|
|
.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');
|
|
|
|
}))
|
2021-06-18 16:14:31 +03:00
|
|
|
// Finally, initialize the server then print welcome message
|
2021-06-04 21:57:43 +03:00
|
|
|
.listen(port, () => {
|
2021-06-18 16:14:31 +03:00
|
|
|
try { printWelcomeMessage(); } catch (e) { printWarning('Dashy is Starting...'); }
|
2021-06-04 21:57:43 +03:00
|
|
|
});
|
2019-09-01 15:38:13 +03:00
|
|
|
} catch (error) {
|
2021-06-18 16:14:31 +03:00
|
|
|
printWarning('Sorry, an error occurred ', error);
|
2019-07-23 18:03:59 +03:00
|
|
|
}
|