Skip to content

Commit

Permalink
Feature/add podcast capture (#7)
Browse files Browse the repository at this point in the history
* Removed previous database and refer to new submission database

* Added url submission

* Fixed search route

* Use json post body

* Using correct table name

* Fixed sql

* Fixed responses

* Fixed error response

* Remove deploy for branch

---------

Co-authored-by: Jon Breen <[email protected]>
  • Loading branch information
cultpodcasts and cultpodcasts committed Feb 17, 2024
1 parent e81d97c commit 1241f22
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
1 change: 0 additions & 1 deletion create-sql-dump.cmd

This file was deleted.

11 changes: 0 additions & 11 deletions migrations/0000_full-text-search.sql

This file was deleted.

33 changes: 31 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface Env {
Content: R2Bucket;
Analytics: AnalyticsEngineDataset;
DB: D1Database;
apikey: string;
apihost: string;
}
Expand All @@ -10,6 +11,7 @@ export default {
const { pathname, searchParams } = new URL(request.url);
const homeRoute = "/homepage";
const searchRoute = "/api";
const submitRoute = "/submit";
const corsHeaders = {
"Access-Control-Allow-Origin": "*", //"https://cultpodcasts.com",
"Access-Control-Allow-Methods": "GET,HEAD,POST,OPTIONS",
Expand All @@ -25,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 @@ -42,7 +44,7 @@ export default {
return new Response(object.body, { headers });
}

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

return request
Expand Down Expand Up @@ -117,6 +119,33 @@ 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 (result.success) {
return new Response();
} else {
return new Response("Unable to accept", { status: 400 });
}
});
}

return new Response(
`Call ${homeRoute} to get the last 7-days of new releases
${pathname}`,
Expand Down
4 changes: 2 additions & 2 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ bucket_name = "content"

[[d1_databases]]
binding = "DB"
database_name = "cultpodcasts"
database_id = "d60db6d1-994d-448d-9be1-5067d1be72d8"
database_name = "submissions"
database_id = "6d00eb71-b420-47f7-9e74-96d53bcb943a"

[[analytics_engine_datasets]]
binding = "Analytics"

0 comments on commit 1241f22

Please sign in to comment.