diff --git a/public-api/src/main.mjs b/public-api/src/main.mjs index da2cbf9..16b4919 100644 --- a/public-api/src/main.mjs +++ b/public-api/src/main.mjs @@ -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; }, };