From a24318185648d8631c34ab8e3c3b6f7cf5d4ab15 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Thu, 16 Mar 2017 09:23:59 +0100 Subject: [PATCH] Moved API endpoints to /api/ --- client.js | 20 ++++++++++---------- master.js | 22 +++++++++++----------- static/slaveDisplay.js | 4 ++-- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/client.js b/client.js index 4453c735..bf9df53f 100644 --- a/client.js +++ b/client.js @@ -383,7 +383,7 @@ function instanceManagement() { if (err) throw err payload.mac = mac // console.log(payload) - needle.post(config.masterIP + ":" + config.masterPort + '/getID', payload, function (err, response, body) { + needle.post(config.masterIP + ":" + config.masterPort + '/api/getID', payload, function (err, response, body) { if (response && response.body) { // In the future we might be interested in whether or not we actually manage to send it, but honestly I don't care. console.log(response.body) @@ -408,7 +408,7 @@ function instanceManagement() { modName: modHashes[i].modName, hash: modHashes[i].hash, } - needle.post(config.masterIP + ":" + config.masterPort + '/checkMod', payload, function (err, response, body) { + needle.post(config.masterIP + ":" + config.masterPort + '/api/checkMod', payload, function (err, response, body) { if(err) throw err // Unable to contact master server! Please check your config.json. if(response && body && body == "found") { console.log("master has mod") @@ -417,7 +417,7 @@ function instanceManagement() { console.log("Sending mod: " + mod) // Send mods master says it wants // response.body is a string which is a modName.zip - var req = request.post("http://"+config.masterIP + ":" + config.masterPort + '/uploadMod', function (err, resp, body) { + var req = request.post("http://"+config.masterIP + ":" + config.masterPort + '/api/uploadMod', function (err, resp, body) { if (err) { console.log('Error!'); throw err @@ -467,8 +467,8 @@ function instanceManagement() { payload[key] = 0; } } - // console.log("Recorded flows, copper plate since last time: " + payload["copper-plate"]); - needle.post(config.masterIP + ":" + config.masterPort + '/logStats', {timestamp: timestamp, slaveID: instanceconfig.unique,data: payload}, function (err, response, body) { + console.log("Recorded flows, copper plate since last time: " + payload["copper-plate"]); + needle.post(config.masterIP + ":" + config.masterPort + '/api/logStats', {timestamp: timestamp, slaveID: instanceconfig.unique,data: payload}, function (err, response, body) { // we did it, keep going }) } @@ -495,7 +495,7 @@ function instanceManagement() { g[0] = g[0].replace("\u0000", ""); // console.log("exporting " + JSON.stringify(g)); // send our entity and count to the master for him to keep track of - needle.post(config.masterIP + ":" + config.masterPort + '/place', { + needle.post(config.masterIP + ":" + config.masterPort + '/api/place', { name: g[0], count: g[1] }, function (err, resp, body) { @@ -543,7 +543,7 @@ function instanceManagement() { // request our items, one item at a time for (let i = 0; i < Object.keys(preparedPackage).length; i++) { console.log(preparedPackage[Object.keys(preparedPackage)[i]]) - needle.post(config.masterIP + ":" + config.masterPort + '/remove', preparedPackage[Object.keys(preparedPackage)[i]], function (err, response, body) { + needle.post(config.masterIP + ":" + config.masterPort + '/api/remove', preparedPackage[Object.keys(preparedPackage)[i]], function (err, response, body) { if (response && response.body && typeof response.body == "object") { // buffer confirmed orders confirmedOrders[confirmedOrders.length] = { @@ -563,7 +563,7 @@ function instanceManagement() { // COMBINATOR SIGNALS --------------------------------------------------------- // get inventory from Master and RCON it to our slave setInterval(function () { - needle.get(config.masterIP + ":" + config.masterPort + '/inventory', function (err, response, body) { + needle.get(config.masterIP + ":" + config.masterPort + '/api/inventory', function (err, response, body) { if (response && response.body) { // Take the inventory we (hopefully) got and turn it into the format LUA accepts // console.log(response.body) @@ -582,7 +582,7 @@ function instanceManagement() { // send any signals the slave has been told to send setInterval(function () { // Fetch combinator signals from the server - needle.post(config.masterIP + ":" + config.masterPort + '/readSignal', { + needle.post(config.masterIP + ":" + config.masterPort + '/api/readSignal', { since: lastSignalCheck }, function (err, response, body) { if (response && response.body && typeof response.body == "object" && response.body[0]) { @@ -617,7 +617,7 @@ function instanceManagement() { frame: framepart, // thats our array of objects(single signals) } // console.log(doneframe) - needle.post(config.masterIP + ":" + config.masterPort + '/setSignal', doneframe, function (err, response, body) { + needle.post(config.masterIP + ":" + config.masterPort + '/api/setSignal', doneframe, function (err, response, body) { if (response && response.body) { // In the future we might be interested in whether or not we actually manage to send it, but honestly I don't care. } diff --git a/master.js b/master.js index 2e1d23d1..dc90c08e 100644 --- a/master.js +++ b/master.js @@ -69,7 +69,7 @@ db.items.additem = function(object) { var slaves = {} // world ID management // slaves post here to tell the server they exist -app.post("/getID", function(req,res) { +app.post("/api/getID", function(req,res) { // request.body should be an object // {rconPort, rconPassword, serverPort, mac, time} // time us a unix timestamp we can use to check for how long the server has been unresponsive @@ -81,7 +81,7 @@ app.post("/getID", function(req,res) { }); // mod management // should handle uploading and checking if mods are uploaded -app.post("/checkMod", function(req,res) { +app.post("/api/checkMod", function(req,res) { let files = fs.readdirSync(masterModFolder) let found = false; files.forEach(file => { @@ -97,7 +97,7 @@ app.post("/checkMod", function(req,res) { } res.end() }); -app.post("/uploadMod", function(req,res) { +app.post("/api/uploadMod", function(req,res) { if (!req.files) { res.send('No files were uploaded.'); return; @@ -114,7 +114,7 @@ app.post("/uploadMod", function(req,res) { } }); // endpoint for getting information about all our slaves -app.get("/slaves", function(req, res) { +app.get("/api/slaves", function(req, res) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); let copyOfSlaves = slaves @@ -126,7 +126,7 @@ app.get("/slaves", function(req, res) { }); // endpoint to send items to -app.post("/place", function(req, res) { +app.post("/api/place", function(req, res) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); console.log("added: " + req.body.name + " " + req.body.count); @@ -137,7 +137,7 @@ app.post("/place", function(req, res) { }); // endpoint to remove items from DB when client orders items -app.post("/remove", function(req, res) { +app.post("/api/remove", function(req, res) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); // save items we get @@ -170,14 +170,14 @@ app.post("/remove", function(req, res) { }); // circuit stuff -app.post("/setSignal", function(req,res) { +app.post("/api/setSignal", function(req,res) { if(typeof req.body == "object" && req.body.time){ db.signals.insert(req.body); // console.log("signal set") } }); -app.post("/readSignal", function(req,res) { +app.post("/api/readSignal", function(req,res) { // request.body should be an object // {since:UNIXTIMESTAMP,} // we should send an array of all signals since then @@ -189,7 +189,7 @@ app.post("/readSignal", function(req,res) { }); // endpoint for getting an inventory of what we got -app.get("/inventory", function(req, res) { +app.get("/api/inventory", function(req, res) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); // Check it and send it @@ -200,7 +200,7 @@ app.get("/inventory", function(req, res) { // post flowstats here for production graphs // {timestamp: Date, slaveID: string, data: {"item":number}} -app.post("/logStats", function(req,res) { +app.post("/api/logStats", function(req,res) { if(typeof req.body == "object" && req.body.slaveID && req.body.timestamp && req.body.data) { if(Number(req.body.timestamp) != NaN){ req.body.timestamp = Number(req.body.timestamp); @@ -219,7 +219,7 @@ app.post("/logStats", function(req,res) { } }) // {slaveID: string, fromTime: Date, toTime, Date} -app.post("/getStats", function(req,res) { +app.post("/api/getStats", function(req,res) { console.log(req.body) if(typeof req.body == "object" && req.body.slaveID) { // if not specified, get stats for last 24 hours diff --git a/static/slaveDisplay.js b/static/slaveDisplay.js index 70e42179..b902cf87 100644 --- a/static/slaveDisplay.js +++ b/static/slaveDisplay.js @@ -33,14 +33,14 @@ setTimeout(function() { } } } - xmlhttp.open("GET", "slaves", true); + xmlhttp.open("GET", "api/slaves", true); xmlhttp.send(); }, 0) // ID of slave, ID of canvasjs div without # function makeGraph(slaveID, selector) { - post("getStats", {slaveID: slaveID}, function(data){ + post("api/getStats", {slaveID: slaveID}, function(data){ console.log("Building chart " + slaveID + " with this data:") console.log(data) if(data.length > 0) {