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
View File

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

4
assets/css/main.css Normal file
View File

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

View File

@ -4,32 +4,18 @@
<head>
<link rel="stylesheet" type="text/css" href="css/main.css">
<title>Minetrack</title>
</head>
<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/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var socket = io.connect();
socket.on('update', function(network) {
console.log(network);
});
socket.on('add', function(networks) {
console.log(networks);
});
});
</script>
<script src="js/site.js"></script>
</body>

7
assets/js/site.js Normal file
View File

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

View File

@ -29,5 +29,11 @@
"site": {
"port": 80,
"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"
}
}

View File

@ -4,9 +4,20 @@ var url = require('url');
var mime = require('mime');
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 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.
exports.io = (io = io.listen(server));
// Since everything is loaded, do some final prep work.
callback();
};
console.log('Started on %s:%d', config.site.ip, config.site.port);
exports.urlMapping = urlMapping;
callback();
};