quick fix for couchdb

This commit is contained in:
Enstrayed
2024-07-06 10:52:00 -07:00
parent 6ea66c0407
commit 49f9927ab9
3 changed files with 6 additions and 22 deletions

View File

@@ -2,7 +2,7 @@
This repository contains the code for my personal web API written in JavaScript using the Express framework. This repository contains the code for my personal web API written in JavaScript using the Express framework.
## Documentation ## Documentation
This file contains documentation relevant for development and deployment, but not necessarily usage. Information for all endpoints is available [on my website](https://music.youtube.com/watch?v=n2LxBXS4jJM&si=ZpzGNBGvQp1cFicW). This file contains documentation relevant for development and deployment, but not necessarily usage. Information for all endpoints is available [on my website](https://enstrayed.com/posts/20240409-API-Documentation.html).
## Issues ## Issues
If you would like to report a bug or security issue, please open a GitHub issue. If you are the operator of a service this application accesses, use the contact information provided during registration with your service to contact me directly. If you would like to report a bug or security issue, please open a GitHub issue. If you are the operator of a service this application accesses, use the contact information provided during registration with your service to contact me directly.

View File

@@ -1,11 +1,7 @@
const { globalConfig } = require("../index.js") const { globalConfig } = require("../index.js")
async function checkAuthorization(documentToUse,keyToCheck) { async function checkAuthorization(documentToUse,keyToCheck) {
return await fetch(`http://${globalConfig.couchdb.host}/apiauthkeys/${documentToUse}`, { return await fetch(`${globalConfig.couchdbHost}/apiauthkeys/${documentToUse}`).then(fetchRes => {
headers: {
"Authorization": `Basic ${btoa(globalConfig.couchdb.authorization)}`
}
}).then(fetchRes => {
if (fetchRes.status === 404) { // If document doesnt exist fail gracefully if (fetchRes.status === 404) { // If document doesnt exist fail gracefully

View File

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