From e3ccd4b3c7c13401155ed7bfde4ddcd0394410ac Mon Sep 17 00:00:00 2001 From: Enstrayed <48845980+Enstrayed@users.noreply.github.com> Date: Wed, 27 Mar 2024 17:01:18 -0700 Subject: [PATCH] move cider to use couchdb and introduce blogpost endpoint --- routes/blog.js | 40 ++++++++++++++++++++++++++++++++++++++++ routes/cider.js | 26 +++++++++++++++----------- 2 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 routes/blog.js diff --git a/routes/blog.js b/routes/blog.js new file mode 100644 index 0000000..c1e6f22 --- /dev/null +++ b/routes/blog.js @@ -0,0 +1,40 @@ +const { app, globalConfig, fs } = require("../index.js") // Get globals from index + +var timeSinceLastQuery = Date.now()-10000 +var cachedResult = {} + +app.get("/blogposts", (rreq, rres) => { + + if (Date.now() < timeSinceLastQuery+10000) { // if it has been <10 seconds since last request + rres.set("Access-Control-Allow-Origin","*") + rres.send(cachedResult) // send cached json + } else { + timeSinceLastQuery = Date.now() + cachedResult = parseFiles() + rres.set("Access-Control-Allow-Origin","*") + rres.send(cachedResult); + } + +}) + +function parseFiles() { + let files = fs.readdirSync(globalConfig.blog.postsDirectory) + let parsedFiles = [] + + for (x in files) { + if (files[x].endsWith(".html") === false) { break } // If file/dir is not .html then ignore + + let date = files[x].split("-")[0] + if (date < 10000000 || date > 99999999) { break } // If date does not fit ISO8601 format then ignore + + date = date.replace(/.{2}/g,"$&-").replace("-","").slice(0,-1) // Insert a dash every 2 characters, remove the first dash, remove the last character + + let name = files[x].slice(9).replace(/รท/g," ").replace(".html","") // Strip Date, replace seperator with space & remove file extension + + parsedFiles.push({ "date": date, "name": name, "path": `${globalConfig.blog.postsDirUrl}/${files[x]}`}) // Add metadata as JSON to array + } + + return parsedFiles.reverse() +} + +module.exports = {app} \ No newline at end of file diff --git a/routes/cider.js b/routes/cider.js index 69853fd..ea3cfdc 100644 --- a/routes/cider.js +++ b/routes/cider.js @@ -1,9 +1,7 @@ const { app, db, globalConfig } = require("../index.js") // Get globals from index -const date = new Date(); // Import Date for GET caching -timeSinceLastCiderQuery = Date.now()-2000; - -currentListening = {} // GET cache storage +var timeSinceLastCiderQuery = Date.now()-2000; +var currentListening = {} // GET cache storage app.get("/cider", (rreq,rres) => { // GET current listening from target @@ -40,14 +38,19 @@ app.get("/cider", (rreq,rres) => { // GET current listening from target }) app.post("/cider", (rreq,rres) => { // POST stop listening on cider target - db.get(globalConfig.cider.authKeyInDb).then(dbres => { // get auth keys from database - if (dbres == null) { // if key in db is null (non existant) then return 500 to requestee - console.log("ERROR: Configured key containing cider authkeys is null") - rres.sendStatus(500) + + fetch(`http://${globalConfig.couchdb.host}/apiauthkeys/cider`, { + headers: { + "Authorization": `Basic ${btoa(globalConfig.couchdb.authorization)}` + } + }).then(dbRes => dbRes.json()).then(dbRes => { + + if (dbRes.status == 404) { // If document containing cider auth keys does not exist + console.log("ERROR: Could not find apiauthkeys/cider") + rres.sendStatus(500) // Refuse request } else { - let validKeys = dbres.split(','); // format keys (stored as csv) - if (validKeys.includes(rreq.get("Authorization"))) { // if Authorization header exists in formatted keys array - + if (dbRes["content"][rreq.get("Authorization").split("_")[0]] === rreq.get("Authorization").split("_")[1]) { + fetch(`http://${globalConfig.cider.targetHost}:${globalConfig.cider.targetPort}/stop`).then(fres => { // send GET /stop to cider target if (fres.status == 204) { console.log(`${rreq.get("cf-connecting-ip")} POST /cider returned 200 KEY:${rreq.get("Authorization")}`) @@ -65,6 +68,7 @@ app.post("/cider", (rreq,rres) => { // POST stop listening on cider target } } }) + }) async function getCurrentListening() { // async function to actually get and return the json (this is just adapted from the original gist)