-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsslServer.js
93 lines (76 loc) · 2.59 KB
/
sslServer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import express from 'express';
import favicon from 'serve-favicon';
import {MongoClient, ObjectID} from 'mongodb';
import fs from 'fs';
import path from 'path';
import zlib from 'zlib';
import https from 'https';
import * as React from 'react';
import ReactDOMServer from 'react-dom/server';
import {StaticRouter} from 'react-router';
var certificate = fs.readFileSync('/etc/letsencrypt/live/www.csua.berkeley.edu/fullchain.pem');
var privateKey = fs.readFileSync('/etc/letsencrypt/live/www.csua.berkeley.edu/privkey.pem');
var credentials = { key: privateKey, cert: certificate, requestCert: true };
var sslPort = 8443;
var port = 8081;
var legacyPort = 8080;
global.window = {
addEventListener: () => {},
scrollTo: () => {}
};
global.document = {
addEventListener: () => {}
};
var AppComponent = require('./src/App').default;
/* GZIP everything */
function sendBase(req, res, next) {
fs.readFile(__dirname + '/../public/index.html', 'utf8', function (error, docData) {
if (error) throw error;
res.writeHead(200, {'Content-Type': 'text/html', 'Content-Encoding': 'gzip'});
const AppElement = ReactDOMServer.renderToString(
<StaticRouter location={req.url} context={{}}>
<AppComponent/>
</StaticRouter>
);
const document = docData.replace(/<div id="app"><\/div>/,`<div id="app">${AppElement}</div>`);
zlib.gzip(document, function (_, result) {
res.end(result);
});
});
}
const app = express();
const sslServer = https.createServer(credentials, app);
app.all('*', function(req, res, next){
console.log('Ping');
if (req.path.startsWith('/newuser') || req.path.startsWith('/computers')) {
res.redirect('https://' + req.hostname + ':' + legacyPort + req.path);
return;
}
if (req.secure) {
return next();
}
});
app.use(favicon(path.join(__dirname, '/../public/static/images/logos/favicon.ico')));
app.get('/bundle.js', function (req, res, next) {
req.url = req.url + '.gz';
res.set('Content-Encoding', 'gzip');
res.set('Content-Type', 'application/javascript');
next();
});
app.get('/bundle.css', function (req, res, next) {
req.url = req.url + '.gz';
res.set('Content-Encoding', 'gzip');
res.set('Content-Type', 'text/css');
next();
});
app.get('/', sendBase);
app.use(express.static('public'));
app.get('*', sendBase);
sslServer.listen(sslPort,
() => console.log('Node/express SSL server started on port ' + sslPort)
);
var server = express();
server.get('*', function(req, res) {
res.redirect('https://' + req.hostname + ':' + sslPort);
});
server.listen(port);