Skip to content

Commit

Permalink
feat: add cors proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
kawamataryo committed Jan 25, 2025
1 parent 4411032 commit 250ac9b
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions server/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,42 @@ app.get("/og", async (c) => {
app.get("/lp", (c) => {
return generateLandingHtml(c);
});

// CORS Proxy endpoint
app.on(["GET", "OPTIONS"], "/proxy", async (c) => {
// Handle preflight request
if (c.req.method === "OPTIONS") {
return new Response(null, {
status: 204,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
}
});
}

const targetUrl = c.req.query("url");
if (!targetUrl) {
return c.text("Missing URL parameter", 400);
}

try {
const response = await fetch(targetUrl);
const data = await response.text();

return new Response(data, {
status: response.status,
headers: {
"Content-Type": response.headers.get("Content-Type") || "text/plain",
"Access-Control-Allow-Origin": "*",
"Cache-Control": "public, max-age=300"
}
});
} catch (error) {
console.error("Proxy error:", error);
return c.text("Failed to fetch target URL", 500);
}
});

export default app;

0 comments on commit 250ac9b

Please sign in to comment.