Merge pull request #4 from Enstrayed/main

Merge changes into new auth branch
This commit was merged in pull request #4.
This commit is contained in:
Nathan Ritchie
2024-07-06 10:56:53 -07:00
committed by GitHub
12 changed files with 204 additions and 205 deletions

View File

@@ -2,11 +2,7 @@ const { app, globalConfig } = require("../index.js") // Get globals from index
const { checkToken } = require("../liberals/auth.js")
app.get("/etyd*", (rreq,rres) => {
fetch(`http://${globalConfig.couchdb.host}/etyd${rreq.path.replace("/etyd","")}`, {
headers: {
"Authorization": `Basic ${btoa(globalConfig.couchdb.authorization)}`
}
}).then(dbRes => {
fetch(`${globalConfig.couchdbHost}/etyd${rreq.path.replace("/etyd","")}`).then(dbRes => {
if (dbRes.status == 404) {
rres.sendStatus(404)
} else {
@@ -35,21 +31,16 @@ app.delete("/etyd*", (rreq,rres) => {
rres.sendStatus(401)
} else if (authRes === true) { // Authorization successful
fetch(`http://${globalConfig.couchdb.host}/etyd${rreq.path.replace("/etyd", "")}`, {
headers: {
"Authorization": `Basic ${btoa(globalConfig.couchdb.authorization)}`
}
}).then(dbRes => {
fetch(`${globalConfig.couchdbHost}/etyd${rreq.path.replace("/etyd", "")}`).then(dbRes => {
if (dbRes.status == 404) {
rres.sendStatus(404)
} else {
dbRes.json().then(dbRes => {
fetch(`http://${globalConfig.couchdb.host}/etyd${rreq.path.replace("/etyd", "")}`, {
fetch(`${globalConfig.couchdbHost}/etyd${rreq.path.replace("/etyd", "")}`, {
method: "DELETE",
headers: {
"Authorization": `Basic ${btoa(globalConfig.couchdb.authorization)}`,
"If-Match": dbRes["_rev"] // Using the If-Match header is easiest for deleting entries in couchdb
}
}).then(fetchRes => {
@@ -89,10 +80,7 @@ app.post("/etyd*", (rreq,rres) => {
if (rreq.body["url"] == undefined) {
rres.sendStatus(400)
} else {
fetch(`http://${globalConfig.couchdb.host}/etyd${rreq.path.replace("/etyd", "")}`, {
headers: {
"Authorization": `Basic ${btoa(globalConfig.couchdb.authorization)}`
},
fetch(`${globalConfig.couchdbHost}/etyd${rreq.path.replace("/etyd", "")}`, {
method: "PUT",
body: JSON.stringify({
"content": {

View File

@@ -1,11 +1,33 @@
const { app, globalConfig } = require("../index.js")
const { queryLastfm } = require("../liberals/nowplaying.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("/nowplaying", (rreq,rres) => {
if (rreq.query.format === "html") {
rres.send("<span>The /nowplaying endpoint is currently under construction.</span>")
if (Date.now() < timeSinceLastLastfmQuery+5000) {
rres.send(cachedLastfmResult[rreq.query.format] ?? cachedLastfmResult.json)
} else {
rres.send({"message":"The /nowplaying endpoint is currently under construction."})
timeSinceLastLastfmQuery = Date.now()
queryLastfm().then(response => {
if (response == 1) {
cachedLastfmResult = notPlayingAnythingPlaceholder
} else {
cachedLastfmResult = response
}
rres.send(cachedLastfmResult[rreq.query.format] ?? cachedLastfmResult.json)
})
}
})
module.exports = {app}