Last active
December 14, 2025 09:33
-
-
Save kkroesch/17ad7371e1fca1fdedaed085ebd904f4 to your computer and use it in GitHub Desktop.
Some Deno Experiments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| PORT := "8000" | |
| VERSION := "v2.6.0" | |
| ARCH := "x86_64-unknown-linux-gnu" | |
| install: # Deno binary for architecture | |
| curl -LO https://github.com/denoland/deno/releases/download/{{VERSION}}/deno-{{ARCH}}.zip | |
| unzip deno-{{ARCH}}.zip | |
| rm deno-{{ARCH}}.zip | |
| serve: | |
| PORT=8000 \ | |
| ./deno run \ | |
| --allow-net \ | |
| --allow-env \ | |
| --allow-read=./welcome.txt \ | |
| index.ts | |
| get: | |
| curl 'http://localhost:8000/health' | |
| curl 'http://localhost:8000/hello?name=Karsten' | |
| format: | |
| ./deno fmt index.ts | |
| ./deno lint index.ts | |
| test: | |
| ./deno test --allow-read index_test.ts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { serve } from "https://deno.land/std@0.224.0/http/server.ts"; | |
| import { z } from "https://deno.land/x/zod@v3.23.8/mod.ts"; | |
| const PORT = Number(Deno.env.get("PORT") ?? "8000"); | |
| const TEMPLATE_PATH = Deno.env.get("TEMPLATE_PATH") ?? "./welcome.txt"; | |
| const Query = z.object({ | |
| name: z.string().min(1).max(40).optional(), | |
| }); | |
| async function loadWelcomeTemplate(): Promise<string> { | |
| return await Deno.readTextFile(TEMPLATE_PATH); | |
| } | |
| serve( | |
| async (req) => { | |
| const url = new URL(req.url); | |
| if (url.pathname === "/health") { | |
| return Response.json({ ok: true }); | |
| } | |
| if (url.pathname === "/hello") { | |
| const parsed = Query.safeParse(Object.fromEntries(url.searchParams.entries())); | |
| if (!parsed.success) { | |
| return Response.json( | |
| { error: "invalid query", details: parsed.error.flatten() }, | |
| { status: 400 }, | |
| ); | |
| } | |
| const name = parsed.data.name ?? "Welt"; | |
| const template = await loadWelcomeTemplate(); | |
| return Response.json({ | |
| message: template.replaceAll("{{name}}", name), | |
| meta: { | |
| ts: new Date().toISOString(), | |
| ua: req.headers.get("user-agent"), | |
| }, | |
| }); | |
| } | |
| return new Response("Not found", { status: 404 }); | |
| }, | |
| { hostname: "0.0.0.0", port: PORT }, | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment