Trying clustering

This commit is contained in:
Kasper Rynning-Tønnesen
2018-02-05 12:56:10 +01:00
parent c5ef3404b7
commit c9446e151c
4 changed files with 178 additions and 46 deletions

47
package-lock.json generated
View File

@@ -442,6 +442,11 @@
"version": "https://registry.npmjs.org/dom-walk/-/dom-walk-0.1.1.tgz",
"integrity": "sha1-ZyIm3HTI95mtNTB9+TaroRrNYBg="
},
"double-ended-queue": {
"version": "2.1.0-0",
"resolved": "https://registry.npmjs.org/double-ended-queue/-/double-ended-queue-2.1.0-0.tgz",
"integrity": "sha1-ED01J/0xUo9AGIEwyEHv3XgmTlw="
},
"duplexer2": {
"version": "https://registry.npmjs.org/duplexer2/-/duplexer2-0.0.2.tgz",
"integrity": "sha1-xhTc9n4vsUmVqRcR5aYX6KYKMds=",
@@ -1760,6 +1765,11 @@
"remove-trailing-separator": "https://registry.npmjs.org/remove-trailing-separator/-/remove-trailing-separator-1.1.0.tgz"
}
},
"notepack.io": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/notepack.io/-/notepack.io-2.1.2.tgz",
"integrity": "sha1-lKR/XkzkrOHz1xsnxvg8cWM5PFI="
},
"oauth-sign": {
"version": "https://registry.npmjs.org/oauth-sign/-/oauth-sign-0.8.2.tgz",
"integrity": "sha1-Rqarfwrq2N6unsBWV4C31O/rnUM="
@@ -2088,6 +2098,26 @@
"resolve": "https://registry.npmjs.org/resolve/-/resolve-1.4.0.tgz"
}
},
"redis": {
"version": "2.8.0",
"resolved": "https://registry.npmjs.org/redis/-/redis-2.8.0.tgz",
"integrity": "sha512-M1OkonEQwtRmZv4tEWF2VgpG0JWJ8Fv1PhlgT5+B+uNq2cA3Rt1Yt/ryoR+vQNOQcIEgdCdfH0jr3bDpihAw1A==",
"requires": {
"double-ended-queue": "2.1.0-0",
"redis-commands": "1.3.1",
"redis-parser": "2.6.0"
}
},
"redis-commands": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.3.1.tgz",
"integrity": "sha1-gdgm9F+pyLIBH0zXoP5ZfSQdRCs="
},
"redis-parser": {
"version": "2.6.0",
"resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-2.6.0.tgz",
"integrity": "sha1-Uu0J2srBCPGmMcB+m2mUHnoZUEs="
},
"regex-cache": {
"version": "https://registry.npmjs.org/regex-cache/-/regex-cache-0.4.4.tgz",
"integrity": "sha1-db3FiioUls7EihKDW8VMjVYjNt0=",
@@ -2300,6 +2330,18 @@
}
}
},
"socket.io-redis": {
"version": "5.2.0",
"resolved": "https://registry.npmjs.org/socket.io-redis/-/socket.io-redis-5.2.0.tgz",
"integrity": "sha1-j+KtlEX8UIhvtwq8dZ1nQD1Ymd8=",
"requires": {
"debug": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
"notepack.io": "2.1.2",
"redis": "2.8.0",
"socket.io-adapter": "https://registry.npmjs.org/socket.io-adapter/-/socket.io-adapter-1.1.1.tgz",
"uid2": "0.0.3"
}
},
"source-map": {
"version": "https://registry.npmjs.org/source-map/-/source-map-0.5.7.tgz",
"integrity": "sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w="
@@ -2478,6 +2520,11 @@
"integrity": "sha1-bgkk1r2mta/jSeOabWMoUKD4grc=",
"optional": true
},
"uid2": {
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/uid2/-/uid2-0.0.3.tgz",
"integrity": "sha1-SDEm4Rd03y9xuLY53NeZw3YWK4I="
},
"ultron": {
"version": "https://registry.npmjs.org/ultron/-/ultron-1.1.0.tgz",
"integrity": "sha1-sHoualQagV/Go0zNRTO67DB8qGQ="

View File

@@ -48,6 +48,7 @@
"nodemailer": "^4.0.1",
"request": "^2.72.0",
"socket.io": "^2.0.3",
"socket.io-redis": "^5.2.0",
"uniqid": "^4.1.1"
}
}

122
server/app.js Normal file
View File

