Setup structure, dynamic routing from config.json

This commit is contained in:
Cryptkeeper 2015-11-01 23:19:27 -06:00
parent 86853613cc
commit 89bd722fa1
6 changed files with 40 additions and 30 deletions

10
app.js

@ -18,10 +18,10 @@ setInterval(function() {
console.log('Failed to ping ' + network.ip + ': ' + err); console.log('Failed to ping ' + network.ip + ': ' + err);
} else { } else {
console.log(network.ip + ' reply: ' + result.players.online + '/' + result.players.max); console.log(network.ip + ' reply: ' + result.players.online + '/' + result.players.max);
server.io.sockets.emit('update', result);
} }
server.io.sockets.emit('update', result);
// Log our response. // Log our response.
if (!networkHistory[network.ip]) { if (!networkHistory[network.ip]) {
networkHistory[network.ip] = []; networkHistory[network.ip] = [];
@ -52,11 +52,7 @@ setInterval(function() {
console.log('Connected clients: %d', connectedClients); console.log('Connected clients: %d', connectedClients);
}, 1000); }, 1000);
// Manually construct our paths. server.start(function() {
server.urlMapping['/'] = 'assets/html/index.html';
server.urlMapping['/compass-icon'] = 'assets/images/compass.png';
server.start(config.site.ip, config.site.port, function() {
// Track how many people are currently connected. // Track how many people are currently connected.
server.io.on('connect', function(client) { server.io.on('connect', function(client) {
console.log('Incoming connection: %s', client.request.connection.remoteAddress); console.log('Incoming connection: %s', client.request.connection.remoteAddress);

4
assets/css/main.css Normal file

@ -0,0 +1,4 @@
* {
margin: 0;
padding: 0;
}

@ -4,32 +4,18 @@
<head> <head>
<link rel="stylesheet" type="text/css" href="css/main.css">
<title>Minetrack</title> <title>Minetrack</title>
</head> </head>
<body> <body>
<img src="/compass-icon">
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.7/socket.io.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.7/socket.io.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript"> <script src="js/site.js"></script>
$(document).ready(function() {
var socket = io.connect();
socket.on('update', function(network) {
console.log(network);
});
socket.on('add', function(networks) {
console.log(networks);
});
});
</script>
</body> </body>

7
assets/js/site.js Normal file

@ -0,0 +1,7 @@
$(document).ready(function() {
var socket = io.connect();
socket.on('connect', function() {
});
});

@ -29,5 +29,11 @@
"site": { "site": {
"port": 80, "port": 80,
"ip": "0.0.0.0" "ip": "0.0.0.0"
},
"routes": {
"/": "assets/html/index.html",
"/images/compass.png": "assets/images/compass.png",
"/js/site.js": "assets/js/site.js",
"/css/main.css": "assets/css/main.css"
} }
} }

@ -4,9 +4,20 @@ var url = require('url');
var mime = require('mime'); var mime = require('mime');
var io = require('socket.io'); var io = require('socket.io');
var urlMapping = []; var config = require('../config.json');
exports.start = function(ip, port, callback) { exports.start = function(callback) {
var urlMapping = [];
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]];
}
console.log(Object.keys(config.routes));
// Create our tiny little HTTP server.
var server = http.createServer(function(req, res) { var server = http.createServer(function(req, res) {
var requestUrl = url.parse(req.url).pathname; var requestUrl = url.parse(req.url).pathname;
@ -24,13 +35,13 @@ exports.start = function(ip, port, callback) {
} }
}); });
server.listen(port, 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 = io.listen(server)); exports.io = (io = io.listen(server));
// Since everything is loaded, do some final prep work. // Since everything is loaded, do some final prep work.
console.log('Started on %s:%d', config.site.ip, config.site.port);
callback(); callback();
}; };
exports.urlMapping = urlMapping;