Skip to content
This repository was archived by the owner on Mar 10, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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});
Expand Down
1 change: 1 addition & 0 deletions src/config/config dist.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"SSL": false,
"KEY_FILE_SSL": null,
"CERT_FILE_SSL" : null,
"REDIRECT_PORT": null,
"METRICS": false,
"METRICS_PORT": 8000
}
15 changes: 10 additions & 5 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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);
}