-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
47 lines (39 loc) · 1.69 KB
/
server.js
File metadata and controls
47 lines (39 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* 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);