first commit

This commit is contained in:
Daniel Ledda
2024-10-30 20:42:01 +01:00
commit e6f20af28d
13 changed files with 556 additions and 0 deletions

42
main.ts Normal file
View File

@@ -0,0 +1,42 @@
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 './transpileTs.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("./playground/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, `playground${pathname}`);
}
} else {
return new Response("Only GET allowed.", { status: 500 });
}
});
Deno.addSignalListener("SIGINT", () => {
console.info("Shutting down (received SIGINT)");
Deno.exit();
});