diff --git a/README.md b/README.md index 231bd88233dba74aee1e2804467cf20ee03814d3..7b413f5bbc24e37bab8ab45b77060520c36cc899 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 84f89446262b5584712b491ea5b6f5056a7cb467..04064aae1c407d03aa22ef4c3fad0a3041e159f7 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 b47cbf21e4b612ead707f0039b385a4973bc58b9..19bd763900759c75c283d28ddcf8b3d62061c909 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 4cb9eb649efde7cac19295b6b4e695e287879953..a5c7ac0ca9c91c6f77cca5a22db097f9aedf2647 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 d1d096e22cb53c97aebe92c7d47e3b2344af6393..aa8784158dd18d820de4fd200011f2f1917266e4 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'));