Skip to content

Commit

Permalink
Implement a routine deletion and re-registration process for geoCML i…
Browse files Browse the repository at this point in the history
…nstances on DRGON
  • Loading branch information
TristanDamron committed Nov 28, 2024
1 parent e2f64f6 commit 697b690
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 31 deletions.
57 changes: 30 additions & 27 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { db } from "./db.js"
import { checkForBannedWords, email, key, orderBy, searchByTag, url, sanitizeString } from "./utils.js"
import { checkForBannedWords, email, key, orderBy, searchByTag, url, sanitizeString, queueForRemoval, wipeDB } from "./utils.js"
import express from "express"
import { generateApiKey } from "generate-api-key"

Expand Down Expand Up @@ -50,6 +50,8 @@ app.post("/registry", async (req, res) => {
}, 400)
return
}

await queueForRemoval(urlVal)
res.json({
"message": "Done.",
})
Expand Down Expand Up @@ -83,33 +85,34 @@ app.get("/apikey", async (req, res) => {
})

app.get("/recommendations", async (req, res) => {
res.header("Access-Control-Allow-Origin", "*")
const tagsVal = sanitizeString(checkForBannedWords(req.query, "tags"))
const limit = parseInt(sanitizeString(checkForBannedWords(req.query, "limit")))

if (limit === NaN || tagsVal === "") {
res.json({
"message": "Invalid request body",
}, 400)
return
}

let deployments = []
let deploymentIds = []
for (const tag of tagsVal.split(",")) {
const deployment = await db.manyOrNone(`SELECT * FROM registry WHERE tags LIKE '%${tag}%';`)
if (deployment.length > 0 && !deploymentIds.includes(deployment[0].id)) {
deployments.push(deployment)
deploymentIds.push(deployment[0].id)
}
}

res.json({
"message": "Done.",
"deployments": deployments.slice(0, limit),
})
res.header("Access-Control-Allow-Origin", "*")
const tagsVal = sanitizeString(checkForBannedWords(req.query, "tags"))
const limit = parseInt(sanitizeString(checkForBannedWords(req.query, "limit")))

if (limit === NaN || tagsVal === "") {
res.json({
"message": "Invalid request body",
}, 400)
return
}

let deployments = []
let deploymentIds = []
for (const tag of tagsVal.split(",")) {
const deployment = await db.manyOrNone(`SELECT * FROM registry WHERE tags LIKE '%${tag}%';`)
if (deployment.length > 0 && !deploymentIds.includes(deployment[0].id)) {
deployments.push(deployment)
deploymentIds.push(deployment[0].id)
}
}

res.json({
"message": "Done.",
"deployments": deployments.slice(0, limit),
})
})

app.listen(port, () => {
app.listen(port, async () => {
await wipeDB()
console.log(`DRGON is listening on port ${port}`)
})
25 changes: 21 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { db } from "./db.js"
const validColumns = ["id", "url", "description", "owner", "tags"]

export function sanitizeString(str) {
str = str.replaceAll("'", "")
str = str.replaceAll("\"", "")
return str
str = str.replaceAll("'", "")
str = str.replaceAll("\"", "")
return str
}

export function orderBy(body) {
Expand Down Expand Up @@ -58,4 +58,21 @@ export function email(body) {
if (!body.hasOwnProperty("email")) return ""
if (body["email"].match(/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/)) return body["email"]
return ""
}
}

export async function queueForRemoval(deployment) {
if (await db.oneOrNone(`SELECT count(*) FROM registry WHERE url = '${deployment}';`) === null)
return

setTimeout(async () => {
try {
await db.none(`DELETE FROM registry WHERE url = '${deployment}';`)
} catch (err) { // There is a pending transaction in PSQL, try again...
queueForRemoval(deployment)
}
}, 60 * 1000, deployment)
}

export async function wipeDB() {
await db.none("DELETE FROM registry;")
}

0 comments on commit 697b690

Please sign in to comment.