Major changes
This commit is contained in:
@@ -1,55 +0,0 @@
|
||||
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
|
||||
|
||||
|
||||
if (rreq.query.format === "html") { // if ?format=html then send HTML
|
||||
rres.send(cachedResult.asHtml)
|
||||
} else { // otherwise send json
|
||||
rres.send(cachedResult.asJson)
|
||||
}
|
||||
|
||||
} else {
|
||||
timeSinceLastQuery = Date.now()
|
||||
cachedResult = parseFiles()
|
||||
|
||||
|
||||
if (rreq.query.format === "html") { // if ?format=html then send HTML
|
||||
rres.send(cachedResult.asHtml)
|
||||
} else { // otherwise send json
|
||||
rres.send(cachedResult.asJson)
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
function parseFiles() {
|
||||
let files = fs.readdirSync(globalConfig.blog.postsDirectory)
|
||||
let result = {
|
||||
asJson: [],
|
||||
asHtml: ""
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
result.asJson.unshift({ "date": date, "name": name, "path": `${globalConfig.blog.postsDirUrl}/${files[x]}`}) // Add to asJson array in the result
|
||||
result.asHtml = `<span>${date} <a href="${globalConfig.blog.postsDirUrl}/${files[x]}">${name}</a></span>`+result.asHtml
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
module.exports = {app}
|
||||
@@ -1,8 +1,9 @@
|
||||
const { app, globalConfig } = require("../index.js") // Get globals from index
|
||||
const { checkToken } = require("../liberals/auth.js")
|
||||
const { logRequest } = require("../liberals/logging.js")
|
||||
|
||||
app.get("/etyd*", (rreq,rres) => {
|
||||
fetch(`${globalConfig.couchdbHost}/etyd${rreq.path.replace("/etyd","")}`).then(dbRes => {
|
||||
app.get("/api/etyd*", (rreq,rres) => {
|
||||
fetch(`${globalConfig.couchdbHost}/etyd${rreq.path.replace("/api/etyd","")}`).then(dbRes => {
|
||||
if (dbRes.status == 404) {
|
||||
rres.sendStatus(404)
|
||||
} else {
|
||||
@@ -10,18 +11,18 @@ app.get("/etyd*", (rreq,rres) => {
|
||||
try {
|
||||
rres.redirect(dbRes.content.url) // Node will crash if the Database entry is malformed
|
||||
} catch (responseError) {
|
||||
logRequest(rres,rreq,500,responseError)
|
||||
rres.sendStatus(500)
|
||||
console.log(`${rres.get("cf-connecting-ip")} GET ${rreq.path} returned 500: ${responseError}`)
|
||||
}
|
||||
})
|
||||
}
|
||||
}).catch(fetchError => {
|
||||
logRequest(rres,rreq,500,fetchError)
|
||||
rres.sendStatus(500)
|
||||
console.log(`${rres.get("cf-connecting-ip")} GET ${rreq.path} returned 500: ${fetchError}`)
|
||||
})
|
||||
})
|
||||
|
||||
app.delete("/etyd*", (rreq,rres) => {
|
||||
app.delete("/api/etyd*", (rreq,rres) => {
|
||||
|
||||
if (rreq.get("Authorization") === undefined) {
|
||||
rres.sendStatus(400)
|
||||
@@ -31,25 +32,27 @@ app.delete("/etyd*", (rreq,rres) => {
|
||||
rres.sendStatus(401)
|
||||
} else if (authRes === true) { // Authorization successful
|
||||
|
||||
fetch(`${globalConfig.couchdbHost}/etyd${rreq.path.replace("/etyd", "")}`).then(dbRes => {
|
||||
fetch(`${globalConfig.couchdbHost}/etyd${rreq.path.replace("/api/etyd", "")}`).then(dbRes => {
|
||||
|
||||
if (dbRes.status == 404) {
|
||||
rres.sendStatus(404)
|
||||
rres.sendStatus(404) // Entry does not exist
|
||||
} else {
|
||||
dbRes.json().then(dbRes => {
|
||||
|
||||
fetch(`${globalConfig.couchdbHost}/etyd${rreq.path.replace("/etyd", "")}`, {
|
||||
fetch(`${globalConfig.couchdbHost}/etyd${rreq.path.replace("/api/etyd", "")}`, {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
"If-Match": dbRes["_rev"] // Using the If-Match header is easiest for deleting entries in couchdb
|
||||
}
|
||||
}).then(fetchRes => {
|
||||
if (fetchRes.status == 200) {
|
||||
console.log(`${rres.get("cf-connecting-ip")} DELETE ${rreq.path} returned 200 KEY: ${rreq.get("Authorization")}`)
|
||||
// console.log(`${rres.get("cf-connecting-ip")} DELETE ${rreq.path} returned 200 KEY: ${rreq.get("Authorization")}`)
|
||||
logRequest(rres,rreq,200)
|
||||
rres.sendStatus(200)
|
||||
}
|
||||
}).catch(fetchError => {
|
||||
console.log(`${rres.get("cf-connecting-ip")} DELETE ${rreq.path} returned 500: ${fetchError}`)
|
||||
// console.log(`${rres.get("cf-connecting-ip")} DELETE ${rreq.path} returned 500: ${fetchError}`)
|
||||
logRequest(rres,rreq,500,fetchError)
|
||||
rres.sendStatus(500)
|
||||
})
|
||||
|
||||
@@ -57,7 +60,7 @@ app.delete("/etyd*", (rreq,rres) => {
|
||||
}
|
||||
|
||||
}).catch(fetchError => {
|
||||
console.log(`${rres.get("cf-connecting-ip")} DELETE ${rreq.path} returned 500: ${fetchError}`)
|
||||
logRequest(rres,rreq,500,fetchError)
|
||||
rres.sendStatus(500)
|
||||
})
|
||||
|
||||
@@ -67,7 +70,7 @@ app.delete("/etyd*", (rreq,rres) => {
|
||||
|
||||
})
|
||||
|
||||
app.post("/etyd*", (rreq,rres) => {
|
||||
app.post("/api/etyd*", (rreq,rres) => {
|
||||
|
||||
if (rreq.get("Authorization") === undefined) {
|
||||
rres.sendStatus(400)
|
||||
@@ -80,7 +83,7 @@ app.post("/etyd*", (rreq,rres) => {
|
||||
if (rreq.body["url"] == undefined) {
|
||||
rres.sendStatus(400)
|
||||
} else {
|
||||
fetch(`${globalConfig.couchdbHost}/etyd${rreq.path.replace("/etyd", "")}`, {
|
||||
fetch(`${globalConfig.couchdbHost}/etyd${rreq.path.replace("/api/etyd", "")}`, {
|
||||
method: "PUT",
|
||||
body: JSON.stringify({
|
||||
"content": {
|
||||
@@ -95,16 +98,17 @@ app.post("/etyd*", (rreq,rres) => {
|
||||
break;
|
||||
|
||||
case 201:
|
||||
rres.status(200).send(rreq.path.replace("/etyd", ""))
|
||||
rres.status(200).send(rreq.path.replace("/api/etyd", ""))
|
||||
break;
|
||||
|
||||
default:
|
||||
console.log(`ERROR: CouchDB PUT did not return expected code: ${dbRes.status}`)
|
||||
logRequest(rres,rreq,500,`CouchDB PUT did not return expected code: ${dbRes.status}`)
|
||||
rres.sendStatus(500)
|
||||
break;
|
||||
}
|
||||
|
||||
}).catch(fetchError => {
|
||||
console.log(`${rres.get("cf-connecting-ip")} DELETE ${rreq.path} returned 500: ${fetchError}`)
|
||||
logRequest(rres,rreq,500,fetchError)
|
||||
rres.sendStatus(500)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
const { app } = require("../index.js")
|
||||
|
||||
app.get("/ip", (rreq,rres) => {
|
||||
app.get("/api/ip", (rreq,rres) => {
|
||||
let jsonResponse = {
|
||||
"IP": rreq.get("cf-connecting-ip") || rreq.ip,
|
||||
"Country": rreq.get("cf-ipcountry") || "not_cloudflare",
|
||||
@@ -10,7 +10,7 @@ app.get("/ip", (rreq,rres) => {
|
||||
rres.send(jsonResponse)
|
||||
})
|
||||
|
||||
app.get("/headers", (rreq,rres) => {
|
||||
app.get("/api/headers", (rreq,rres) => {
|
||||
rres.send(rreq.headers)
|
||||
})
|
||||
|
||||
|
||||
@@ -1,38 +1,22 @@
|
||||
const { app, globalConfig } = require("../index.js") // Get globals from index
|
||||
const { checkToken } = require("../liberals/auth.js")
|
||||
const { logRequest } = require("../liberals/logging.js")
|
||||
|
||||
app.post("/sendemail", (rreq,rres) => {
|
||||
|
||||
app.post("/api/sendemail", (rreq,rres) => {
|
||||
checkToken(rreq.get("Authorization"),"mailjet").then(authRes => {
|
||||
if (authRes === false) { // If the supplied authorization is invalid or an error occured
|
||||
|
||||
console.log(`${rreq.get("cf-connecting-ip")} POST /sendemail returned 401`) // Log the request
|
||||
if (authRes === false) {
|
||||
rres.sendStatus(401)
|
||||
|
||||
} else if (authRes === true) { // If the authorization was valid, continue function
|
||||
|
||||
// 2024-05-11: Turbo bodge check to make sure request JSON is valid, probably wont work but whatever
|
||||
if (rreq.body == undefined || rreq.body.recipient == undefined) {
|
||||
console.log(`${rreq.get("cf-connecting-ip")} POST /sendemail returned 400 KEY:${rreq.get("Authorization").split("_")[0]}`)
|
||||
} else if (authRes === true) {
|
||||
if (rreq.body == undefined || rreq.body.recipient == undefined) { // 2024-05-11: Turbo bodge check to make sure request JSON is valid, probably wont work but whatever
|
||||
rres.sendStatus(400)
|
||||
} else {
|
||||
|
||||
let message = {
|
||||
"Messages": [
|
||||
{
|
||||
"From": {
|
||||
"Email": globalConfig.mailjet.senderAddress
|
||||
},
|
||||
"To": [
|
||||
{
|
||||
"Email": rreq.body.recipient,
|
||||
}
|
||||
],
|
||||
|
||||
"Messages": [{
|
||||
"From": { "Email": globalConfig.mailjet.senderAddress },
|
||||
"To": [{ "Email": rreq.body.recipient, }],
|
||||
"Subject": rreq.body.subject || "Request did not include a subject.",
|
||||
"TextPart": rreq.body.message || "Request did not include a message.",
|
||||
}
|
||||
]
|
||||
}]
|
||||
}
|
||||
|
||||
fetch("https://api.mailjet.com/v3.1/send", {
|
||||
@@ -44,17 +28,16 @@ app.post("/sendemail", (rreq,rres) => {
|
||||
body: JSON.stringify(message)
|
||||
}).then(fetchRes => {
|
||||
if (fetchRes.status == 200) {
|
||||
console.log(`${rreq.get("cf-connecting-ip")} POST /sendemail returned 200 KEY:${rreq.get("Authorization").split("_")[1]}`)
|
||||
logRequest(rres,rreq,200)
|
||||
rres.sendStatus(200)
|
||||
} else {
|
||||
console.log(`Mailjet Fetch returned result other than OK: ${fetchRes.status} ${fetchRes.statusText}`)
|
||||
logRequest(rres,rreq,500,`Mailjet fetch did not return OK: ${fetchRes.status} ${fetchRes.statusText}`)
|
||||
rres.sendStatus(500)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
module.exports = {app} // export routes to be imported by index for execution
|
||||
module.exports = {app}
|
||||
@@ -1,5 +1,5 @@
|
||||
const { app, globalConfig } = require("../index.js")
|
||||
const { queryLastfm } = require("../liberals/nowplaying.js")
|
||||
const { queryLastfm } = require("../liberals/libnowplaying.js")
|
||||
|
||||
var timeSinceLastLastfmQuery = Date.now()-5000
|
||||
var cachedLastfmResult = {}
|
||||
@@ -11,14 +11,14 @@ const notPlayingAnythingPlaceholder = {
|
||||
"html": `<span>I'm not currently listening to anything.</span>`
|
||||
}
|
||||
|
||||
app.get("/nowplaying", (rreq,rres) => {
|
||||
app.get("/api/nowplaying", (rreq,rres) => {
|
||||
|
||||
if (Date.now() < timeSinceLastLastfmQuery+5000) {
|
||||
rres.send(cachedLastfmResult[rreq.query.format] ?? cachedLastfmResult.json)
|
||||
} else {
|
||||
timeSinceLastLastfmQuery = Date.now()
|
||||
queryLastfm().then(response => {
|
||||
if (response == 1) {
|
||||
if (response == {}) {
|
||||
cachedLastfmResult = notPlayingAnythingPlaceholder
|
||||
} else {
|
||||
cachedLastfmResult = response
|
||||
|
||||
Reference in New Issue
Block a user