move cider to use couchdb and introduce blogpost endpoint
This commit is contained in:
40
routes/blog.js
Normal file
40
routes/blog.js
Normal file
@@ -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}
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user