import { getRandomEmoji } from "./utils.ts"; type Route = { url: URLPattern; execute: (req: Request, match?: URLPatternResult) => Promise; }; const routes: Route[] = []; const thingRoute: Route = { url: new URLPattern({ pathname: "/thing/:id" }), execute: async (req: Request, match?: URLPatternResult) => { const id = match?.pathname.groups.id; console.log("Method:", req.method); console.log("Headers:", req.headers); const url = new URL(req.url); console.log("Path:", url.pathname); console.log("Query parameters:", url.searchParams); if (req.body) { const body = await req.text(); console.log("Body:", body); } if (!id) { const responseData = JSON.stringify({}); return new Response(responseData, { status: 400, headers: { "content-type": "application/json; charset=utf-8", }, }); } const responseBody = JSON.stringify({ from: `/thing/${id}`, emoji: getRandomEmoji(), }); return new Response(responseBody, { status: 200, headers: { "content-type": "application/json; charset=utf-8", }, }); }, }; const homeRoute: Route = { url: new URLPattern({ pathname: "/" }), execute: async (req: Request) => { console.log("Method:", req.method); console.log("Headers:", req.headers); const url = new URL(req.url); console.log("Path:", url.pathname); console.log("Query parameters:", url.searchParams); if (req.body) { const body = await req.text(); console.log("Body:", body); } const responseBody = JSON.stringify({ from: "/", emoji: getRandomEmoji(), }); return new Response(responseBody, { status: 200, headers: { "content-type": "application/json; charset=utf-8", }, }); }, }; routes.push(thingRoute); async function handler(req: Request): Promise { for (const route of routes) { const match = route.url.exec(req.url); console.log(match ?? "nee"); if (match) return await thingRoute.execute(req, match); } return await homeRoute.execute(req); } Deno.serve({ port: 7776 }, handler);