From ce1912428616d9329952d4a3c20d9324548ae37c Mon Sep 17 00:00:00 2001 From: ryzetech Date: Mon, 23 Jan 2023 12:43:59 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20make=20the=20code=20less=20shit?= =?UTF-8?q?=20to=20read?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 52 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/app.js b/app.js index 0a5af08..4f00a5d 100644 --- a/app.js +++ b/app.js @@ -44,16 +44,7 @@ let root = { let app = express(); -/* leaving this here for reference -app.options('/graphql', function (req, res) { - res.header('Access-Control-Allow-Origin', '*'); - res.header('Access-Control-Allow-Methods', 'GET,POST,OPTIONS'); - res.send(200); -}); -*/ - app.use(cors()); - app.use('/graphql', graphqlHTTP({ schema: schema, rootValue: root, @@ -75,20 +66,31 @@ async function loop() { console.log(new Date(), "Checking for spaces..."); let changecount = 0; for (const space of spaces) { - //console.log(`Checking ${space.id}...`); let response = await checkSpace(space); - //console.log(`Space ${space.id} is ${o ? "open" : "closed"}`); if (typeof response.open === "undefined") { console.error(`The space ${space.id} might not be reachable. Please check the endpoint.`); continue; } if (JSON.stringify(response) != JSON.stringify(cache.get(space.id))) { cache.set(space.id, response); + + // update or create the space in the database let update = await prisma.space.upsert({ where: { id: space.id }, - update: { open: response.open, lastChange: response.lastchange }, - create: { id: space.id, open: response.open, lastChange: response.lastchange, name: space.name } + + update: { + open: response.open, + lastChange: response.lastchange + }, + + create: { + id: space.id, + open: response.open, + lastChange: response.lastchange, + name: space.name, + } }); + changecount++; } } @@ -99,20 +101,34 @@ async function loop() { async function checkSpace(space) { let response, data, open; let lastchange = null; + try { response = await fetch(space.endpoint); data = await response.json(); - } catch (e) { console.error(`The space ${space.id} might not be reachable. Please check the endpoint. Error: ${e}`); } + } + catch (e) { + console.error(`The space ${space.id} might not be reachable. Please check the endpoint. Error: ${e}`); + } + // Check if the space is using the SpaceAPI standard if (!space.path) { try { open = data.state.open; lastchange = new Date(data.state.lastchange*1000); } - catch { console.error(`The space ${space.id} is not using the SpaceAPI standard. Please specify a path.`); } - } else { - try { open = (jp.query(data, space.path) == (space.expected ? space.expected : true)); } - catch { console.error(`The space ${space.id} has an invalid JSONPath to the target value. Please use https://jsonpath.com/ to evaluate the path.`); } + catch { + console.error(`The space ${space.id} is not using the SpaceAPI standard. Please specify a path.`); + } } + else { + try { + // Query the JSONPath. If the expected value is not set, assume true or a similar value like 1. + open = (jp.query(data, space.path) == (space.expected ? space.expected : true)); + } + catch { + console.error(`The space ${space.id} has an invalid JSONPath to the target value. Please use https://jsonpath.com/ to evaluate the path.`); + } + } + return { open, lastchange }; } \ No newline at end of file