Skip to content

Commit

Permalink
Added Cors-response headers
Browse files Browse the repository at this point in the history
  • Loading branch information
cultpodcasts committed Feb 20, 2024
1 parent 86a9c28 commit 26d02b9
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 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) && request.method==="GET") {
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) && request.method==="POST") {
if (pathname.startsWith(searchRoute) && request.method === "POST") {
const url = `${env.apihost}`;

return request
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 @@ -121,18 +120,25 @@ export default {

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("Missing url param.", { status: 400 });
return new Response("Missing url param.", { headers, status: 400 });
}
try {
url = new URL(urlParam);
} catch {
return new Response(`Invalid url '${data.url}'.`, { status: 400 });
return new Response(`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 (?, ?, ?, ?, ?, ?)")
Expand All @@ -142,11 +148,11 @@ export default {
if (result.success) {
return new Response();
} else {
return new Response("Unable to accept", { status: 400 });
return new Response("Unable to accept", { headers, status: 400 });
}
});
} else if (request.method === "GET") {
let submissionIds = env.DB
let submissionIds = env.DB
.prepare("SELECT id FROM urls WHERE state=0");
let result = await submissionIds.all();
if (result.success) {
Expand Down Expand Up @@ -181,8 +187,6 @@ export default {
}
}



return new Response(
`Call ${homeRoute} to get the last 7-days of new releases
${pathname}`,
Expand Down

0 comments on commit 26d02b9

Please sign in to comment.