actually write the handling for saving posts
This commit is contained in:
@@ -62,7 +62,6 @@
|
|||||||
<p>
|
<p>
|
||||||
400: Bad Request - You will see this if you try and delete a non-existent URL<br>
|
400: Bad Request - You will see this if you try and delete a non-existent URL<br>
|
||||||
401: Unauthorized - Did you enter your API key?<br>
|
401: Unauthorized - Did you enter your API key?<br>
|
||||||
405: Method Not Allowed - You will see this if you try a request with no arguments<br>
|
|
||||||
409: Conflict - The entered URL already exists, tick 'Random' and try again<br>
|
409: Conflict - The entered URL already exists, tick 'Random' and try again<br>
|
||||||
500: Internal Server Error - If this happens something has gone very wrong<br>
|
500: Internal Server Error - If this happens something has gone very wrong<br>
|
||||||
502: Bad Gateway - If you see this the backend is down/unreachable by Caddy<br>
|
502: Bad Gateway - If you see this the backend is down/unreachable by Caddy<br>
|
||||||
|
|||||||
195
routes/etyd.js
195
routes/etyd.js
@@ -1,4 +1,5 @@
|
|||||||
const { app, db, globalConfig } = require("../index.js") // Get globals from index
|
const { app, globalConfig } = require("../index.js") // Get globals from index
|
||||||
|
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(`http://${globalConfig.couchdb.host}/etyd${rreq.path.replace("/etyd","")}`, {
|
||||||
@@ -25,119 +26,111 @@ app.get("/etyd*", (rreq,rres) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
app.delete("/etyd*", (rreq,rres) => {
|
app.delete("/etyd*", (rreq,rres) => {
|
||||||
|
|
||||||
fetch(`http://${globalConfig.couchdb.host}/apiauthkeys/${globalConfig.etyd.authKeysDoc}`, {
|
|
||||||
headers: {
|
|
||||||
"Authorization": `Basic ${btoa(globalConfig.couchdb.authorization)}`
|
|
||||||
}
|
|
||||||
}).then(dbRes => dbRes.json()).then(dbRes => {
|
|
||||||
|
|
||||||
if (dbRes.status == 404) { // If document containing cider auth keys does not exist
|
if (rreq.get("Authorization") === undefined) {
|
||||||
console.log(`ERROR: Could not find apiauthkeys/${globalConfig.etyd.authKeysDoc}`)
|
rres.sendStatus(400)
|
||||||
rres.sendStatus(500) // Refuse request
|
} else {
|
||||||
} else {
|
checkAuthorization(globalConfig.etyd.authKeysDoc,rreq.get("Authorization")).then(authRes => {
|
||||||
if (rreq.get("Authorization") == null) { // If authorization header is not supplied
|
if (authRes === false) {
|
||||||
rres.sendStatus(400) // then return bad request (would return 500 otherwise)
|
console.log(`${rreq.get("cf-connecting-ip")} DELETE ${rreq.path} returned 401`) // Log unauthorized requests
|
||||||
} else {
|
rres.sendStatus(401)
|
||||||
if (dbRes["content"][rreq.get("Authorization").split("_")[0]] === rreq.get("Authorization").split("_")[1]) {
|
} else if (authRes === true) { // Authorization successful
|
||||||
|
|
||||||
fetch(`http://${globalConfig.couchdb.host}/etyd${rreq.path.replace("/etyd", "")}`, {
|
fetch(`http://${globalConfig.couchdb.host}/etyd${rreq.path.replace("/etyd", "")}`, {
|
||||||
headers: {
|
headers: {
|
||||||
"Authorization": `Basic ${btoa(globalConfig.couchdb.authorization)}`
|
"Authorization": `Basic ${btoa(globalConfig.couchdb.authorization)}`
|
||||||
}
|
}
|
||||||
}).then(dbRes => {
|
}).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(`http://${globalConfig.couchdb.host}/etyd${rreq.path.replace("/etyd", "")}`, {
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
headers: {
|
headers: {
|
||||||
"Authorization": `Basic ${btoa(globalConfig.couchdb.authorization)}`,
|
"Authorization": `Basic ${btoa(globalConfig.couchdb.authorization)}`,
|
||||||
"If-Match": dbRes["_rev"]
|
"If-Match": dbRes["_rev"] // Using the If-Match header is easiest for deleting entries in couchdb
|
||||||
}
|
}
|
||||||
}).then(fetchRes => {
|
}).then(fetchRes => {
|
||||||
if (fetchRes.status == 200) {
|
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")}`)
|
||||||
rres.sendStatus(200)
|
rres.sendStatus(200)
|
||||||
}
|
}
|
||||||
}).catch(fetchError => {
|
}).catch(fetchError => {
|
||||||
rres.sendStatus(500)
|
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}`)
|
rres.sendStatus(500)
|
||||||
})
|
|
||||||
|
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
})
|
||||||
}).catch(fetchError => {
|
}
|
||||||
rres.sendStatus(500)
|
|
||||||
console.log(`${rres.get("cf-connecting-ip")} DELETE ${rreq.path} returned 500: ${fetchError}`)
|
}).catch(fetchError => {
|
||||||
})
|
console.log(`${rres.get("cf-connecting-ip")} DELETE ${rreq.path} returned 500: ${fetchError}`)
|
||||||
|
rres.sendStatus(500)
|
||||||
} else {
|
})
|
||||||
console.log(`${rreq.get("cf-connecting-ip")} DELETE ${rreq.path} returned 401`) // log ip of unauthorized requests
|
|
||||||
rres.sendStatus(401) // received auth key was not in database
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
}).catch(fetchError => {
|
}
|
||||||
rres.sendStatus(500)
|
|
||||||
console.log(`${rres.get("cf-connecting-ip")} DELETE ${rreq.path} returned 500: ${fetchError}`)
|
|
||||||
})
|
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// app.post("/etyd*", (rreq,rres) => {
|
app.post("/etyd*", (rreq,rres) => {
|
||||||
|
|
||||||
|
if (rreq.get("Authorization") === undefined) {
|
||||||
|
rres.sendStatus(400)
|
||||||
|
} else {
|
||||||
|
checkAuthorization(globalConfig.etyd.authKeysDoc,rreq.get("Authorization")).then(authRes => {
|
||||||
|
if (authRes === false) {
|
||||||
|
console.log(`${rreq.get("cf-connecting-ip")} POST ${rreq.path} returned 401`) // Log unauthorized requests
|
||||||
|
rres.sendStatus(401)
|
||||||
|
} else if (authRes === true) { // Authorization successful
|
||||||
|
|
||||||
|
if (rreq.body["url"] == undefined) {
|
||||||
|
console.log(`${rreq.get("cf-connecting-ip")} POST ${rreq.path} returned 400 KEY: ${rreq.get("Authorization")}`)
|
||||||
|
rres.sendStatus(400)
|
||||||
|
} else {
|
||||||
|
fetch(`http://${globalConfig.couchdb.host}/etyd${rreq.path.replace("/etyd", "")}`, {
|
||||||
|
headers: {
|
||||||
|
"Authorization": `Basic ${btoa(globalConfig.couchdb.authorization)}`
|
||||||
|
},
|
||||||
|
method: "PUT",
|
||||||
|
body: JSON.stringify({
|
||||||
|
"content": {
|
||||||
|
"url": rreq.body["url"]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}).then(dbRes => {
|
||||||
|
|
||||||
// fetch(`http://${globalConfig.couchdb.host}/apiauthkeys/${globalConfig.etyd.authKeysDoc}`, {
|
switch(dbRes.status) {
|
||||||
// headers: {
|
case 409:
|
||||||
// "Authorization": `Basic ${btoa(globalConfig.couchdb.authorization)}`
|
console.log(`${rreq.get("cf-connecting-ip")} POST ${rreq.path} returned 409 KEY: ${rreq.get("Authorization")}`)
|
||||||
// }
|
rres.sendStatus(409)
|
||||||
// }).then(dbRes => dbRes.json()).then(dbRes => {
|
break;
|
||||||
|
|
||||||
// if (dbRes.status == 404) { // If document containing cider auth keys does not exist
|
case 201:
|
||||||
// console.log(`ERROR: Could not find apiauthkeys/${globalConfig.etyd.authKeysDoc}`)
|
console.log(`${rreq.get("cf-connecting-ip")} POST ${rreq.path} returned 200 KEY: ${rreq.get("Authorization")}`)
|
||||||
// rres.sendStatus(500) // Refuse request
|
rres.status(200).send(rreq.path.replace("/etyd", ""))
|
||||||
// } else {
|
break;
|
||||||
// if (rreq.get("Authorization") == null) { // If authorization header is not supplied
|
|
||||||
// rres.sendStatus(400) // then return bad request (would return 500 otherwise)
|
|
||||||
// } else {
|
|
||||||
// if (dbRes["content"][rreq.get("Authorization").split("_")[0]] === rreq.get("Authorization").split("_")[1]) {
|
|
||||||
|
|
||||||
// fetch(`http://${globalConfig.couchdb.host}/etyd${rreq.path.replace("/etyd", "")}`, {
|
default:
|
||||||
// headers: {
|
console.log(`ERROR: CouchDB PUT did not return expected code: ${dbRes.status}`)
|
||||||
// "Authorization": `Basic ${btoa(globalConfig.couchdb.authorization)}`
|
break;
|
||||||
// }
|
}
|
||||||
// }).then(dbRes => {
|
|
||||||
|
|
||||||
// if (dbRes.status !== 404) {
|
}).catch(fetchError => {
|
||||||
// console.log(`${rres.get("cf-connecting-ip")} POST ${rreq.path} returned 409 KEY: ${rreq.get("Authorization")}`)
|
console.log(`${rres.get("cf-connecting-ip")} DELETE ${rreq.path} returned 500: ${fetchError}`)
|
||||||
// rres.sendStatus(409)
|
rres.sendStatus(500)
|
||||||
// } else {
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// }
|
})
|
||||||
|
|
||||||
// }).catch(fetchError => {
|
|
||||||
// rres.sendStatus(500)
|
|
||||||
// console.log(`${rres.get("cf-connecting-ip")} DELETE ${rreq.path} returned 500: ${fetchError}`)
|
|
||||||
// })
|
|
||||||
|
|
||||||
// } else {
|
|
||||||
// console.log(`${rreq.get("cf-connecting-ip")} DELETE ${rreq.path} returned 401`) // log ip of unauthorized requests
|
|
||||||
// rres.sendStatus(401) // received auth key was not in database
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }).catch(fetchError => {
|
|
||||||
// rres.sendStatus(500)
|
|
||||||
// console.log(`${rres.get("cf-connecting-ip")} DELETE ${rreq.path} returned 500: ${fetchError}`)
|
|
||||||
// })
|
|
||||||
|
|
||||||
// })
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = {app} // export routes to be imported by index for execution
|
module.exports = {app} // export routes to be imported by index for execution
|
||||||
@@ -7,7 +7,7 @@ app.post("/sendemail", (rreq,rres) => {
|
|||||||
if (authRes === false) { // If the supplied authorization is invalid or an error occured
|
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
|
console.log(`${rreq.get("cf-connecting-ip")} POST /sendemail returned 401`) // Log the request
|
||||||
rres.sendStatus(401) // Return 401 Unauthorized
|
rres.sendStatus(401)
|
||||||
|
|
||||||
} else if (authRes === true) { // If the authorization was valid, continue function
|
} else if (authRes === true) { // If the authorization was valid, continue function
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user