You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.9 KiB

import { h } from "https://deno.land/x/corfu@v2.0.1/mod.ts";
import { Application, Router } from "https://deno.land/x/oak@v11.1.0/mod.ts";
import config from "./config.ts";
const app = new Application();
//print info on listen
app.addEventListener("listen", (event) => {
console.log(
`voltpad listening on http://${event.hostname.replace(
"::1",
"localhost"
)}:${event.port}`
);
});
app.use(async (ctx) => {
if (ctx.request.url.pathname === "/") ctx.request.url.pathname = "/index";
let mod;
try {
mod = await import(
"./routes" +
ctx.request.url.pathname +
".tsx" +
`?update=${Date.now()}`
);
} catch {
try {
return await ctx.send({
root: `./static`,
index: `${ctx.request.url.pathname}`
});
} catch {
mod = await import("./routes/404.tsx");
}
}
let rizz;
try {
rizz = await mod.default(ctx.request.url.searchParams);
} catch (e) {
console.error(e);
mod = await import("./routes/500.tsx");
rizz = await mod.default();
}
ctx.response.body = "<!DOCTYPE html>" + rizz;
});
//(attempt to) set mimetype
app.use(async (ctx, next) => {
await next();
if (!ctx.response.headers.get("Content-Type")) {
switch (ctx.request.url.pathname.split(".").at(-1)) {
case "json":
ctx.response.headers.set("Content-Type", "application/json");
break;
default:
ctx.response.headers.set("Content-Type", "text/html");
}
}
});
app.use(async (ctx, next) => {
await next();
const rt = ctx.response.headers.get("X-Response-Time");
console.log(`${ctx.request.method} ${ctx.request.url} - ${rt}`);
});
await app.listen({
hostname: config.serve.address,
port: config.serve.port
});