diff options
author | Noah Loomans <noahloomans@gmail.com> | 2017-03-08 11:14:11 +0100 |
---|---|---|
committer | Noah Loomans <noahloomans@gmail.com> | 2017-03-08 11:14:11 +0100 |
commit | ab199d91c16946acea07b35ad42366766cb55696 (patch) | |
tree | 590a971a1e8fc4025e1b81454569276aa30bcc5b /bin | |
parent | 3d87f000fe2d85e9c7ad0958189d242838d63266 (diff) | |
parent | d4ea3ff68f5b476aab009672968d3d348782a367 (diff) |
Merge branch 'useHTTPS'
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/www | 25 |
1 files changed, 15 insertions, 10 deletions
@@ -3,11 +3,16 @@ const fs = require('fs') const app = require('../app') const http = require('http') -const https = require('https') +const https = require('spdy') + +const fileLocations = { + cert: '/etc/letsencrypt/live/rooster.hetmml.nl/fullchain.pem', + privkey: '/etc/letsencrypt/live/rooster.hetmml.nl/privkey.pem' +} function setupHTTPS () { - const certificate = fs.readFileSync('/etc/letsencrypt/live/rooster.hetmml.nl/fullchain.pem', 'utf8') - const privateKey = fs.readFileSync('/etc/letsencrypt/live/rooster.hetmml.nl/privkey.pem', 'utf8') + const certificate = fs.readFileSync(fileLocations.cert, 'utf8') + const privateKey = fs.readFileSync(fileLocations.privkey, 'utf8') const credentials = { key: privateKey, cert: certificate } const httpsPort = normalizePort(process.env.PORT_HTTPS || '3001') @@ -20,6 +25,11 @@ function setupHTTPS () { app.set('port', httpsPort) } +function redirectToHTTPS (req, res) { + res.writeHead(301, { 'Location': 'https://' + req.headers['host'] + req.url }) + res.end() +} + function setupHTTPSRedirect () { const httpPort = normalizePort(process.env.PORT || '3000') const httpServer = http.createServer(redirectToHTTPS) @@ -88,7 +98,7 @@ function onListening (server) { let useHTTPS = true try { - fs.accessSync('/etc/letsencrypt/live/rooster.hetmml.nl/privkey.pem') + fs.accessSync(fileLocations.privkey) } catch (e) { useHTTPS = false } @@ -102,11 +112,6 @@ if (useHTTPS) { setupHTTP() } } else { - console.warn('NOT USING HTTPS! Could not read /etc/letsencrypt/live/rooster.hetmml.nl/privkey.pem') + console.warn(`NOT USING HTTPS! Could not read ${fileLocations.privkey}`) setupHTTP() } - -function redirectToHTTPS (req, res) { - res.writeHead(302, { 'Location': 'https://' + req.headers['host'] + req.url }) - res.end() -} |