2017-12-13 22:05:07 +08:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
2017-12-29 15:58:18 +08:00
|
|
|
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')
|
|
|
|
}
|
2017-12-13 22:05:07 +08:00
|
|
|
|
|
|
|
/**
|
2017-12-29 21:39:16 +08:00
|
|
|
* Get ports from environment and store in Express.
|
2017-12-13 22:05:07 +08:00
|
|
|
*/
|
|
|
|
|
2017-12-29 21:39:16 +08:00
|
|
|
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);
|
2017-12-13 22:05:07 +08:00
|
|
|
|
|
|
|
/**
|
2017-12-29 21:39:16 +08:00
|
|
|
* Get interface address on which to listen for HTTPS requests from env.
|
2017-12-13 22:05:07 +08:00
|
|
|
*/
|
2017-12-29 21:39:16 +08:00
|
|
|
const https_host = process.env.HTTPS_HOST || null;
|
|
|
|
app.set('https_host', https_host);
|
2017-12-13 22:05:07 +08:00
|
|
|
|
2017-12-29 21:39:16 +08:00
|
|
|
/**
|
|
|
|
* 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');
|
2017-12-29 15:58:18 +08:00
|
|
|
const server = https.createServer(options, app);
|
2017-12-13 22:05:07 +08:00
|
|
|
|
2017-12-29 21:39:16 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-12-13 22:05:07 +08:00
|
|
|
server.on('error', onError);
|
|
|
|
server.on('listening', onListening);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalize a port into a number, string, or false.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function normalizePort(val) {
|
2017-12-29 21:39:16 +08:00
|
|
|
const port = parseInt(val, 10);
|
2017-12-13 22:05:07 +08:00
|
|
|
|
|
|
|
if (isNaN(port)) {
|
|
|
|
// named pipe
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (port >= 0) {
|
|
|
|
// port number
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-29 21:39:16 +08:00
|
|
|
* Event listener for HTTP/S server "error" event.
|
2017-12-13 22:05:07 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
function onError(error) {
|
|
|
|
if (error.syscall !== 'listen') {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2017-12-29 21:39:16 +08:00
|
|
|
const bind = typeof http_port === 'string'
|
|
|
|
? 'Pipe ' + http_port
|
|
|
|
: 'Port ' + http_port;
|
2017-12-13 22:05:07 +08:00
|
|
|
|
2017-12-29 21:39:16 +08:00
|
|
|
const sbind = typeof https_port === 'string'
|
|
|
|
? 'Pipe ' + https_port
|
|
|
|
: 'Port ' + https_port;
|
2017-12-29 15:58:18 +08:00
|
|
|
|
2017-12-13 22:05:07 +08:00
|
|
|
// handle specific listen errors with friendly messages
|
|
|
|
switch (error.code) {
|
|
|
|
case 'EACCES':
|
2017-12-29 15:58:18 +08:00
|
|
|
console.error(bind + ' and ' + sbind + ' require elevated privileges');
|
2017-12-13 22:05:07 +08:00
|
|
|
process.exit(1);
|
|
|
|
break;
|
|
|
|
case 'EADDRINUSE':
|
2017-12-29 15:58:18 +08:00
|
|
|
console.error(bind + ' and/or ' + sbind + ' already in use');
|
2017-12-13 22:05:07 +08:00
|
|
|
process.exit(1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-29 15:58:18 +08:00
|
|
|
* Event listener for HTTPS server "listening" event.
|
2017-12-13 22:05:07 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
function onListening() {
|
2017-12-29 21:39:16 +08:00
|
|
|
const addr = server.address();
|
|
|
|
const bind = typeof addr === 'string'
|
2017-12-13 22:05:07 +08:00
|
|
|
? 'pipe ' + addr
|
|
|
|
: 'port ' + addr.port;
|
|
|
|
debug('Listening on ' + bind);
|
|
|
|
}
|