minor refactoring

This commit is contained in:
AJ ONeal 2016-10-11 10:58:18 -06:00
parent d192cd8255
commit 344a3eba41
1 changed files with 27 additions and 10 deletions

View File

@ -26,9 +26,13 @@ function showError(err, port) {
} }
function createInsecureServer(port, pubdir, opts) { function createInsecureServer(port, pubdir, opts) {
return new PromiseA(function (resolve) { return new PromiseA(function (realResolve) {
var server = http.createServer(); var server = http.createServer();
function resolve() {
realResolve(server);
}
server.on('error', function (err) { server.on('error', function (err) {
if (opts.errorInsecurePort || opts.manualInsecurePort) { if (opts.errorInsecurePort || opts.manualInsecurePort) {
showError(err, port); showError(err, port);
@ -41,9 +45,7 @@ function createInsecureServer(port, pubdir, opts) {
return createInsecureServer(insecurePortFallback, pubdir, opts).then(resolve); return createInsecureServer(insecurePortFallback, pubdir, opts).then(resolve);
}); });
server.on('request', require('redirect-https')({ server.on('request', opts.redirectApp);
port: opts.port
}));
server.listen(port, function () { server.listen(port, function () {
opts.insecurePort = port; opts.insecurePort = port;
@ -53,15 +55,20 @@ function createInsecureServer(port, pubdir, opts) {
} }
function createServer(port, pubdir, content, opts) { function createServer(port, pubdir, content, opts) {
return new PromiseA(function (resolve) { return new PromiseA(function (realResolve) {
var server = https.createServer(opts.httpsOptions); var server = https.createServer(opts.httpsOptions);
var app = require('./app'); var app = require('./app');
var directive = { public: pubdir, content: content, livereload: opts.livereload var directive = { public: pubdir, content: content, livereload: opts.livereload
, servername: opts.servername, expressApp: opts.expressApp }; , servername: opts.servername, expressApp: opts.expressApp };
var redirectApp = require('redirect-https')({ var insecureServer;
port: port
}); function resolve() {
realResolve({
plainServer: insecureServer
, server: server
});
}
server.on('error', function (err) { server.on('error', function (err) {
if (opts.errorPort || opts.manualPort) { if (opts.errorPort || opts.manualPort) {
@ -77,6 +84,7 @@ function createServer(port, pubdir, content, opts) {
server.listen(port, function () { server.listen(port, function () {
opts.port = port; opts.port = port;
opts.redirectOptions.port = port;
if (opts.livereload) { if (opts.livereload) {
opts.lrPort = opts.lrPort || lrPort; opts.lrPort = opts.lrPort || lrPort;
@ -93,7 +101,10 @@ function createServer(port, pubdir, content, opts) {
} }
if ('false' !== opts.insecurePort && httpPort !== opts.insecurePort) { if ('false' !== opts.insecurePort && httpPort !== opts.insecurePort) {
return createInsecureServer(opts.insecurePort, pubdir, opts).then(resolve); return createInsecureServer(opts.insecurePort, pubdir, opts).then(function (_server) {
insecureServer = _server;
resolve();
});
} else { } else {
opts.insecurePort = opts.port; opts.insecurePort = opts.port;
resolve(); resolve();
@ -108,7 +119,7 @@ function createServer(port, pubdir, content, opts) {
server.on('request', function (req, res) { server.on('request', function (req, res) {
if (!req.socket.encrypted) { if (!req.socket.encrypted) {
redirectApp(req, res); opts.redirectApp(req, res);
return; return;
} }
@ -246,6 +257,12 @@ function run() {
opts.expressApp = require(path.resolve(process.cwd(), argv['express-app'])); opts.expressApp = require(path.resolve(process.cwd(), argv['express-app']));
} }
// can be changed to tunnel external port
opts.redirectOptions = {
port: opts.port
};
opts.redirectApp = require('redirect-https')(opts.redirectOptions);
return createServer(port, pubdir, content, opts).then(function () { return createServer(port, pubdir, content, opts).then(function () {
var msg; var msg;
var p; var p;