···33 "version": "0.1.0",
44 "tasks": {
55 "start": "deno run -A --unstable-kv main.ts",
66- "dev": "deno run -A --unstable-kv --watch main.ts"
66+ "dev": "deno run -A --unstable-kv --watch main.ts",
77+ "build": "deno run -A build.ts"
78 }
89}
+18-4
main.ts
···203203 return null;
204204};
205205206206+const corsHeaders = {
207207+ headers: {
208208+ "Access-Control-Allow-Origin": "*",
209209+ "Access-Control-Allow-Methods": "GET",
210210+ }
211211+};
206212Deno.serve({ port: PORT }, async (req) => {
213213+ if (req.method === "OPTIONS") {
214214+ return new Response(null, { status: 204, ...corsHeaders });
215215+ }
216216+207217 const { pathname } = new URL(req.url);
208218209219 if (pathname === "/health") {
210220 const sites = await allSites();
211211- return Response.json({
221221+ const data = {
212222 total: sites.length,
213223 withDomain: sites.filter((s) => s.domainUrl).length,
214214- });
224224+ };
225225+ return Response.json(data, corsHeaders);
215226 }
216227217228 const site = await pickRandomReachable(await allSites());
218229 return site
219219- ? new Response(JSON.stringify(site))
220220- : new Response("no sites discovered yet, try again later", { status: 503 });
230230+ ? Response.json(site, corsHeaders)
231231+ : new Response(
232232+ "no sites discovered yet, try again later",
233233+ { status: 503, ...corsHeaders },
234234+ );
221235});
222236console.log(`[?] listening on :${PORT}`);
223237