From e908fdbbbb294384afaf650c1a39679c9f3b9484 Mon Sep 17 00:00:00 2001 From: dawn <90008@gaze.systems> Date: Fri, 6 Mar 2026 13:12:35 +0300 Subject: [PATCH] add app for serving on wisp place --- .gitignore | 1 + build.ts | 3 ++ deno.json | 3 +- main.ts | 22 ++++++++++++--- redirect.html | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 build.ts create mode 100644 redirect.html diff --git a/.gitignore b/.gitignore index b8bfd58..29dfa27 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ random-wisp-place.kv* /.envrc /.direnv hydrant.db +redirect.out.html diff --git a/build.ts b/build.ts new file mode 100644 index 0000000..c4446be --- /dev/null +++ b/build.ts @@ -0,0 +1,3 @@ +const html = await Deno.readTextFile("redirect.html"); +const url = Deno.env.get("API_URL") ?? "/"; +await Deno.writeTextFile("redirect.out.html", html.replaceAll("__API_URL__", url)); diff --git a/deno.json b/deno.json index 0704e0c..ede2960 100644 --- a/deno.json +++ b/deno.json @@ -3,6 +3,7 @@ "version": "0.1.0", "tasks": { "start": "deno run -A --unstable-kv main.ts", - "dev": "deno run -A --unstable-kv --watch main.ts" + "dev": "deno run -A --unstable-kv --watch main.ts", + "build": "deno run -A build.ts" } } diff --git a/main.ts b/main.ts index 015dcb8..909a018 100644 --- a/main.ts +++ b/main.ts @@ -203,21 +203,35 @@ const pickRandomReachable = async (sites: SiteValue[]): Promise { + if (req.method === "OPTIONS") { + return new Response(null, { status: 204, ...corsHeaders }); + } + const { pathname } = new URL(req.url); if (pathname === "/health") { const sites = await allSites(); - return Response.json({ + const data = { total: sites.length, withDomain: sites.filter((s) => s.domainUrl).length, - }); + }; + return Response.json(data, corsHeaders); } const site = await pickRandomReachable(await allSites()); return site - ? new Response(JSON.stringify(site)) - : new Response("no sites discovered yet, try again later", { status: 503 }); + ? Response.json(site, corsHeaders) + : new Response( + "no sites discovered yet, try again later", + { status: 503, ...corsHeaders }, + ); }); console.log(`[?] listening on :${PORT}`); diff --git a/redirect.html b/redirect.html new file mode 100644 index 0000000..ec2cf15 --- /dev/null +++ b/redirect.html @@ -0,0 +1,78 @@ + + + + + redirecting... + + + +

picking a random site for you (^w^)

+ +
+ + + -- 2.51.2