some stuff

This commit is contained in:
john 2024-12-03 17:10:40 +01:00
parent 08ad294d46
commit 6cbf15b783
11 changed files with 165 additions and 5 deletions

44
server.ts Normal file
View file

@ -0,0 +1,44 @@
import * as path from "@std/path"
export function startServer(hostname = "localhost", port = 6969) {
return Deno.serve(
{ hostname, port },
async (req) => {
const url = new URL(req.url);
const filepath = decodeURIComponent(url.pathname);
try {
const file = await Deno.open("." + filepath, { read: true });
const headers: [string, string][] = [
["Cache-Control", "no-cache"]
]
const extension = path.extname(filepath);
const contentType = {
'.svg': "image/svg+xml",
'.png': "image/png",
'.jpeg': "image/jpeg",
'.woff2': "font/woff2",
'.ttf': "font/ttf",
'.css': "text/css",
".html": "text/html"
}[extension]
if (contentType) {
headers.push(["Content-Type", contentType])
}
return new Response(file.readable, {
headers
});
} catch {
return new Response("404 Not Found", { status: 404 });
}
},
);
}
if (import.meta.main) {
startServer("localhost", 6969)
}