cohost/serverside/randirect.ts

23 lines
786 B
TypeScript
Raw Normal View History

2023-09-24 07:02:33 -04:00
// i really need to learn deno emit (or host this publicly so i can reference it with urls)
import { randomInt } from "https://deno.land/std@0.146.0/node/internal/crypto/random.ts";
export const pick_random = <T>(xs: readonly T[]): T => xs[randomInt(xs.length)];
Deno.serve({ port: 61262 }, (req: Request) => {
const url = new URL(req.url);
const links = url.search.replace(/^\?+/, "")
.split("::")
.filter(Boolean)
.map((href) => /^.+:\/\/.+/.test(href) ? href : ("http://" + href));
if (links.length == 0) {
return new Response("couldn't get any links man", { status: 400 });
}
console.log(links);
const chosen = pick_random(links);
return new Response(`redirecting you to ${chosen}`, {
status: 307,
headers: { "Location": chosen },
});
});