Files
djledda-web/main.ts
Daniel Ledda ea5be77bc9 better
2024-10-31 17:14:30 +01:00

43 lines
1.7 KiB
TypeScript

import { serveFile } from "jsr:@std/http/file-server";
import { createSSRApp } from "vue";
import { renderToString } from "vue/server-renderer";
import App from "@/App.tsx";
import transpileResponse from './transpile.ts';
const utf8Decoder = new TextDecoder("utf-8");
Deno.serve({
port: 8080,
hostname: "0.0.0.0",
onListen({ port, hostname }) {
console.log(`Listening on port http://${hostname}:${port}/`);
},
}, async (req, _conn) => {
if (req.method === "GET") {
const pathname = URL.parse(req.url)?.pathname ?? "/";
if (pathname === "/") {
const rendered = await renderToString(createSSRApp(App));
const content = utf8Decoder.decode(await Deno.readFile("./public/index.html"))
.replace(`<!-- SSR OUTLET -->`, rendered)
.replace(`<!-- SSR HEAD OUTLET -->`, '');
return new Response(content, { headers: { "Content-Type": "text/html" } });
} else if (pathname === "/health") {
return new Response("OK");
} else if (pathname.startsWith('/app') && (pathname.endsWith('.ts') || pathname.endsWith('.tsx'))) {
const response = await serveFile(req, './' + pathname);
return await transpileResponse(response, req.url, pathname);
} else if (pathname.startsWith('/deps')) {
return serveFile(req, `node_modules/${pathname.split('/deps')[1]}`);
} else {
return serveFile(req, `public${ pathname }`);
}
} else {
return new Response("Only GET allowed.", { status: 500 });
}
});
Deno.addSignalListener("SIGINT", () => {
console.info("Shutting down (received SIGINT)");
Deno.exit();
});