@@ -0,0 +1,122 @@
var app = require('./index.js');
var path = require('path');
var publicPath = path.join(__dirname, 'public');
var debug = require('debug')('king:server');
var http = require('http');
//var config = require('../config/index');
/**
* Get port from environment and store in Express.
*/
//var env = app.get('env');
var port = 8080;
app.set('port', port);
var cluster = require('cluster');
if (cluster.isMaster) {
var numWorkers = require('os').cpus().length;
for (var i = 0; i < numWorkers; i++) cluster.fork();
console.log('[CLUSTER] Master cluster setting up ' + numWorkers + ' workers...');
cluster.on('online', function(worker) {
console.log('[CLUSTER] Worker ' + worker.process.pid + ' is online');
});
cluster.on('exit', function(worker, code, signal){
console.log('[CLUSTER] Worker ' + worker.process.pid + ' died with code: ' + code + ', and signal: ' + signal);
console.log('[CLUSTER] Starting a new worker');
cluster.fork();
});
} else {
/**
* Create HTTP server.
*/
try {
var cert_config = require(path.join(path.join(__dirname, 'config'), 'cert_config.js'));
var fs = require('fs');
var privateKey = fs.readFileSync(cert_config.privateKey).toString();
var certificate = fs.readFileSync(cert_config.certificate).toString();
var ca = fs.readFileSync(cert_config.ca).toString();
var credentials = {
key: privateKey,
cert: certificate,
ca: ca
};
var https = require('https');
server = https.Server(credentials, app);
var cors_proxy = require('cors-anywhere');
cors_proxy.createServer({
requireHeader: ['origin', 'x-requested-with'],
removeHeaders: ['cookie', 'cookie2'],
httpsOptions: credentials
}).listen(8081, function() {
console.log('Running CORS Anywhere on :' + 8081);
});
} catch(err){
console.log("Starting without https (probably on localhost)");
var cors_proxy = require('cors-anywhere');
cors_proxy.createServer({
requireHeader: ['origin', 'x-requested-with'],
removeHeaders: ['cookie', 'cookie2'],
}).listen(8081, function() {
console.log('Running CORS Anywhere on :' + 8081);
});
var http = require('http');
server = http.Server(app);
add = ",http://localhost:80*,http://localhost:8080*,localhost:8080*, localhost:8082*,http://zoff.dev:80*,http://zoff.dev:8080*,zoff.dev:8080*, zoff.dev:8082*";
}
var port = 8080;
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Socket.io
*/
var io = app.socketIO;
io.listen(server);
//socketIO.set('transports', ['websocket']);
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port;
debug('Listening on ' + bind);
}

View File

@@ -1,6 +1,5 @@
VERSION = require('./VERSION.js');
var server;
var add = "";
var path = require('path');
var publicPath = path.join(__dirname, 'public');
@@ -22,45 +21,6 @@ app.set('view engine', 'handlebars');
app.enable('view cache');
app.set('views', publicPath);
try{
var cert_config = require(path.join(path.join(__dirname, 'config'), 'cert_config.js'));
var fs = require('fs');
var privateKey = fs.readFileSync(cert_config.privateKey).toString();
var certificate = fs.readFileSync(cert_config.certificate).toString();
var ca = fs.readFileSync(cert_config.ca).toString();
var credentials = {
key: privateKey,
cert: certificate,
ca: ca
};
var https = require('https');
server = https.Server(credentials, app);
var cors_proxy = require('cors-anywhere');
cors_proxy.createServer({
requireHeader: ['origin', 'x-requested-with'],
removeHeaders: ['cookie', 'cookie2'],
httpsOptions: credentials
}).listen(8081, function() {
console.log('Running CORS Anywhere on :' + 8081);
});
}
catch(err){
console.log("Starting without https (probably on localhost)");
var cors_proxy = require('cors-anywhere');
cors_proxy.createServer({
requireHeader: ['origin', 'x-requested-with'],
removeHeaders: ['cookie', 'cookie2'],
}).listen(8081, function() {
console.log('Running CORS Anywhere on :' + 8081);
});
var http = require('http');
server = http.Server(app);
add = ",http://localhost:80*,http://localhost:8080*,localhost:8080*, localhost:8082*,http://zoff.dev:80*,http://zoff.dev:8080*,zoff.dev:8080*, zoff.dev:8082*";
}
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser')
app.use( bodyParser.json() ); // to support JSON-encoded bodies
@@ -70,13 +30,18 @@ app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
app.use(cookieParser());
/* Starting DB and socketio */
io = require('socket.io')(server, {
io = require('socket.io')({
pingTimeout: 25000,
path: '/zoff',
}); //, "origins": ("https://zoff.me:443*,https://zoff.me:8080*,zoff.me:8080*,https://remote.zoff.me:443*,https://remote.zoff.me:8080*,https://fb.zoff.me:443*,https://fb.zoff.me:8080*,https://admin.zoff.me:443*,https://admin.zoff.me:8080*" + add)});
db = require('./handlers/db.js');
redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));
var socketIO = require('./handlers/io.js');
socketIO();
app.socketIO = io;
request = require('request');
/* Globally needed "libraries" and files */
@@ -99,11 +64,6 @@ filter = new Filter({ placeHolder: 'x'});
var router = require('./routing/router.js');
var api = require('./routing/api.js');
var ico_router = require('./routing/icons_routing.js');
var port = 8080;
server.listen(port, function () {
console.log('Server listening at port %d', port);
});
app.get('/robots.txt', function (req, res) {
res.type('text/plain');
@@ -147,3 +107,5 @@ db.collection("user_names").update({"_id": "all_names"}, {$set: {names: []}}, {m
db.collection("connected_users").update({users: {$exists: true}}, {$set: {users: []}}, {multi: true, upsert: true}, function(err, docs){});
db.collection("connected_users").update({"_id": "total_users"}, {$set: {total_users: []}}, {multi: true, upsert: true}, function(err, docs) {});
db.collection("frontpage_lists").update({viewers: {$ne: 0}}, {$set: {"viewers": 0}}, {multi: true, upsert: true}, function(err, docs) {});
module.exports = app;