Browse Source

more work on HTTP => HTTPS redirect and HTTPS integration

develop
Rob Colbert 2 years ago
parent
commit
611645d87a
  1. 3
      .vscode/launch.json
  2. 30
      lib/site-platform.js

3
.vscode/launch.json

@ -14,7 +14,8 @@
"program": "${workspaceFolder:dtp-base}/dtp-webapp.js",
"console": "integratedTerminal",
"env": {
"HTTP_BIND_PORT": "3010"
"HTTP_BIND_PORT": "3010",
"HTTPS_BIND_PORT": "3410",
}
},
{

30
lib/site-platform.js

@ -370,27 +370,29 @@ module.exports.startWebServer = async (dtp) => {
});
if (process.env.HTTP_ENABLE === 'enabled') {
await module.createHttpServer(dtp);
if (process.env.HTTP_REDIRECT_SSL === 'enabled') {
module.log.info('creating HTTP SSL redirect app');
module.redirectApp = express();
module.redirectApp.use((req, res) => {
module.log.info('redirecting to SSL', { host: req.host, url: req.url });
res.redirect(`https://${process.env.DTP_SITE_DOMAIN}${req.url}`);
});
await module.createHttpServer(dtp, module.redirectApp);
} else {
await module.createHttpServer(dtp, module.app);
}
}
if (process.env.HTTPS_ENABLE === 'enabled') {
await module.createHttpsServer(dtp);
await module.createHttpsServer(dtp, module.app);
}
// prefer to attach Socket.io to the HTTPS server and fall back to HTTP
await module.createSocketServer(dtp, module.https || module.http);
if (module.http) {
module.log.info('starting HTTP server', {
port: dtp.config.http.port,
bind: dtp.config.http.address,
});
await module.startHttpServer(dtp, module.http, dtp.config.http);
}
if (module.https) {
module.log.info('starting HTTPS server', {
port: dtp.config.https.port,
bind: dtp.config.https.address,
});
await module.startHttpServer(dtp, module.https, dtp.config.https);
}
@ -400,12 +402,12 @@ module.exports.startWebServer = async (dtp) => {
});
};
module.createHttpServer = async (/* dtp */) => {
module.createHttpServer = async (dtp, app) => {
module.log.info('creating HTTP server');
module.http = require('http').createServer(module.app);
module.http = require('http').createServer(app);
};
module.createHttpsServer = async (dtp) => {
module.createHttpsServer = async (dtp, app) => {
const httpsOptions = {
cert: await fs.promises.readFile(
process.env.HTTPS_SSL_CRT || path.join(dtp.config.root, 'ssl', 'dtp-webapp.crt')
@ -416,7 +418,7 @@ module.createHttpsServer = async (dtp) => {
};
module.log.info('creating HTTPS server');
module.https = require('https').createServer(httpsOptions, module.app);
module.https = require('https').createServer(httpsOptions, app);
return module.https;
};

Loading…
Cancel
Save