Code clean up

This commit is contained in:
Cryptkeeper 2015-12-26 14:18:09 -06:00
parent b31d66d1b3
commit 2c7526e442
2 changed files with 69 additions and 71 deletions

15
app.js

@ -123,20 +123,14 @@ function startMainLoop() {
}
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;
}
// We're good to connect them!
connectedClients += 1;
logger.log('info', 'Accepted connection: %s, total clients: %d', client.request.connection.remoteAddress, connectedClients);
logger.log('info', '%s connected, total clients: %d', client.request.connection.remoteAddress, connectedClients);
setTimeout(function() {
client.emit('setGraphDuration', config.graphDuration);
@ -159,7 +153,7 @@ function startServices() {
client.on('disconnect', function() {
connectedClients -= 1;
logger.log('info', 'Client disconnected, total clients: %d', connectedClients);
logger.log('info', '%s disconnected, total clients: %d', client.request.connection.remoteAddress, connectedClients);
});
client.on('requestHistoryGraph', function() {
@ -171,7 +165,6 @@ function startServices() {
});
startMainLoop();
});
}
logger.log('info', 'Booting, please wait...');

@ -8,8 +8,9 @@ var logger = require('./logger');
var config = require('../config.json');
exports.start = function(callback) {
var urlMapping = [];
function setupRoutes() {
var routeKeys = Object.keys(config.routes);
// Map the (static) routes from our config.
@ -17,10 +18,10 @@ exports.start = function(callback) {
urlMapping[routeKeys[i]] = config.routes[routeKeys[i]];
}
logger.log('info', Object.keys(config.routes));
logger.log('info', 'Routes: %s', routeKeys);
}
// Create our tiny little HTTP server.
var server = http.createServer(function(req, res) {
function handleRequest(req, res) {
var requestUrl = url.parse(req.url).pathname;
logger.log('info', '%s requested: %s', req.connection.remoteAddress, requestUrl);
@ -47,15 +48,19 @@ exports.start = function(callback) {
res.end();
}
});
}
exports.start = function() {
setupRoutes();
// Create our tiny little HTTP server.
var server = http.createServer(handleRequest);
server.listen(config.site.port, config.site.ip);
// I don't like this. But it works, I think.
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);
callback();
};