Skip to content

Commit

Permalink
Added url submission
Browse files Browse the repository at this point in the history
  • Loading branch information
cultpodcasts committed Feb 17, 2024
1 parent 3819df4 commit 46a5703
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 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/add-podcast-capture
jobs:
deploy:
runs-on: ubuntu-latest
Expand Down
26 changes: 23 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default {
});
}

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

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

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

return request
Expand Down Expand Up @@ -119,7 +119,27 @@ export default {
});
}

if (pathname.startsWith(submitRoute)) {
if (pathname.startsWith(submitRoute) && request.method === "POST") {
let url: URL | undefined;
let urlParam = searchParams.get("url");
if (urlParam == null) {
return new Error("Missing url param.");
}
try {
url = new URL(urlParam);
} catch {
return new Error(`Invalid url '${url}'.`);
}
let insert = env.DB
.prepare("INSERT INTO submissions (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-Connecting-IP"), request.headers.get("User-Agent"));
let result = await insert.run();

if (result.success) {
return new Response();
} else {
return new Error();
}
}

return new Response(
Expand Down

0 comments on commit 46a5703

Please sign in to comment.