diff --git a/master.js b/master.js index 112abdd9..21abe08b 100644 --- a/master.js +++ b/master.js @@ -5,13 +5,14 @@ const masterModFolder = "./database/masterMods/" const mkdirp = require("mkdirp"); mkdirp.sync("./database"); mkdirp.sync(masterModFolder); - +const path = require("path") const fs = require("fs") var nedb = require("nedb") // require config.json var config = require('./config'); var express = require("express"); +var ejs = require("ejs"); // Required for express post requests var bodyParser = require("body-parser"); var fileUpload = require('express-fileupload'); @@ -21,6 +22,14 @@ app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.use(fileUpload()); +// dynamic HTML generations with EJS +app.set('views', path.join(__dirname, 'static')); +app.set('view engine', 'ejs'); + +var routes = require("./routes.js"); +routes(app); + + // Set folder to serve static content from (the website) app.use(express.static('static')); // mod downloads diff --git a/package.json b/package.json index 238662cf..92c979b5 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "body-parser": "^1.15.2", "canvasjs": "^1.8.1", "child_process": "^1.0.2", + "ejs": "^2.5.6", "express": "^4.14.0", "express-fileupload": "0.0.6", "find-remove": "^1.0.0", diff --git a/routes.js b/routes.js new file mode 100644 index 00000000..4199ba46 --- /dev/null +++ b/routes.js @@ -0,0 +1,8 @@ +module.exports = function(app) { + app.get("/", function(req, res) { + res.render("index"); + }); + app.get("/nodes", function(req, res) { + res.render("nodes"); + }); +}; \ No newline at end of file diff --git a/static/head.html b/static/head.html new file mode 100644 index 00000000..7ce98e7d --- /dev/null +++ b/static/head.html @@ -0,0 +1,4 @@ +Clusterio + + + diff --git a/static/index.ejs b/static/index.ejs new file mode 100644 index 00000000..4b31b24c --- /dev/null +++ b/static/index.ejs @@ -0,0 +1,16 @@ + + + <%- include("head.html") %> + + + + <%- include("sidebar.html") %> +
+
+

Storage

+
+
+ +
+
+ \ No newline at end of file diff --git a/static/index.html b/static/index.html deleted file mode 100644 index 4c6789df..00000000 --- a/static/index.html +++ /dev/null @@ -1,48 +0,0 @@ - - - Clusterio - - - - - - - -
- - - - -
-
-
-

Storage

-
-
- -
-
-

Production

-
- -
-
-
-
-

Slaves

-
- -
-
-
- \ No newline at end of file diff --git a/static/main.js b/static/main.js index 5c63abe6..0876029d 100644 --- a/static/main.js +++ b/static/main.js @@ -29,196 +29,7 @@ function capitalizeFirstLetter(string) { function hideThis(object) { object.style.visibility = "hidden"; } -// function to sort arrays of objects after a keys value -function sortByKey(array, key) { - return array.sort(function(a, b) { - var x = Number(a[key]); var y = Number(b[key]); - return ((x < y) ? 1 : ((x > y) ? -1 : 0)); - }); -} -// function to draw data we recieve from ajax requests -function drawcontents(data) { - var data = sortByKey(data, "count"); - result = ""; - for(i = 0;i < data.length; i++) { - var img = ""; - if(imagedata[data[i].name]){ - img = "https://wiki.factorio.com/images/" + imagedata[data[i].name] + ".png" - } else { - img = "https://wiki.factorio.com/images/" + capitalizeFirstLetter(data[i].name) + ".png"; - } - result = result + ""; - } - result = result + "
" + data[i].name + "" + data[i].count + "
" - document.getElementById("contents").innerHTML = result; -} -// handle the navigation buttons -currentPage = "" -function display(page) { - var pages = document.querySelector("#body").childNodes; - for(i=0;i

Last seen: "+seenDate+"

Port: "+slaveData[key].serverPort+"

Host: "+slaveData[key].mac+"

" - } - } - document.querySelector("#slaves > #display").innerHTML = HTML - } - } - xmlhttp.open("GET", "slaves", true); - xmlhttp.send(); -}, 500) - // image data // key is the name of the item in the database, value is the name of the image on wiki.factorio.com/images/* // this list has to include all entities that doesn't just follow the simple capitalize first letter convention diff --git a/static/nodes.ejs b/static/nodes.ejs new file mode 100644 index 00000000..1457ee84 --- /dev/null +++ b/static/nodes.ejs @@ -0,0 +1,17 @@ + + + <%- include("head.html") %> + + + + + <%- include("sidebar.html") %> +
+
+

Slaves

+
+ +
+
+
+ \ No newline at end of file diff --git a/static/sidebar.html b/static/sidebar.html new file mode 100644 index 00000000..6f542679 --- /dev/null +++ b/static/sidebar.html @@ -0,0 +1,17 @@ + +
+ + + + +
\ No newline at end of file diff --git a/static/slaveDisplay.js b/static/slaveDisplay.js new file mode 100644 index 00000000..066ee2de --- /dev/null +++ b/static/slaveDisplay.js @@ -0,0 +1,28 @@ +// ask master for slaves +setInterval(function() { + var xmlhttp = new XMLHttpRequest(); + xmlhttp.onreadystatechange = function() { + if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { + var slaveData = JSON.parse(xmlhttp.responseText); + var HTML = "

Current connections:

" + for(i=0;i

Last seen: "+seenDate+"

Port: "+slaveData[key].serverPort+"

Host: "+slaveData[key].mac+"

" + } + } + document.querySelector("#slaves > #display").innerHTML = HTML + } + } + xmlhttp.open("GET", "slaves", true); + xmlhttp.send(); +}, 500) + +// get all slaves recently connected to master diff --git a/static/storage.js b/static/storage.js new file mode 100644 index 00000000..25daab92 --- /dev/null +++ b/static/storage.js @@ -0,0 +1,37 @@ +// function to draw data we recieve from ajax requests +function drawcontents(data) { + var data = sortByKey(data, "count"); + result = ""; + for(i = 0;i < data.length; i++) { + var img = ""; + if(imagedata[data[i].name]){ + img = "https://wiki.factorio.com/images/" + imagedata[data[i].name] + ".png" + } else { + img = "https://wiki.factorio.com/images/" + capitalizeFirstLetter(data[i].name) + ".png"; + } + result = result + ""; + } + result = result + "
" + data[i].name + "" + data[i].count + "
" + document.getElementById("contents").innerHTML = result; +} + +// get cluster inventory from master +setInterval(function() { + var xmlhttp = new XMLHttpRequest(); + xmlhttp.onreadystatechange = function() { + if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { + data = JSON.parse(xmlhttp.responseText); + drawcontents(data); + } + } + xmlhttp.open("GET", "inventory", true); + xmlhttp.send(); +}, 500) + +// function to sort arrays of objects after a keys value +function sortByKey(array, key) { + return array.sort(function(a, b) { + var x = Number(a[key]); var y = Number(b[key]); + return ((x < y) ? 1 : ((x > y) ? -1 : 0)); + }); +} \ No newline at end of file diff --git a/static/style.css b/static/style.css index 13e5a73c..86e80a73 100644 --- a/static/style.css +++ b/static/style.css @@ -16,9 +16,6 @@ #body { margin-left:200px; } -#body > div { - display:none; -} #body > #production { display:block; }