2015-11-02 04:56:08 +00:00
|
|
|
var http = require('http');
|
|
|
|
var fs = require('fs');
|
|
|
|
var url = require('url');
|
|
|
|
var mime = require('mime');
|
|
|
|
var io = require('socket.io');
|
|
|
|
|
2016-02-07 00:26:29 +00:00
|
|
|
var util = require('./util');
|
2015-11-02 05:46:24 +00:00
|
|
|
var logger = require('./logger');
|
|
|
|
|
2015-11-02 05:19:27 +00:00
|
|
|
var config = require('../config.json');
|
2017-03-11 23:44:21 +00:00
|
|
|
var minecraft = require('../minecraft.json');
|
2016-02-06 23:34:33 +00:00
|
|
|
var servers = require('../servers.json');
|
2015-11-02 04:56:08 +00:00
|
|
|
|
2015-12-26 20:18:09 +00:00
|
|
|
var urlMapping = [];
|
|
|
|
|
|
|
|
function setupRoutes() {
|
2015-11-02 05:19:27 +00:00
|
|
|
var routeKeys = Object.keys(config.routes);
|
|
|
|
|
|
|
|
// Map the (static) routes from our config.
|
|
|
|
for (var i = 0; i < routeKeys.length; i++) {
|
|
|
|
urlMapping[routeKeys[i]] = config.routes[routeKeys[i]];
|
|
|
|
}
|
|
|
|
|
2015-12-26 20:18:09 +00:00
|
|
|
logger.log('info', 'Routes: %s', routeKeys);
|
|
|
|
}
|
2015-11-02 05:19:27 +00:00
|
|
|
|
2015-12-26 20:18:09 +00:00
|
|
|
function handleRequest(req, res) {
|
|
|
|
var requestUrl = url.parse(req.url).pathname;
|
2015-11-02 04:56:08 +00:00
|
|
|
|
2020-04-02 02:54:43 +00:00
|
|
|
logger.log('info', '%s requested: %s', util.getRemoteAddr(req), requestUrl);
|
2015-11-02 07:08:54 +00:00
|
|
|
|
2020-03-30 06:06:50 +00:00
|
|
|
if (requestUrl === '/publicConfig.json') {
|
2016-02-06 23:34:33 +00:00
|
|
|
res.setHeader('Content-Type', 'application/javascript');
|
|
|
|
|
|
|
|
var publicConfig = {
|
|
|
|
graphDuration: config.graphDuration,
|
2016-02-07 00:26:29 +00:00
|
|
|
servers: servers,
|
2016-02-24 01:19:15 +00:00
|
|
|
bootTime: util.getBootTime(),
|
2017-03-11 23:44:21 +00:00
|
|
|
serverTypesVisible: config.serverTypesVisible || false,
|
|
|
|
minecraftVersions: minecraft.versions
|
2016-02-06 23:34:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
res.write('setPublicConfig(' + JSON.stringify(publicConfig) + ');');
|
|
|
|
|
2015-12-26 20:18:09 +00:00
|
|
|
res.end();
|
|
|
|
} else if (requestUrl in urlMapping) {
|
|
|
|
var file = urlMapping[requestUrl];
|
2015-11-02 04:56:08 +00:00
|
|
|
|
2020-04-02 01:42:27 +00:00
|
|
|
res.setHeader('Content-Type', mime.getType(file));
|
2017-03-11 23:44:21 +00:00
|
|
|
|
2015-12-26 20:18:09 +00:00
|
|
|
fs.createReadStream(file).pipe(res);
|
|
|
|
} else {
|
|
|
|
res.statusCode = 404;
|
2015-12-19 03:23:24 +00:00
|
|
|
|
2015-12-26 20:18:09 +00:00
|
|
|
res.write('404');
|
|
|
|
|
|
|
|
res.end();
|
|
|
|
}
|
|
|
|
}
|
2015-11-02 04:56:08 +00:00
|
|
|
|
2015-12-26 20:18:09 +00:00
|
|
|
exports.start = function() {
|
|
|
|
setupRoutes();
|
|
|
|
|
|
|
|
// Create our tiny little HTTP server.
|
|
|
|
var server = http.createServer(handleRequest);
|
2015-11-02 04:56:08 +00:00
|
|
|
|
2015-11-02 05:19:27 +00:00
|
|
|
server.listen(config.site.port, config.site.ip);
|
2015-11-02 04:56:08 +00:00
|
|
|
|
|
|
|
// I don't like this. But it works, I think.
|
2015-12-19 03:23:24 +00:00
|
|
|
exports.io = io.listen(server);
|
2015-11-02 04:56:08 +00:00
|
|
|
|
2015-12-26 20:18:09 +00:00
|
|
|
// Since everything is loaded, let's celebrate!
|
2015-11-02 05:46:24 +00:00
|
|
|
logger.log('info', 'Started on %s:%d', config.site.ip, config.site.port);
|
2017-03-11 23:44:21 +00:00
|
|
|
};
|