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

View File

@ -8,8 +8,9 @@ var logger = require('./logger');
var config = require('../config.json');
exports.start = function(callback) {
var urlMapping = [];
var urlMapping = [];
function setupRoutes() {
var routeKeys = Object.keys(config.routes);
// Map the (static) routes from our config.
@ -17,45 +18,49 @@ exports.start = function(callback) {
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.
var server = http.createServer(function(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();
}
});
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();
};