Skip to content

Commit

Permalink
Public API: CORS.
Browse files Browse the repository at this point in the history
  • Loading branch information
e3ndr committed Sep 21, 2023
1 parent 9e7290e commit 45f0fc5
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions public-api/src/main.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
import * as env from "./env.mjs";

function handleCORS(request, response) {
response.headers.set("Access-Control-Allow-Origin", "*");
response.headers.set("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS");
response.headers.set("Access-Control-Max-Age", "86400");
response.headers.set("Access-Control-Allow-Headers", request.headers.get("Access-Control-Request-Headers"));
}

export default {
fetch: async function (request, _env) {
let response;

try {
console.log("Incoming request!");
env.configure(_env);

console.log("Handling request...");
if (request.method == "OPTIONS") {
console.log("Handling options!");
response = new Response(null);
} else {
console.log("Handling request!");
env.configure(_env);

const router = (await import("./routes/index.mjs")).default;
response = await router.handle(request, env);
}

const router = (await import("./routes/index.mjs")).default;
return await router.handle(request, env);
} catch (e) {
console.error("Internal error:", e.toString(), e.stack);
console.error(e);
return new Response("Internal Error.");
}

handleCORS(request, response);

return response;
},
};

0 comments on commit 45f0fc5

Please sign in to comment.