the etyd refactor in question #3

Merged
Enstrayed merged 8 commits from etyd-refactor into main 2024-06-13 01:18:27 -07:00
Showing only changes of commit 98de002e14 - Show all commits

View File

@@ -31,24 +31,26 @@ app.post("/etydwrite", (rreq,rres) => {
switch(rreq.body.action) { switch(rreq.body.action) {
case "set": // Write to db case "set": // Write to db
if (rreq.body.random == true) { if (rreq.body.random == true) {
let workingTarget = makeRandomHex() let workingTarget = makeRandomHex() // Make a random URL
db.get(`/${workingTarget}`).then(dbres => { // Check if it exists
db.get(`/${workingTarget}`).then(dbres => { if (dbres != null) { // If it does
if (dbres != null) { let workingTarget = makeRandomHex() // Make a new one
let workingTarget = makeRandomHex() db.get(`/${workingTarget}`).then(dbres => { // Check if *that* exists
if (dbres != null) { // If it does
db.get(`/${workingTarget}`).then(dbres => { // Then everything is dumb and pointless so just give up
if (dbres != null) { console.log(`${rreq.get("cf-connecting-ip")} POST /etydwrite ACTION set returned 409 (Two attempts to find an open key failed)`)
// well fuck
rres.sendStatus(409) rres.sendStatus(409)
} else { // if it doesnt then set the stupid key I hate this code so much why did I do this serverside this is so dumb
console.log(`${rreq.get("cf-connecting-ip")} POST /etydwrite ACTION set returned 200 KEY:${rreq.get("Authorization")} TARGET: ${workingTarget}`)
db.set(`/${workingTarget}`,rreq.body.value)
rres.send(`https://etyd.cc/${workingTarget}`)
} }
}) })
} else { } else {
console.log(`${rreq.get("cf-connecting-ip")} POST /etydwrite ACTION set returned 200 KEY:${rreq.get("Authorization")} TARGET: ${workingTarget}`)
db.set(`/${workingTarget}`,rreq.body.value) db.set(`/${workingTarget}`,rreq.body.value)
rres.send(`https://etyd.cc/${workingTarget}`) rres.send(`https://etyd.cc/${workingTarget}`)
} }
@@ -71,12 +73,18 @@ app.post("/etydwrite", (rreq,rres) => {
case "delete": case "delete":
db.get(`/${rreq.body.target}`).then(dbres => { let workingTarget = rreq.body.target.replace("https://etyd.cc/","") // Sanitize input
if (workingTarget.startsWith("/")) {
workingTarget = workingTarget.slice(1)
}
db.get(`/${workingTarget}`).then(dbres => {
if (dbres == null) { //if key doesnt exist then log and return 400 if (dbres == null) { //if key doesnt exist then log and return 400
console.log(`${rreq.get("cf-connecting-ip")} POST /etydwrite ACTION delete returned 400 KEY:${rreq.get("Authorization")}`) console.log(`${rreq.get("cf-connecting-ip")} POST /etydwrite ACTION delete returned 404 KEY:${rreq.get("Authorization")} TARGET: ${workingTarget}`)
rres.sendStatus(400) rres.sendStatus(404)
} else { } else {
db.del(`/${rreq.body.target}`) console.log(`${rreq.get("cf-connecting-ip")} POST /etydwrite ACTION delete returned 200 KEY:${rreq.get("Authorization")} TARGET: ${workingTarget}`)
db.del(`/${workingTarget}`)
rres.sendStatus(200) rres.sendStatus(200)
} }
}) })
@@ -84,6 +92,7 @@ app.post("/etydwrite", (rreq,rres) => {
default: default:
console.log(`${rreq.get("cf-connecting-ip")} POST /etydwrite ACTION default returned 400 KEY:${rreq.get("Authorization")}`)
rres.sendStatus(400) // request json didnt include a valid action rres.sendStatus(400) // request json didnt include a valid action
break; break;
} }