""refactor"" basically everything
This commit is contained in:
@@ -1,34 +0,0 @@
|
||||
const { globalConfig, app } = require("../index.js")
|
||||
const { logRequest } = require("../liberals/logging.js")
|
||||
|
||||
app.delete("/api/token", (rreq,rres) => {
|
||||
fetch(`${globalConfig.couchdbHost}/auth/sessions`).then(res => res.json()).then(fetchRes => {
|
||||
if (fetchRes.sessions[rreq.get("Authorization")]) {
|
||||
delete fetchRes.sessions[rreq.get("Authorization")]
|
||||
fetch(`${globalConfig.couchdbHost}/auth/sessions`, {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"If-Match": fetchRes._rev
|
||||
},
|
||||
body: JSON.stringify({
|
||||
sessions: fetchRes.sessions
|
||||
})
|
||||
|
||||
}).then(res => {
|
||||
if (res.status == 201) {
|
||||
rres.sendStatus(200)
|
||||
} else {
|
||||
logRequest(rres,rreq,500,`Token invalidation may have failed: ${res.status} ${res.statusText}`)
|
||||
rres.sendStatus(500)
|
||||
}
|
||||
}).catch(fetchError => {
|
||||
logRequest(rres,rreq,500,fetchError)
|
||||
rres.sendStatus(500)
|
||||
})
|
||||
} else {
|
||||
rres.sendStatus(400)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
module.exports = {app}
|
||||
122
routes/etyd.js
122
routes/etyd.js
@@ -1,122 +0,0 @@
|
||||
const { app, globalConfig } = require("../index.js") // Get globals from index
|
||||
const { checkToken } = require("../liberals/auth.js")
|
||||
const { logRequest } = require("../liberals/logging.js")
|
||||
|
||||
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 {
|
||||
dbRes.json().then(dbRes => {
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}).catch(fetchError => {
|
||||
logRequest(rres,rreq,500,fetchError)
|
||||
rres.sendStatus(500)
|
||||
})
|
||||
})
|
||||
|
||||
app.delete("/api/etyd*", (rreq,rres) => {
|
||||
|
||||
if (rreq.get("Authorization") === undefined) {
|
||||
rres.sendStatus(400)
|
||||
} else {
|
||||
checkToken(rreq.get("Authorization"),"etyd").then(authRes => {
|
||||
if (authRes === false) {
|
||||
rres.sendStatus(401)
|
||||
} else if (authRes === true) { // Authorization successful
|
||||
|
||||
fetch(`${globalConfig.couchdbHost}/etyd${rreq.path.replace("/api/etyd", "")}`).then(dbRes => {
|
||||
|
||||
if (dbRes.status == 404) {
|
||||
rres.sendStatus(404) // Entry does not exist
|
||||
} else {
|
||||
dbRes.json().then(dbRes => {
|
||||
|
||||
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")}`)
|
||||
logRequest(rres,rreq,200)
|
||||
rres.sendStatus(200)
|
||||
}
|
||||
}).catch(fetchError => {
|
||||
// console.log(`${rres.get("cf-connecting-ip")} DELETE ${rreq.path} returned 500: ${fetchError}`)
|
||||
logRequest(rres,rreq,500,fetchError)
|
||||
rres.sendStatus(500)
|
||||
})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
}).catch(fetchError => {
|
||||
logRequest(rres,rreq,500,fetchError)
|
||||
rres.sendStatus(500)
|
||||
})
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
app.post("/api/etyd*", (rreq,rres) => {
|
||||
|
||||
if (rreq.get("Authorization") === undefined) {
|
||||
rres.sendStatus(400)
|
||||
} else {
|
||||
checkToken(rreq.get("Authorization"),"etyd").then(authRes => {
|
||||
if (authRes === false) {
|
||||
rres.sendStatus(401)
|
||||
} else if (authRes === true) { // Authorization successful
|
||||
|
||||
if (rreq.body["url"] == undefined) {
|
||||
rres.sendStatus(400)
|
||||
} else {
|
||||
fetch(`${globalConfig.couchdbHost}/etyd${rreq.path.replace("/api/etyd", "")}`, {
|
||||
method: "PUT",
|
||||
body: JSON.stringify({
|
||||
"content": {
|
||||
"url": rreq.body["url"]
|
||||
}
|
||||
})
|
||||
}).then(dbRes => {
|
||||
|
||||
switch(dbRes.status) {
|
||||
case 409:
|
||||
rres.sendStatus(409)
|
||||
break;
|
||||
|
||||
case 201:
|
||||
rres.status(200).send(rreq.path.replace("/api/etyd", ""))
|
||||
break;
|
||||
|
||||
default:
|
||||
logRequest(rres,rreq,500,`CouchDB PUT did not return expected code: ${dbRes.status}`)
|
||||
rres.sendStatus(500)
|
||||
break;
|
||||
}
|
||||
|
||||
}).catch(fetchError => {
|
||||
logRequest(rres,rreq,500,fetchError)
|
||||
rres.sendStatus(500)
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
module.exports = {app} // export routes to be imported by index for execution
|
||||
@@ -1,31 +1,31 @@
|
||||
const { app, globalConfig, fs, globalVersion } = require("../index.js") // Get globals from index
|
||||
import { app, globalConfig, fs, globalVersion } from "../index.js" // Get globals from index
|
||||
|
||||
var timeSinceLastQuery = Date.now()-10000
|
||||
var cachedResult = ""
|
||||
|
||||
app.get("/static/*", (rreq,rres) => {
|
||||
rres.sendFile(globalConfig.frontpage.frontpageDir+"/static/"+rreq.url.replace("/static/",""))
|
||||
rres.sendFile(globalConfig.frontpage.directory+"static/"+rreq.url.replace("/static/",""))
|
||||
})
|
||||
|
||||
app.get("/posts/*", (rreq,rres) => {
|
||||
rres.sendFile(globalConfig.frontpage.frontpageDir+"/posts/"+rreq.url.replace("/posts/",""))
|
||||
rres.sendFile(globalConfig.frontpage.directory+"posts/"+rreq.url.replace("/posts/",""))
|
||||
})
|
||||
|
||||
app.get("/", (rreq, rres) => {
|
||||
if (Date.now() < timeSinceLastQuery+10000) {
|
||||
rres.send(cachedResult)
|
||||
} else {
|
||||
let indexFile = fs.readFileSync(globalConfig.frontpage.frontpageDir+"/index.html","utf-8")
|
||||
let indexFile = fs.readFileSync(globalConfig.frontpage.directory+"index.html","utf-8")
|
||||
cachedResult = indexFile.replace("<!--SSR_BLOGPOSTS-->",parseFiles()).replace("<!--SSR_APIVERSION-->",`<sup>API Version ${globalVersion}</sup>`)
|
||||
rres.send(cachedResult)
|
||||
}
|
||||
})
|
||||
|
||||
function parseFiles() {
|
||||
let files = fs.readdirSync(globalConfig.frontpage.frontpageDir+"/posts/")
|
||||
let files = fs.readdirSync(globalConfig.frontpage.directory+"posts/")
|
||||
let result = ""
|
||||
|
||||
for (x in files) {
|
||||
for (let x in files) {
|
||||
if (files[x].endsWith(".html") === false) { break } // If file/dir is not .html then ignore
|
||||
|
||||
let date = files[x].split("-")[0]
|
||||
@@ -41,4 +41,4 @@ function parseFiles() {
|
||||
return result
|
||||
}
|
||||
|
||||
module.exports = {app}
|
||||
export {app}
|
||||
@@ -1,4 +1,4 @@
|
||||
const { app } = require("../index.js")
|
||||
import { app } from "../index.js"
|
||||
|
||||
app.get("/api/ip", (rreq,rres) => {
|
||||
let jsonResponse = {
|
||||
@@ -6,7 +6,6 @@ app.get("/api/ip", (rreq,rres) => {
|
||||
"Country": rreq.get("cf-ipcountry") || "not_cloudflare",
|
||||
"CfRay": rreq.get("cf-ray") || "not_cloudflare"
|
||||
}
|
||||
|
||||
rres.send(jsonResponse)
|
||||
})
|
||||
|
||||
@@ -14,4 +13,4 @@ app.get("/api/headers", (rreq,rres) => {
|
||||
rres.send(rreq.headers)
|
||||
})
|
||||
|
||||
module.exports = {app}
|
||||
export { app }
|
||||
@@ -1,43 +0,0 @@
|
||||
const { app, globalConfig } = require("../index.js") // Get globals from index
|
||||
const { checkToken } = require("../liberals/auth.js")
|
||||
const { logRequest } = require("../liberals/logging.js")
|
||||
|
||||
app.post("/api/sendemail", (rreq,rres) => {
|
||||
checkToken(rreq.get("Authorization"),"mailjet").then(authRes => {
|
||||
if (authRes === false) {
|
||||
rres.sendStatus(401)
|
||||
} 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, }],
|
||||
"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", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Authorization": `Basic ${btoa(globalConfig.mailjet.apiKey)}`,
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify(message)
|
||||
}).then(fetchRes => {
|
||||
if (fetchRes.status == 200) {
|
||||
logRequest(rres,rreq,200)
|
||||
rres.sendStatus(200)
|
||||
} else {
|
||||
logRequest(rres,rreq,500,`Mailjet fetch did not return OK: ${fetchRes.status} ${fetchRes.statusText}`)
|
||||
rres.sendStatus(500)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
module.exports = {app}
|
||||
@@ -1,33 +0,0 @@
|
||||
const { app, globalConfig } = require("../index.js")
|
||||
const { queryLastfm } = require("../liberals/libnowplaying.js")
|
||||
|
||||
var timeSinceLastLastfmQuery = Date.now()-5000
|
||||
var cachedLastfmResult = {}
|
||||
|
||||
const notPlayingAnythingPlaceholder = {
|
||||
"json": {
|
||||
"playing": false
|
||||
},
|
||||
"html": `<span>I'm not currently listening to anything.</span>`
|
||||
}
|
||||
|
||||
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 == {}) {
|
||||
cachedLastfmResult = notPlayingAnythingPlaceholder
|
||||
} else {
|
||||
cachedLastfmResult = response
|
||||
}
|
||||
|
||||
rres.send(cachedLastfmResult[rreq.query.format] ?? cachedLastfmResult.json)
|
||||
})
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
module.exports = {app}
|
||||
Reference in New Issue
Block a user