From 89bd722fa136ac022a651550fa59d760d9b3c1b2 Mon Sep 17 00:00:00 2001 From: Cryptkeeper Date: Sun, 1 Nov 2015 23:19:27 -0600 Subject: [PATCH] Setup structure, dynamic routing from config.json --- app.js | 10 +++------- assets/css/main.css | 4 ++++ assets/html/index.html | 20 +++----------------- assets/js/site.js | 7 +++++++ config.json | 6 ++++++ lib/server.js | 23 +++++++++++++++++------ 6 files changed, 40 insertions(+), 30 deletions(-) create mode 100644 assets/css/main.css create mode 100644 assets/js/site.js diff --git a/app.js b/app.js index e1cb9da..c51803b 100644 --- a/app.js +++ b/app.js @@ -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); diff --git a/assets/css/main.css b/assets/css/main.css new file mode 100644 index 0000000..6ef5823 --- /dev/null +++ b/assets/css/main.css @@ -0,0 +1,4 @@ +* { + margin: 0; + padding: 0; +} \ No newline at end of file diff --git a/assets/html/index.html b/assets/html/index.html index c14e783..176f662 100644 --- a/assets/html/index.html +++ b/assets/html/index.html @@ -4,32 +4,18 @@ + + Minetrack - - - + diff --git a/assets/js/site.js b/assets/js/site.js new file mode 100644 index 0000000..b35d6e0 --- /dev/null +++ b/assets/js/site.js @@ -0,0 +1,7 @@ +$(document).ready(function() { + var socket = io.connect(); + + socket.on('connect', function() { + + }); +}); \ No newline at end of file diff --git a/config.json b/config.json index e1b53d7..225c025 100644 --- a/config.json +++ b/config.json @@ -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" } } \ No newline at end of file diff --git a/lib/server.js b/lib/server.js index e6ceb2b..1f11f2c 100644 --- a/lib/server.js +++ b/lib/server.js @@ -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; \ No newline at end of file + callback(); +}; \ No newline at end of file