mirror of
https://github.com/KevinMidboe/zoff.git
synced 2025-10-29 18:00:23 +00:00
Added a way to check if redis exists, and open for non-redis-users
This commit is contained in:
@@ -46,6 +46,7 @@
|
|||||||
"mongojs": "^2.4.0",
|
"mongojs": "^2.4.0",
|
||||||
"node-cryptojs-aes": "^0.4.0",
|
"node-cryptojs-aes": "^0.4.0",
|
||||||
"nodemailer": "^4.0.1",
|
"nodemailer": "^4.0.1",
|
||||||
|
"redis": "^2.8.0",
|
||||||
"request": "^2.72.0",
|
"request": "^2.72.0",
|
||||||
"socket.io": "^2.0.3",
|
"socket.io": "^2.0.3",
|
||||||
"socket.io-redis": "^5.2.0",
|
"socket.io-redis": "^5.2.0",
|
||||||
|
|||||||
196
server/app.js
196
server/app.js
@@ -2,126 +2,124 @@ var cluster = require('cluster'),
|
|||||||
net = require('net'),
|
net = require('net'),
|
||||||
path = require('path'),
|
path = require('path'),
|
||||||
publicPath = path.join(__dirname, 'public'),
|
publicPath = path.join(__dirname, 'public'),
|
||||||
http = require('http');
|
http = require('http'),
|
||||||
|
port = 8080,
|
||||||
var port = 8080,
|
|
||||||
num_processes = require('os').cpus().length;
|
num_processes = require('os').cpus().length;
|
||||||
|
|
||||||
if (cluster.isMaster) {
|
try {
|
||||||
// This stores our workers. We need to keep them to be able to reference
|
var redis = require("redis");
|
||||||
// them based on source IP address. It's also useful for auto-restart,
|
var client = redis.createClient({host: "localhost", port: 6379});
|
||||||
// for example.
|
client.quit();
|
||||||
var workers = [];
|
startClustered();
|
||||||
|
} catch(e) {
|
||||||
|
console.log("Couldn't connect to redis-server, assuming non-clustered run");
|
||||||
|
startSingle(false);
|
||||||
|
}
|
||||||
|
|
||||||
// Helper function for spawning worker at index 'i'.
|
function startClustered() {
|
||||||
var spawn = function(i) {
|
//Found https://stackoverflow.com/questions/40885592/use-node-js-cluster-with-socket-io-chat-application
|
||||||
workers[i] = cluster.fork();
|
if (cluster.isMaster) {
|
||||||
|
var workers = [];
|
||||||
|
var spawn = function(i) {
|
||||||
|
workers[i] = cluster.fork();
|
||||||
|
workers[i].on('exit', function(code, signal) {
|
||||||
|
console.log('respawning worker', i);
|
||||||
|
spawn(i);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
// Optional: Restart worker on exit
|
for (var i = 0; i < num_processes; i++) {
|
||||||
workers[i].on('exit', function(code, signal) {
|
|
||||||
console.log('respawning worker', i);
|
|
||||||
spawn(i);
|
spawn(i);
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// Spawn workers.
|
|
||||||
for (var i = 0; i < num_processes; i++) {
|
|
||||||
spawn(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper function for getting a worker index based on IP address.
|
|
||||||
// This is a hot path so it should be really fast. The way it works
|
|
||||||
// is by converting the IP address to a number by removing non numeric
|
|
||||||
// characters, then compressing it to the number of slots we have.
|
|
||||||
//
|
|
||||||
// Compared against "real" hashing (from the sticky-session code) and
|
|
||||||
// "real" IP number conversion, this function is on par in terms of
|
|
||||||
// worker index distribution only much faster.
|
|
||||||
var worker_index = function(ip, len) {
|
|
||||||
var s = '';
|
|
||||||
for (var i = 0, _len = ip.length; i < _len; i++) {
|
|
||||||
if (!isNaN(ip[i])) {
|
|
||||||
s += ip[i];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Number(s) % len;
|
var worker_index = function(ip, len) {
|
||||||
};
|
var s = '';
|
||||||
|
for (var i = 0, _len = ip.length; i < _len; i++) {
|
||||||
|
if (!isNaN(ip[i])) {
|
||||||
|
s += ip[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Number(s) % len;
|
||||||
|
};
|
||||||
|
|
||||||
// Create the outside facing server listening on our port.
|
var server = net.createServer({ pauseOnConnect: true }, function(connection) {
|
||||||
var server = net.createServer({ pauseOnConnect: true }, function(connection) {
|
var worker = workers[worker_index(connection.remoteAddress, num_processes)];
|
||||||
// We received a connection and need to pass it to the appropriate
|
worker.send('sticky-session:connection', connection);
|
||||||
// worker. Get the worker for this connection's source IP and pass
|
}).listen(port);
|
||||||
// it the connection.
|
} else {
|
||||||
var worker = workers[worker_index(connection.remoteAddress, num_processes)];
|
startSingle(true);
|
||||||
worker.send('sticky-session:connection', connection);
|
}
|
||||||
}).listen(port);
|
}
|
||||||
} else {
|
|
||||||
// Note we don't use a port here because the master listens on it for us.
|
function startSingle(clustered) {
|
||||||
var app = require('./index.js');
|
var app = require('./index.js');
|
||||||
|
var cors_options = {};
|
||||||
// Here you might use middleware, attach routes, etc
|
|
||||||
|
|
||||||
// Don't expose our internal server to the outside.
|
|
||||||
try {
|
try {
|
||||||
var cert_config = require(path.join(path.join(__dirname, 'config'), 'cert_config.js'));
|
var cert_config = require(path.join(path.join(__dirname, 'config'), 'cert_config.js'));
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
var privateKey = fs.readFileSync(cert_config.privateKey).toString();
|
var privateKey = fs.readFileSync(cert_config.privateKey).toString();
|
||||||
var certificate = fs.readFileSync(cert_config.certificate).toString();
|
var certificate = fs.readFileSync(cert_config.certificate).toString();
|
||||||
var ca = fs.readFileSync(cert_config.ca).toString();
|
var ca = fs.readFileSync(cert_config.ca).toString();
|
||||||
var credentials = {
|
var credentials = {
|
||||||
key: privateKey,
|
key: privateKey,
|
||||||
cert: certificate,
|
cert: certificate,
|
||||||
ca: ca
|
ca: ca
|
||||||
};
|
};
|
||||||
|
|
||||||
var https = require('https');
|
var https = require('https');
|
||||||
server = https.Server(credentials, app);
|
server = https.Server(credentials, app);
|
||||||
|
|
||||||
var cors_proxy = require('cors-anywhere');
|
cors_options = {
|
||||||
|
requireHeader: ['origin', 'x-requested-with'],
|
||||||
|
removeHeaders: ['cookie', 'cookie2'],
|
||||||
|
httpsOptions: credentials
|
||||||
|
};
|
||||||
|
|
||||||
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){
|
} catch(err){
|
||||||
console.log("Starting without https (probably on localhost)");
|
console.log("Starting without https (probably on localhost)");
|
||||||
var cors_proxy = require('cors-anywhere');
|
cors_options = {
|
||||||
cors_proxy.createServer({
|
requireHeader: ['origin', 'x-requested-with'],
|
||||||
requireHeader: ['origin', 'x-requested-with'],
|
removeHeaders: ['cookie', 'cookie2'],
|
||||||
removeHeaders: ['cookie', 'cookie2'],
|
};
|
||||||
}).listen(8081, function() {
|
var cors_proxy = require('cors-anywhere');
|
||||||
console.log('Running CORS Anywhere on :' + 8081);
|
var http = require('http');
|
||||||
});
|
server = http.Server(app);
|
||||||
var http = require('http');
|
//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*";
|
||||||
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*";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
server.listen();
|
cors_proxy.createServer(cors_options).listen(8081, function() {
|
||||||
|
console.log('Running CORS Anywhere on :' + 8081 + " [" + process.pid + "]");
|
||||||
|
});
|
||||||
|
|
||||||
|
if(clustered) {
|
||||||
|
server.listen(onListen);
|
||||||
|
} else {
|
||||||
|
server.listen(port, onListen);
|
||||||
|
}
|
||||||
|
|
||||||
var socketIO = app.socketIO;
|
var socketIO = app.socketIO;
|
||||||
var redis = require('socket.io-redis');
|
|
||||||
try {
|
|
||||||
socketIO.adapter(redis({ host: 'localhost', port: 6379 }));
|
|
||||||
} catch(e) {
|
|
||||||
console.log("No redis-server to connect to..");
|
|
||||||
}
|
|
||||||
socketIO.listen(server);
|
socketIO.listen(server);
|
||||||
|
|
||||||
// Listen to messages sent from the master. Ignore everything else.
|
if(clustered) {
|
||||||
process.on('message', function(message, connection) {
|
var redis = require('socket.io-redis');
|
||||||
if (message !== 'sticky-session:connection') {
|
try {
|
||||||
return;
|
socketIO.adapter(redis({ host: 'localhost', port: 6379 }));
|
||||||
|
} catch(e) {
|
||||||
|
console.log("No redis-server to connect to..");
|
||||||
}
|
}
|
||||||
|
socketIO.listen(server);
|
||||||
|
process.on('message', function(message, connection) {
|
||||||
|
if (message !== 'sticky-session:connection') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
server.emit('connection', connection);
|
||||||
|
|
||||||
// Emulate a connection event on the server by emitting the
|
connection.resume();
|
||||||
// event with the connection the master sent us.
|
});
|
||||||
server.emit('connection', connection);
|
}
|
||||||
|
}
|
||||||
connection.resume();
|
|
||||||
});
|
function onListen() {
|
||||||
|
console.log("Started with pid [" + process.pid + "]");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user