djledda.de main
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.
 
 

43 lines
1.7 KiB

  1. import { serveFile } from "jsr:@std/http/file-server";
  2. import { createSSRApp } from "vue";
  3. import { renderToString } from "vue/server-renderer";
  4. import App from "@/App.tsx";
  5. import transpileResponse from './transpile.ts';
  6. const utf8Decoder = new TextDecoder("utf-8");
  7. Deno.serve({
  8. port: 8080,
  9. hostname: "0.0.0.0",
  10. onListen({ port, hostname }) {
  11. console.log(`Listening on port http://${hostname}:${port}/`);
  12. },
  13. }, async (req, _conn) => {
  14. if (req.method === "GET") {
  15. const pathname = URL.parse(req.url)?.pathname ?? "/";
  16. if (pathname === "/") {
  17. const rendered = await renderToString(createSSRApp(App));
  18. const content = utf8Decoder.decode(await Deno.readFile("./public/index.html"))
  19. .replace(`<!-- SSR OUTLET -->`, rendered)
  20. .replace(`<!-- SSR HEAD OUTLET -->`, '');
  21. return new Response(content, { headers: { "Content-Type": "text/html" } });
  22. } else if (pathname === "/health") {
  23. return new Response("OK");
  24. } else if (pathname.startsWith('/app') && (pathname.endsWith('.ts') || pathname.endsWith('.tsx'))) {
  25. const response = await serveFile(req, './' + pathname);
  26. return await transpileResponse(response, req.url, pathname);
  27. } else if (pathname.startsWith('/deps')) {
  28. return serveFile(req, `node_modules/${pathname.split('/deps')[1]}`);
  29. } else {
  30. return serveFile(req, `public${ pathname }`);
  31. }
  32. } else {
  33. return new Response("Only GET allowed.", { status: 500 });
  34. }
  35. });
  36. Deno.addSignalListener("SIGINT", () => {
  37. console.info("Shutting down (received SIGINT)");
  38. Deno.exit();
  39. });