diff --git a/src/app.js b/src/app.js index 2cf4f86..9e401e2 100644 --- a/src/app.js +++ b/src/app.js @@ -16,10 +16,22 @@ const index = require('./routes/index'); const editor = require('./routes/editor'); const legal = require('./routes/legal'); const config = require('./config/config'); +const ssl = config.SSL; const app = express(); app.disable("x-powered-by"); +if (ssl) { + app.use('*', function (req, res, next) { + if (req.secure) { + next(); + } else { + const target = (req.headers.host.includes(':') ? req.headers.host.split(':')[0] : req.headers.host) + ':' + config.PORT; + res.redirect('https://' + target + req.url); + } + }); +} + // Configure views folder nunjucks.configure(path.join(__dirname, 'views'), { autoescape: true, @@ -51,13 +63,6 @@ app.use('/', index); app.use('/editor', editor); app.use('/legal', legal); -// redirect to https when ssl is active -if (config.SSL) { - app.get('*', function(req, res) { - res.redirect('https://' + req.headers.host + req.url); - }); -} - // 404 error app.all('*', (req, res) => { res.status(404).render('404.html', {production: config.PRODUCTION, client_versobe: config.CLIENT_VERBOSE}); diff --git a/src/config/config dist.json b/src/config/config dist.json index 6b7e87e..4a547f3 100644 --- a/src/config/config dist.json +++ b/src/config/config dist.json @@ -10,6 +10,7 @@ "SSL": false, "KEY_FILE_SSL": null, "CERT_FILE_SSL" : null, + "REDIRECT_PORT": null, "METRICS": false, "METRICS_PORT": 8000 } diff --git a/src/server.js b/src/server.js index b59d5d6..9042037 100644 --- a/src/server.js +++ b/src/server.js @@ -14,6 +14,7 @@ const host = configs.HOST; const port = configs.PORT; const DEBUG = configs.DEBUG; const ssl = configs.SSL; +const metrics = configs.METRICS; const sslKeyPath = configs.KEY_FILE_SSL; const sslCertPath = configs.CERT_FILE_SSL; @@ -27,9 +28,11 @@ const config = require('./config/config'); const http = ssl ? require('https') : require('http'); const server = http.createServer(options, app); -if (configs.METRICS) { - const {metricsApp} = require('./metricsApp'); - var metricsServer = require('http').createServer(metricsApp); + +if (ssl && configs.REDIRECT_PORT !== null) { + require('http').createServer(app).listen(configs.REDIRECT_PORT, host, () => { + console.log(`http requests from ${configs.REDIRECT_PORT} are redirected to https on ${configs.PORT}`) + }) } // config websockets @@ -39,9 +42,11 @@ require('./socket/socket')(wss); server.listen(port, host, () => { - console.log('Server Started!'); + console.log(`Server Started on ${host}:${port}`); }); -if (config.METRICS) { +if (metrics) { + const {metricsApp} = require('./metricsApp'); + const metricsServer = require('http').createServer(metricsApp); metricsServer.listen(config.METRICS_PORT); }