Files
dashy/server.js
2026-04-12 19:02:49 +01:00

48 lines
1.7 KiB
JavaScript

/**
* This is the main entry point for the web server which serves Dashy
* Reads some config, runs some checks, prints a lil welcome, starts server
* Registers the Express app.js, which has all the routes, middleware, auth
* Note: The app must first be built (yarn build) before this script is run
* */
const dns = require('dns');
const os = require('os');
const http = require('http');
const app = require('./services/app');
const sslServer = require('./services/ssl-server');
const printMessage = require('./services/print-message');
/* Checks if app is running within a container, from env var */
const isDocker = !!process.env.IS_DOCKER;
/* Checks env var for port. If undefined, will use Port 8080 for Docker, or 4000 for metal */
const port = process.env.PORT || (isDocker ? 8080 : 4000);
/* Checks env var for host. If undefined, will use 0.0.0.0 */
const host = process.env.HOST || '0.0.0.0';
/* Gets the users local IP and port, then calls to print welcome message */
const printWelcomeMessage = () => {
dns.promises.lookup(os.hostname())
.then(({ address }) => {
const ip = process.env.HOST || address || 'localhost';
console.log(printMessage(ip, port, isDocker)); // eslint-disable-line no-console
})
.catch(() => {
console.log(`Dashy server has started (${port})`); // eslint-disable-line no-console
});
};
/* Create HTTP server from app on port, and print welcome message */
http.createServer(app)
.listen(port, host, () => {
printWelcomeMessage();
})
.on('error', (err) => {
console.warn('Unable to start Dashy\'s Node server\n', err); // eslint-disable-line no-console
});
/* Check, and if possible start SSL server too */
sslServer.startSSLServer(app);