2021-06-11 16:23:25 +02:00
|
|
|
/* eslint-disable no-console */
|
|
|
|
/* This is a simple Node.js http server, that is used to serve up the contents of ./dist */
|
2019-09-01 14:38:13 +02:00
|
|
|
const connect = require('connect');
|
|
|
|
const serveStatic = require('serve-static');
|
|
|
|
|
2021-06-04 20:57:43 +02:00
|
|
|
const util = require('util');
|
|
|
|
const dns = require('dns');
|
|
|
|
const os = require('os');
|
|
|
|
|
2021-06-05 21:21:15 +02:00
|
|
|
require('./src/utils/ConfigValidator');
|
|
|
|
|
2021-06-10 20:46:46 +02:00
|
|
|
const isDocker = !!process.env.IS_DOCKER;
|
|
|
|
|
|
|
|
/* Checks env var for port. If undefined, will use Port 80 for Docker, or 4000 for metal */
|
|
|
|
const port = process.env.PORT || isDocker ? 80 : 4000;
|
2019-09-01 14:38:13 +02:00
|
|
|
|
2021-06-04 20:57:43 +02:00
|
|
|
const getLocalIp = () => {
|
|
|
|
const dnsLookup = util.promisify(dns.lookup);
|
|
|
|
return dnsLookup(os.hostname());
|
2021-06-11 16:23:25 +02:00
|
|
|
};
|
2021-06-04 20:57:43 +02:00
|
|
|
|
2021-06-11 16:23:25 +02:00
|
|
|
const overComplicatedMessage = (ip) => {
|
2021-06-04 20:57:43 +02:00
|
|
|
let msg = '';
|
|
|
|
const chars = {
|
|
|
|
RESET: '\x1b[0m',
|
|
|
|
CYAN: '\x1b[36m',
|
|
|
|
GREEN: '\x1b[32m',
|
|
|
|
BLUE: '\x1b[34m',
|
2021-06-11 16:23:25 +02:00
|
|
|
BRIGHT: '\x1b[1m',
|
2021-06-04 20:57:43 +02:00
|
|
|
BR: '\n',
|
|
|
|
};
|
|
|
|
const stars = (count) => new Array(count).fill('*').join('');
|
|
|
|
const line = (count) => new Array(count).fill('━').join('');
|
|
|
|
const blanks = (count) => new Array(count).fill(' ').join('');
|
2021-06-10 20:46:46 +02:00
|
|
|
if (isDocker) {
|
2021-06-04 20:57:43 +02:00
|
|
|
const containerId = process.env.HOSTNAME || undefined;
|
|
|
|
msg = `${chars.BLUE}${stars(91)}${chars.BR}${chars.RESET}`
|
2021-06-11 16:23:25 +02:00
|
|
|
+ `${chars.CYAN}Welcome to Dashy! 🚀${chars.RESET}${chars.BR}`
|
2021-06-04 20:57:43 +02:00
|
|
|
+ `${chars.GREEN}Your new dashboard is now up and running `
|
|
|
|
+ `${containerId ? `in container ID ${containerId}` : 'with Docker'}${chars.BR}`
|
2021-06-05 13:48:42 +02:00
|
|
|
+ `${chars.GREEN}After updating your config file, run `
|
2021-06-11 16:23:25 +02:00
|
|
|
+ `'${chars.BRIGHT}docker exec -it ${containerId || '[container-id]'} yarn build`
|
2021-06-04 20:57:43 +02:00
|
|
|
+ `${chars.RESET}${chars.GREEN}' to rebuild${chars.BR}`
|
|
|
|
+ `${chars.BLUE}${stars(91)}${chars.BR}${chars.RESET}`;
|
|
|
|
} else {
|
|
|
|
msg = `${chars.GREEN}┏${line(75)}┓${chars.BR}`
|
2021-06-11 16:23:25 +02:00
|
|
|
+ `┃ ${chars.CYAN}Welcome to Dashy! 🚀${blanks(55)}${chars.GREEN}┃${chars.BR}`
|
|
|
|
+ `┃ ${chars.CYAN}Your new dashboard is now up and running at ${chars.BRIGHT}`
|
2021-06-10 20:46:46 +02:00
|
|
|
+ `http://${ip}:${port}${chars.RESET}${blanks(18 - ip.length)}${chars.GREEN}┃${chars.BR}`
|
2021-06-11 16:23:25 +02:00
|
|
|
+ `┃ ${chars.CYAN}After updating your config file, run '${chars.BRIGHT}yarn build`
|
2021-06-04 20:57:43 +02:00
|
|
|
+ `${chars.RESET}${chars.CYAN}' to rebuild the app${blanks(6)}${chars.GREEN}┃${chars.BR}`
|
|
|
|
+ `┗${line(75)}┛${chars.BR}${chars.BR}`;
|
|
|
|
}
|
|
|
|
return msg;
|
2021-06-11 16:23:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* eslint no-console: 0 */
|
|
|
|
const printWelcomeMessage = () => {
|
|
|
|
getLocalIp().then(({ address }) => {
|
|
|
|
const ip = address || 'localhost';
|
|
|
|
console.log(overComplicatedMessage(ip));
|
|
|
|
});
|
|
|
|
};
|
2021-06-04 20:57:43 +02:00
|
|
|
|
2019-09-01 14:38:13 +02:00
|
|
|
try {
|
|
|
|
connect()
|
|
|
|
.use(serveStatic(`${__dirname}/dist`))
|
2021-06-05 15:12:34 +02:00
|
|
|
.use(serveStatic(`${__dirname}/public`, { index: 'default.html' }))
|
2021-06-04 20:57:43 +02:00
|
|
|
.listen(port, () => {
|
2021-06-11 16:23:25 +02:00
|
|
|
try { printWelcomeMessage(); } catch (e) { console.log('Dashy is Starting...'); }
|
2021-06-04 20:57:43 +02:00
|
|
|
});
|
2019-09-01 14:38:13 +02:00
|
|
|
} catch (error) {
|
2021-06-04 20:57:43 +02:00
|
|
|
console.log('Sorry, an error occurred ', error);
|
2019-07-23 17:03:59 +02:00
|
|
|
}
|