diff options
author | Tor Andersson <tor@ccxvii.net> | 2021-07-01 13:07:13 +0200 |
---|---|---|
committer | Tor Andersson <tor@ccxvii.net> | 2021-07-01 13:09:28 +0200 |
commit | 646c215236e540cee5e9ec44561a74c518515572 (patch) | |
tree | 9170aa23f57ec4e0bc8f02939ba731fd71eb2079 /server.js | |
parent | be5c5e87a8bac59000b503c227e181ead6760540 (diff) | |
download | server-646c215236e540cee5e9ec44561a74c518515572.tar.gz |
Only start HTTPS server if the port is set.
If it's not set, use HTTP.
If it's set, use HTTP server to redirect to HTTPS.
Diffstat (limited to 'server.js')
-rw-r--r-- | server.js | 31 |
1 files changed, 19 insertions, 12 deletions
@@ -20,22 +20,29 @@ const MAX_OPEN_GAMES = 3; let session_store = new SQLiteStore(); let db = new sqlite3(process.env.DATABASE || "./db"); + let app = express(); -let https_port = process.env.HTTPS_PORT || 8443; -let https = require('https').createServer({ - key: fs.readFileSync(process.env.SSL_KEY || "key.pem"), - cert: fs.readFileSync(process.env.SSL_CERT || "cert.pem") +let server = null; +let http_port = process.env.HTTP_PORT || 8080; +let https_port = process.env.HTTPS_PORT; +if (https_port) { + server = require('https').createServer({ + key: fs.readFileSync(process.env.SSL_KEY || "key.pem"), + cert: fs.readFileSync(process.env.SSL_CERT || "cert.pem") }, app); -https.listen(https_port, '0.0.0.0', () => { console.log('listening HTTPS on *:' + https_port); }); + server.listen(https_port, '0.0.0.0', () => { console.log('listening HTTPS on *:' + https_port); }); -let io = require('socket.io')(https); + // Redirect HTTP to HTTPS if we're running HTTPS + let http_app = express(); + let http_server = require('http').createServer(http_app); + http_app.use((req, res) => res.redirect(301, 'https://' + req.hostname + req.originalUrl)); + http_server.listen(http_port, '0.0.0.0', () => { console.log('listening HTTP on *:' + http_port); }); +} else { + server = require('http').createServer(app); + server.listen(http_port, '0.0.0.0', () => { console.log('listening HTTP on *:' + http_port); }); +} -// REDIRECT HTTP TO HTTPS -let http_port = process.env.HTTP_PORT || 8080; -let http_app = express(); -let http_server = require('http').createServer(http_app); -http_app.use((req, res) => res.redirect(301, 'https://' + req.hostname + ":" + https_port + req.path)); -http_server.listen(http_port, '0.0.0.0', () => { console.log('listening HTTP on *:' + http_port); }); +let io = require('socket.io')(server); let mailer = null; if (process.env.MAIL_HOST && process.env.MAIL_PORT) { |