Files
hatsune-2025-stats/main.ts
2026-01-01 01:21:01 -05:00

54 lines
1.3 KiB
TypeScript

import path from "node:path";
const NET_PORT = 4536;
const NET_HOST = '127.0.0.1';
const WEB_ROOT = './res/';
interface FileMapping {
endpoints: string[],
path: string,
mime: string
}
const mappings: FileMapping[] = [
{
endpoints: ['/', '/index.html'],
path: "/index.html",
mime: "text/html"
},
{
endpoints: ['/style.css'],
path: "/style.css",
mime: "text/css"
}
] as const;
Deno.serve({
hostname: NET_HOST,
port: NET_PORT,
onListen: addr => console.log(`Listening on http://${addr.hostname}:${addr.port}`)
}, (req, addr) => {
const notFound = new Response("Not Found. Did you try thinking Miku?", {
status: 404,
statusText: "Not Found (oo-ee-oo)",
headers: { "Content-Type": "text/plain" }
});
const url = new URL(req.url);
if (url.pathname == '/') url.pathname = '/index.html';
const mapping = mappings.find(val => val.endpoints.some(val => url.pathname === val));
console.log(`${addr.remoteAddr.hostname}:${addr.remoteAddr.port} ${req.method} ${url.pathname} | mapping exists: ${typeof mapping !== 'undefined'}`);
return new Promise<Response>(resolve => {
if (mapping) {
Deno.readFile(path.join(WEB_ROOT, mapping.path)).then(data => {
resolve(new Response(data, { headers: { "Content-Type": mapping.mime }}));
}).catch(reason => {
console.error(reason);
resolve(notFound);
});
} else resolve(notFound);
});
});