Skip to content

Commit

Permalink
Update node server to work behind reverse proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
ddaspit committed Apr 6, 2018
1 parent 03e154b commit bd10047
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 34 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ src/cache/
src/dist/
src/js/lib/languageforge.min.js
src/js/lib/scriptureforge.min.js
src/node/config.js
src/node_modules/
!src/vendor/lift/
src/vendor/
Expand Down
2 changes: 1 addition & 1 deletion scripts/server/init/scriptureforge-sharedb.service
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ RestartSec=10
SyslogIdentifier=scriptureforge-sharedb
User=root
Group=www-data
Environment=NODE_ENV=production PORT=8443
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target
2 changes: 1 addition & 1 deletion scripts/server/init/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ RestartSec=10
SyslogIdentifier=scriptureforge-sharedb_%i
User=root
Group=www-data
Environment=NODE_ENV=production PORT=8443
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target
12 changes: 10 additions & 2 deletions src/angular-app/bellows/apps/translate/core/realtime.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,16 @@ export class RealTimeService {
}

private getWebSocketDocUrl() {
const url = 'wss://' + this.$window.location.host;
return (url.endsWith(':8443')) ? url : url + ':8443';
let protocol: string;
switch (this.$window.location.protocol) {
case 'http:':
protocol = 'ws';
break;
case 'https:':
protocol = 'wss';
break;
}
return protocol + '://' + this.$window.location.host + '/sharedb/';
}

}
14 changes: 0 additions & 14 deletions src/node/config.js

This file was deleted.

20 changes: 6 additions & 14 deletions src/node/server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var cookie = require('cookie');
var express = require('express');
var fs = require('fs');
var https = require('https');
var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var os = require('os');
var path = require('path');
Expand All @@ -27,13 +27,8 @@ share.use('connect', function (request, done) {
share.use('apply', useUserData);
share.connect();

var config = (fs.existsSync('./config.js')) ? require('./config') : {};
var defaultSslKey = '/etc/letsencrypt/live/scriptureforge.org/privkey.pem';
var defaultSslCert = '/etc/letsencrypt/live/scriptureforge.org/cert.pem';
var sslKeyPath = config.sslKeyPath || defaultSslKey;
var sslCertPath = config.sslCertPath || defaultSslCert;
var hostname = '0.0.0.0';
var webSocketDocServerPort = 8443;
var webSocketDocServerPort = 5002;

// Client connection time allowed without editing in minutes
var connectionTime = 20;
Expand All @@ -53,16 +48,13 @@ MongoClient.connect('mongodb://localhost:27017/realtime', function (err, db) {

function startServer() {
// Create web servers to serve files and listen to WebSocket connections
var privateKey = fs.readFileSync(sslKeyPath, 'utf8');
var certificate = fs.readFileSync(sslCertPath, 'utf8');
var options = { key: privateKey, cert: certificate };
var app = express();
app.use(express.static('static'));
var docServer = https.createServer(options, app);
var docServer = http.createServer(app);

// Connect any incoming WebSocket connection to ShareDB
var docWss = new WebSocket.Server({ server: docServer });
docWss.on('connection', function (ws) {
var docWs = new WebSocket.Server({ server: docServer });
docWs.on('connection', function (ws) {
getSession(ws, function (err, session) {
if (err) return console.log(err);

Expand All @@ -88,7 +80,7 @@ function startServer() {
});

docServer.listen(webSocketDocServerPort, hostname, function () {
console.log('Doc Server is listening at https://' + hostname + ':' + webSocketDocServerPort);
console.log('Doc Server is listening at ws://' + hostname + ':' + webSocketDocServerPort);
});
}

Expand Down
1 change: 0 additions & 1 deletion upload-exclude.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
/assets/**
/js/lib/languageforge.js
/js/lib/scriptureforge.js
/node/config.js

0 comments on commit bd10047

Please sign in to comment.