From e8df55ed91bd8edc2952ff21751a02fa89beebe4 Mon Sep 17 00:00:00 2001 From: Jason Park Date: Tue, 4 Dec 2018 20:26:38 -0500 Subject: [PATCH] Support HTTPS --- README.md | 2 +- app/index.js | 15 +++++++++++---- bin/www | 15 ++++++++++++--- environment.js | 26 +++++++++++++++++++++++--- src/backend/controllers/algorithms.js | 2 +- 5 files changed, 48 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 231bd88..7b413f5 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Learning algorithms from text and static images is quite boring. For that, there have been many great websites that view animations of various algorithms. However, for us being coders, nothing can be more comprehensible than visualizing the actual working code. So here we introduce Algorithm Visualizer. -[![Screenshot](https://raw.githubusercontent.com/algorithm-visualizer/algorithm-visualizer/master/branding/screenshot.png)](http://algorithm-visualizer.org/) +[![Screenshot](https://raw.githubusercontent.com/algorithm-visualizer/algorithm-visualizer/master/branding/screenshot.png)](https://algorithm-visualizer.org/) ## Contributing diff --git a/app/index.js b/app/index.js index 84f8944..04064aa 100644 --- a/app/index.js +++ b/app/index.js @@ -3,15 +3,22 @@ const history = require('connect-history-api-fallback'); const express = require('express'); const app = express(); +const frontend = require('./frontend'); +const backend = require('./backend'); + const { apiEndpoint, + credentials, } = require('../environment'); -const frontend = require('./frontend'); -const backend = require('./backend'); app.use((req, res, next) => { - if (req.hostname === 'algo-visualizer.jasonpark.me') return res.redirect(301, 'http://algorithm-visualizer.org/'); - next(); + if (req.hostname === 'algo-visualizer.jasonpark.me') { + res.redirect(301, 'https://algorithm-visualizer.org/'); + } else if (credentials && !req.secure) { + res.redirect(301, `https://${req.hostname}${req.url}`); + } else { + next(); + } }); app.use(apiEndpoint, backend); app.use(history()); diff --git a/bin/www b/bin/www index b47cbf2..19bd763 100755 --- a/bin/www +++ b/bin/www @@ -1,12 +1,21 @@ #!/usr/bin/env node const http = require('http'); +const https = require('https'); const app = require('../app'); const { - port, + httpPort, + httpsPort, + credentials, } = require('../environment'); const httpServer = http.createServer(app); -httpServer.listen(port); -console.info(`http: listening on port ${port}`); +httpServer.listen(httpPort); +console.info(`http: listening on port ${httpPort}`); + +if (credentials) { + const httpsServer = https.createServer(credentials, app); + httpsServer.listen(httpsPort); + console.info(`https: listening on port ${httpsPort}`); +} diff --git a/environment.js b/environment.js index 4cb9eb6..a5c7ac0 100644 --- a/environment.js +++ b/environment.js @@ -1,26 +1,44 @@ const path = require('path'); +const fs = require('fs'); const { NODE_ENV = 'production', - PORT = '8080', + HTTP_PORT = '8080', + HTTPS_PORT = '8443', PROXY_PORT = '3000', GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET, GITHUB_WEBHOOK_SECRET, + + CREDENTIALS_ENABLED = '0', + CREDENTIALS_PATH, + CREDENTIALS_CA, + CREDENTIALS_KEY, + CREDENTIALS_CERT, } = process.env; +const isEnabled = v => v === '1'; + const __PROD__ = NODE_ENV === 'production'; const __DEV__ = !__PROD__; -const port = parseInt(PORT); +const httpPort = parseInt(HTTP_PORT); +const httpsPort = parseInt(HTTPS_PORT); const proxyPort = parseInt(PROXY_PORT); const githubClientId = GITHUB_CLIENT_ID; const githubClientSecret = GITHUB_CLIENT_SECRET; const githubWebhookSecret = GITHUB_WEBHOOK_SECRET; +const read = (file) => fs.readFileSync(path.resolve(CREDENTIALS_PATH, file)); +const credentials = isEnabled(CREDENTIALS_ENABLED) && { + ca: read(CREDENTIALS_CA), + key: read(CREDENTIALS_KEY), + cert: read(CREDENTIALS_CERT), +}; + const buildPath = path.resolve(__dirname, 'build'); const frontendBuildPath = path.resolve(buildPath, 'frontend'); const backendBuildPath = path.resolve(buildPath, 'backend'); @@ -34,11 +52,13 @@ const apiEndpoint = '/api'; module.exports = { __PROD__, __DEV__, - port, + httpPort, + httpsPort, proxyPort, githubClientId, githubClientSecret, githubWebhookSecret, + credentials, frontendBuildPath, backendBuildPath, frontendSrcPath, diff --git a/src/backend/controllers/algorithms.js b/src/backend/controllers/algorithms.js index d1d096e..aa87841 100644 --- a/src/backend/controllers/algorithms.js +++ b/src/backend/controllers/algorithms.js @@ -132,7 +132,7 @@ router.route('/sitemap.txt') .get((req, res, next) => { const urls = []; categories.forEach(category => category.algorithms.forEach(algorithm => { - urls.push(`http://algorithm-visualizer.org/${category.key}/${algorithm.key}`); + urls.push(`https://algorithm-visualizer.org/${category.key}/${algorithm.key}`); })); res.set('Content-Type', 'text/plain'); res.send(urls.join('\n')); -- GitLab