Skip to content

Commit

Permalink
Feature/submit url management (#8)
Browse files Browse the repository at this point in the history
* Get new submissions

* Raise state

* Custom In clause

* Retrieve new submissions and bump state

* Retrieve id of submission

* Added Cors-response headers

* Added headers for success response

* Changed response to json

* Remove query from source

* Remove branch from deplpy

* lockdown endpoint

* Deploy on branch

* Remove logging

---------

Co-authored-by: Jon Breen <[email protected]>
  • Loading branch information
cultpodcasts and cultpodcasts authored Mar 6, 2024
1 parent 1241f22 commit 332b8cb
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 26 deletions.
1 change: 1 addition & 0 deletions .github/workflows/buildAndDeploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
push:
branches:
- main
- feature/submit-url-management
jobs:
deploy:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules/
podcasts.sqlite
podcasts.sql
/wrangler.toml
/queries.cmd
93 changes: 67 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface Env {
DB: D1Database;
apikey: string;
apihost: string;
gatewayKey: string;
}

export default {
Expand All @@ -27,7 +28,7 @@ export default {
});
}

if (pathname.startsWith(homeRoute) && request.method==="GET") {
if (pathname.startsWith(homeRoute) && request.method === "GET") {
const object = await env.Content.get("homepage");

if (object === null) {
Expand All @@ -44,9 +45,8 @@ export default {
return new Response(object.body, { headers });
}

if (pathname.startsWith(searchRoute) && request.method==="POST") {
if (pathname.startsWith(searchRoute) && request.method === "POST") {
const url = `${env.apihost}`;

return request
.json()
.then(async (data: any) => {
Expand Down Expand Up @@ -91,7 +91,6 @@ export default {
headers.append("Content-Type", "application/json");
headers.append("Access-Control-Allow-Origin", "*");
headers.append("Access-Control-Allow-Methods", "POST,GET,OPTIONS");
headers.append("x-flow", "2");

if (response.status != 200) {
return new Response(response.body, { headers, status: response.status })
Expand Down Expand Up @@ -119,31 +118,73 @@ export default {
});
}

if (pathname.startsWith(submitRoute) && request.method === "POST") {
return request
.json()
.then(async (data: any) => {
let url: URL | undefined;
let urlParam = data.url;
if (urlParam == null) {
return new Response("Missing url param.", { status: 400 });
}
try {
url = new URL(urlParam);
} catch {
return new Response(`Invalid url '${data.url}'.`, { status: 400 });
}
let insert = env.DB
.prepare("INSERT INTO urls (url, timestamp, timestamp_date, ip_address, country, user_agent) VALUES (?, ?, ?, ?, ?, ?)")
.bind(url.toString(), Date.now(), new Date().toLocaleString(), request.headers.get("CF-Connecting-IP"), request.headers.get("CF-IPCountry"), request.headers.get("User-Agent"));
let result = await insert.run();
if (pathname.startsWith(submitRoute)) {
if (request.method === "POST") {

const headers = new Headers();
headers.set("Cache-Control", "max-age=600");
headers.append("Content-Type", "application/json");
headers.append("Access-Control-Allow-Origin", "*");
headers.append("Access-Control-Allow-Methods", "POST,GET,OPTIONS");

return request
.json()
.then(async (data: any) => {
let url: URL | undefined;
let urlParam = data.url;
if (urlParam == null) {
return new Response(JSON.stringify({ error: "Missing url param."}), { headers, status: 400 });
}
try {
url = new URL(urlParam);
} catch {
return new Response(JSON.stringify({error: `Invalid url '${data.url}'.`}), { headers, status: 400 });
}
let insert = env.DB
.prepare("INSERT INTO urls (url, timestamp, timestamp_date, ip_address, country, user_agent) VALUES (?, ?, ?, ?, ?, ?)")
.bind(url.toString(), Date.now(), new Date().toLocaleString(), request.headers.get("CF-Connecting-IP"), request.headers.get("CF-IPCountry"), request.headers.get("User-Agent"));
let result = await insert.run();

if (result.success) {
return new Response();
if (result.success) {
return new Response(JSON.stringify({ success: "Submitted" }), { headers });
} else {
return new Response(JSON.stringify({ error: "Unable to accept" }), { headers, status: 400 });
}
});
} else if (request.method === "GET" && request.headers.get("key") === env.gatewayKey) {
let submissionIds = env.DB
.prepare("SELECT id FROM urls WHERE state=0");
let result = await submissionIds.all();
if (result.success) {
const inClause = result.results
.map((urlId) => {
if (!Number.isInteger(urlId.id)) { throw Error("invalid id, expected an integer"); }
return urlId.id;
})
.join(',');
let urls = "SELECT id, url, timestamp_date, ip_address, country, user_agent FROM urls WHERE id IN ($urlIds)";
urls = urls.replace('$urlIds', inClause);
let urlResults = await env.DB
.prepare(urls)
.run();
if (urlResults.success) {
let update = "UPDATE urls SET state=1 WHERE id IN ($urlIds)";
update = update.replace('$urlIds', inClause);
let raiseState = await env.DB
.prepare(update)
.run();
if (raiseState.success) {
return new Response(JSON.stringify(urlResults.results));
} else {
return new Response("Failure to raise state of new submissons in ids " + result.results.join(", "), { status: 400 })
}
} else {
return new Response("Unable to accept", { status: 400 });
return new Response("Unable to retrieve new submissions", { status: 500 });
}
});
} else {
return new Response(result.error, { status: 500 });
}
}
}

return new Response(
Expand Down

0 comments on commit 332b8cb

Please sign in to comment.