Code clean up
This commit is contained in:
parent
b31d66d1b3
commit
2c7526e442
67
app.js
67
app.js
@ -123,55 +123,48 @@ function startMainLoop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function startServices() {
|
function startServices() {
|
||||||
server.start(function() {
|
server.start();
|
||||||
// Track how many people are currently connected.
|
|
||||||
server.io.on('connect', function(client) {
|
|
||||||
// If we haven't sent out at least one round of pings, disconnect them for now.
|
|
||||||
if (Object.keys(networkHistory).length < config.servers.length) {
|
|
||||||
client.disconnect();
|
|
||||||
|
|
||||||
return;
|
// Track how many people are currently connected.
|
||||||
}
|
server.io.on('connect', function(client) {
|
||||||
|
// We're good to connect them!
|
||||||
|
connectedClients += 1;
|
||||||
|
|
||||||
// We're good to connect them!
|
logger.log('info', '%s connected, total clients: %d', client.request.connection.remoteAddress, connectedClients);
|
||||||
connectedClients += 1;
|
|
||||||
|
|
||||||
logger.log('info', 'Accepted connection: %s, total clients: %d', client.request.connection.remoteAddress, connectedClients);
|
setTimeout(function() {
|
||||||
|
client.emit('setGraphDuration', config.graphDuration);
|
||||||
|
|
||||||
setTimeout(function() {
|
// Send them our previous data, so they have somewhere to start.
|
||||||
client.emit('setGraphDuration', config.graphDuration);
|
client.emit('updateMojangServices', mojang.toMessage());
|
||||||
|
|
||||||
// Send them our previous data, so they have somewhere to start.
|
// Remap our associative array into just an array.
|
||||||
client.emit('updateMojangServices', mojang.toMessage());
|
var networkHistoryKeys = Object.keys(networkHistory);
|
||||||
|
|
||||||
// Remap our associative array into just an array.
|
networkHistoryKeys.sort();
|
||||||
var networkHistoryKeys = Object.keys(networkHistory);
|
|
||||||
|
|
||||||
networkHistoryKeys.sort();
|
// Send each individually, this should look cleaner than waiting for one big array to transfer.
|
||||||
|
for (var i = 0; i < networkHistoryKeys.length; i++) {
|
||||||
|
client.emit('add', [networkHistory[networkHistoryKeys[i]]]);
|
||||||
|
}
|
||||||
|
}, 1);
|
||||||
|
|
||||||
// Send each individually, this should look cleaner than waiting for one big array to transfer.
|
// Attach our listeners.
|
||||||
for (var i = 0; i < networkHistoryKeys.length; i++) {
|
client.on('disconnect', function() {
|
||||||
client.emit('add', [networkHistory[networkHistoryKeys[i]]]);
|
connectedClients -= 1;
|
||||||
}
|
|
||||||
}, 1);
|
|
||||||
|
|
||||||
// Attach our listeners.
|
logger.log('info', '%s disconnected, total clients: %d', client.request.connection.remoteAddress, connectedClients);
|
||||||
client.on('disconnect', function() {
|
|
||||||
connectedClients -= 1;
|
|
||||||
|
|
||||||
logger.log('info', 'Client disconnected, total clients: %d', connectedClients);
|
|
||||||
});
|
|
||||||
|
|
||||||
client.on('requestHistoryGraph', function() {
|
|
||||||
if (config.logToDatabase) {
|
|
||||||
// Send them the big 24h graph.
|
|
||||||
client.emit('historyGraph', graphData);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
startMainLoop();
|
client.on('requestHistoryGraph', function() {
|
||||||
|
if (config.logToDatabase) {
|
||||||
|
// Send them the big 24h graph.
|
||||||
|
client.emit('historyGraph', graphData);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
startMainLoop();
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.log('info', 'Booting, please wait...');
|
logger.log('info', 'Booting, please wait...');
|
||||||
|
@ -8,8 +8,9 @@ var logger = require('./logger');
|
|||||||
|
|
||||||
var config = require('../config.json');
|
var config = require('../config.json');
|
||||||
|
|
||||||
exports.start = function(callback) {
|
var urlMapping = [];
|
||||||
var urlMapping = [];
|
|
||||||
|
function setupRoutes() {
|
||||||
var routeKeys = Object.keys(config.routes);
|
var routeKeys = Object.keys(config.routes);
|
||||||
|
|
||||||
// Map the (static) routes from our config.
|
// Map the (static) routes from our config.
|
||||||
@ -17,45 +18,49 @@ exports.start = function(callback) {
|
|||||||
urlMapping[routeKeys[i]] = config.routes[routeKeys[i]];
|
urlMapping[routeKeys[i]] = config.routes[routeKeys[i]];
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.log('info', Object.keys(config.routes));
|
logger.log('info', 'Routes: %s', routeKeys);
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleRequest(req, res) {
|
||||||
|
var requestUrl = url.parse(req.url).pathname;
|
||||||
|
|
||||||
|
logger.log('info', '%s requested: %s', req.connection.remoteAddress, requestUrl);
|
||||||
|
|
||||||
|
if (requestUrl === '/status.json') {
|
||||||
|
res.setHeader('Content-Type', 'text/plain');
|
||||||
|
|
||||||
|
res.write(JSON.stringify({
|
||||||
|
error: true,
|
||||||
|
message: 'API deprecated.'
|
||||||
|
}));
|
||||||
|
|
||||||
|
res.end();
|
||||||
|
} else if (requestUrl in urlMapping) {
|
||||||
|
var file = urlMapping[requestUrl];
|
||||||
|
|
||||||
|
res.setHeader('Content-Type', mime.lookup(file));
|
||||||
|
|
||||||
|
fs.createReadStream(file).pipe(res);
|
||||||
|
} else {
|
||||||
|
res.statusCode = 404;
|
||||||
|
|
||||||
|
res.write('404');
|
||||||
|
|
||||||
|
res.end();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.start = function() {
|
||||||
|
setupRoutes();
|
||||||
|
|
||||||
// Create our tiny little HTTP server.
|
// Create our tiny little HTTP server.
|
||||||
var server = http.createServer(function(req, res) {
|
var server = http.createServer(handleRequest);
|
||||||
var requestUrl = url.parse(req.url).pathname;
|
|
||||||
|
|
||||||
logger.log('info', '%s requested: %s', req.connection.remoteAddress, requestUrl);
|
|
||||||
|
|
||||||
if (requestUrl === '/status.json') {
|
|
||||||
res.setHeader('Content-Type', 'text/plain');
|
|
||||||
|
|
||||||
res.write(JSON.stringify({
|
|
||||||
error: true,
|
|
||||||
message: 'API deprecated.'
|
|
||||||
}));
|
|
||||||
|
|
||||||
res.end();
|
|
||||||
} else if (requestUrl in urlMapping) {
|
|
||||||
var file = urlMapping[requestUrl];
|
|
||||||
|
|
||||||
res.setHeader('Content-Type', mime.lookup(file));
|
|
||||||
|
|
||||||
fs.createReadStream(file).pipe(res);
|
|
||||||
} else {
|
|
||||||
res.statusCode = 404;
|
|
||||||
|
|
||||||
res.write('404');
|
|
||||||
|
|
||||||
res.end();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
server.listen(config.site.port, config.site.ip);
|
server.listen(config.site.port, config.site.ip);
|
||||||
|
|
||||||
// I don't like this. But it works, I think.
|
// I don't like this. But it works, I think.
|
||||||
exports.io = io.listen(server);
|
exports.io = io.listen(server);
|
||||||
|
|
||||||
// Since everything is loaded, do some final prep work.
|
// Since everything is loaded, let's celebrate!
|
||||||
logger.log('info', 'Started on %s:%d', config.site.ip, config.site.port);
|
logger.log('info', 'Started on %s:%d', config.site.ip, config.site.port);
|
||||||
|
|
||||||
callback();
|
|
||||||
};
|
};
|
Loading…
Reference in New Issue
Block a user