117 lines
2.6 KiB
JavaScript
Executable File
117 lines
2.6 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
/**
|
|
* Module dependencies.
|
|
*/
|
|
|
|
const app = require('../app');
|
|
const debug = require('debug')('ztncui:server');
|
|
const http = require('http');
|
|
const https = require('https');
|
|
const fs = require('fs');
|
|
|
|
const options = {
|
|
cert: fs.readFileSync('etc/tls/fullchain.pem'),
|
|
key: fs.readFileSync('etc/tls/privkey.pem')
|
|
}
|
|
|
|
/**
|
|
* Get ports from environment and store in Express.
|
|
*/
|
|
|
|
const http_port = normalizePort(process.env.HTTP_PORT || '3000');
|
|
app.set('http_port', http_port);
|
|
const https_port = normalizePort(process.env.HTTPS_PORT || null);
|
|
app.set('https_port', https_port);
|
|
|
|
/**
|
|
* Get interface address on which to listen for HTTPS requests from env.
|
|
*/
|
|
const https_host = process.env.HTTPS_HOST || null;
|
|
app.set('https_host', https_host);
|
|
|
|
/**
|
|
* Create HTTPS server and listen on localhost only for HTTP and
|
|
* on all network interfaces for HTTPS if HTTPS_PORT is set in env,
|
|
* or on specific interface if HTTPS_HOST is set in env.
|
|
*/
|
|
|
|
app.listen(http_port, 'localhost');
|
|
const server = https.createServer(options, app);
|
|
|
|
if (https_port) {
|
|
if (https_host) {
|
|
console.log('Listening for HTTPS requests on port ' + https_port + ' on address ' + https_host);
|
|
} else {
|
|
console.log('Listening for HTTPS requests on port ' + https_port + ' on all interfaces');
|
|
}
|
|
server.listen(https_port, https_host);
|
|
}
|
|
|
|
server.on('error', onError);
|
|
server.on('listening', onListening);
|
|
|
|
/**
|
|
* Normalize a port into a number, string, or false.
|
|
*/
|
|
|
|
function normalizePort(val) {
|
|
const port = parseInt(val, 10);
|
|
|
|
if (isNaN(port)) {
|
|
// named pipe
|
|
return val;
|
|
}
|
|
|
|
if (port >= 0) {
|
|
// port number
|
|
return port;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Event listener for HTTP/S server "error" event.
|
|
*/
|
|
|
|
function onError(error) {
|
|
if (error.syscall !== 'listen') {
|
|
throw error;
|
|
}
|
|
|
|
const bind = typeof http_port === 'string'
|
|
? 'Pipe ' + http_port
|
|
: 'Port ' + http_port;
|
|
|
|
const sbind = typeof https_port === 'string'
|
|
? 'Pipe ' + https_port
|
|
: 'Port ' + https_port;
|
|
|
|
// handle specific listen errors with friendly messages
|
|
switch (error.code) {
|
|
case 'EACCES':
|
|
console.error(bind + ' and ' + sbind + ' require elevated privileges');
|
|
process.exit(1);
|
|
break;
|
|
case 'EADDRINUSE':
|
|
console.error(bind + ' and/or ' + sbind + ' already in use');
|
|
process.exit(1);
|
|
break;
|
|
default:
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Event listener for HTTPS server "listening" event.
|
|
*/
|
|
|
|
function onListening() {
|
|
const addr = server.address();
|
|
const bind = typeof addr === 'string'
|
|
? 'pipe ' + addr
|
|
: 'port ' + addr.port;
|
|
debug('Listening on ' + bind);
|
|
}
|