blob: 0197ba90d1196d559a592a70ebb16f91cc8a8abf [file] [log] [blame]
var http = require('http'),
url = require('url'),
fs = require('fs'),
path = require('path'),
mime = require('mime'),
config = require('./config'),
templates = require('./src/dashboard/templates'),
api = require('./src/dashboard/api'),
bootstrap = require('./bootstrap');
var boot_start = new Date().getTime();
// Different way of doing routes :P
function routeApi(resource) {
return function(req, res) {
console.log('[HTTP] API request for ' + resource + '.');
var queries = url.parse(req.url, true).query;
res.writeHead(200, {
"Content-Type":"application/json"
});
var json = api[resource];
if (queries.platform) {
json = json[queries.platform];
}
if (queries.sha) {
json = json[queries.sha];
}
res.write(JSON.stringify(json), 'utf-8');
res.end();
};
}
var routes = {
"":function(req, res) {
// Homepage
res.writeHead(200);
var html = templates.html();
res.write(html ? html : '<h1>not yet</h1>', 'utf-8');
res.end();
},
"api/results":routeApi('results'),
"api/errors":routeApi('errors'),
"api/commits/recent":routeApi('commits'),
"api/commits/tested":routeApi('tested_shas')
};
// cache local static content in memory (in memory? k)
['js', 'img', 'css'].forEach(function(media) {
var dir = path.join(__dirname, 'src', 'dashboard', 'templates', media);
fs.readdirSync(dir).forEach(function(m) {
var file_path = path.join(dir, m);
var contents = fs.readFileSync(file_path);
var type = mime.lookup(file_path);
routes[media + "/" + m] = function(req, res) {
res.setHeader('Content-Type', type);
res.writeHead(200);
res.write(contents);
res.end();
}
});
});
http.createServer(function (req, res) {
var method = req.method.toLowerCase();
// Get rid of leading and trailing / if exists
var route = url.parse(req.url).pathname.substr(1);
if (route[route.length-1] == '/') route = route.substr(0, route.length-1);
if (method == 'get' && route in routes) {
routes[route](req,res);
} else {
res.writeHead(404);
res.write('<h1>not found!</h1>', 'utf-8');
res.end();
}
}).listen(config.dashboard.port);
setTimeout(function() {
console.log('[BOOT] Cloning necessary git repos (bootstrap).');
bootstrap.go(function() {
console.log('[BOOT] Retrieving results from couch...');
api.boot(function() {
var boot_end = new Date().getTime();
var diff = (boot_end - boot_start)/1000;
console.log('[BOOT] Finished in ' + diff + ' seconds. READY TO ROCK!');
});
});
}, 3000);