fix that
This commit is contained in:
5
README.md
Normal file
5
README.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# hatsune-2025-stats
|
||||||
|
|
||||||
|
use [Deno](https://deno.com/) to run
|
||||||
|
|
||||||
|
merry new year
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"tasks": {
|
"tasks": {
|
||||||
"dev": "deno run -A main.ts"
|
"dev": "deno run --allow-read=./ --allow-net main.ts"
|
||||||
},
|
},
|
||||||
"imports": {
|
"imports": {
|
||||||
"@std/assert": "jsr:@std/assert@1",
|
"@std/assert": "jsr:@std/assert@1",
|
||||||
|
|||||||
62
main.ts
62
main.ts
@@ -1,42 +1,54 @@
|
|||||||
import { contentType } from "@std/media-types";
|
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
|
|
||||||
const NET_PORT = 4536;
|
const NET_PORT = 4536;
|
||||||
const NET_HOST = '10.0.1.39';
|
const NET_HOST = '10.0.1.39';
|
||||||
const WEB_ROOT = './res/';
|
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({
|
Deno.serve({
|
||||||
hostname: NET_HOST,
|
hostname: NET_HOST,
|
||||||
port: NET_PORT,
|
port: NET_PORT,
|
||||||
onListen: addr => console.log(`Listening on http://${addr.hostname}:${addr.port}`)
|
onListen: addr => console.log(`Listening on http://${addr.hostname}:${addr.port}`)
|
||||||
}, req => {
|
}, req => {
|
||||||
|
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);
|
const url = new URL(req.url);
|
||||||
|
|
||||||
if (url.pathname == '/') url.pathname = '/index.html';
|
if (url.pathname == '/') url.pathname = '/index.html';
|
||||||
|
|
||||||
|
const mapping = mappings.find(val => val.endpoints.some(val => url.pathname === val));
|
||||||
|
|
||||||
|
console.log(`${req.method} ${url.pathname} | mapping exists: ${typeof mapping !== 'undefined'}`);
|
||||||
return new Promise<Response>(resolve => {
|
return new Promise<Response>(resolve => {
|
||||||
Deno.readFile(path.join(WEB_ROOT, url.pathname)).then(data => {
|
if (mapping) {
|
||||||
|
Deno.readFile(path.join(WEB_ROOT, mapping.path)).then(data => {
|
||||||
const pathParts = url.pathname.split('/');
|
resolve(new Response(data, { headers: { "Content-Type": mapping.mime }}));
|
||||||
const lastPart = pathParts[pathParts.length - 1];
|
}).catch(reason => {
|
||||||
const extensionParts = lastPart.split('.');
|
console.error(reason);
|
||||||
const extension = extensionParts[extensionParts.length - 1];
|
resolve(notFound);
|
||||||
|
});
|
||||||
const mime = contentType(extension);
|
} else resolve(notFound);
|
||||||
if (!mime) throw new Error("No mime-type found");
|
});
|
||||||
|
|
||||||
console.log(`${req.method} ${url}`);
|
|
||||||
resolve(new Response(data, { headers: { "Content-Type": mime }}))
|
|
||||||
|
|
||||||
}).catch(() => {
|
|
||||||
|
|
||||||
console.log(`404 ${req.method} ${url}`);
|
|
||||||
resolve(new Response("Not Found. Did you try thinking Miku?", {
|
|
||||||
status: 404,
|
|
||||||
statusText: "Not Found (oo-ee-oo)",
|
|
||||||
headers: { "Content-Type": "text/plain" }
|
|
||||||
}));
|
|
||||||
|
|
||||||
});
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user