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.
 
 

207 lines
7.5 KiB

  1. import { serveFile } from "jsr:@std/http/file-server";
  2. import { type App, toValue } from "vue";
  3. import { type Router } from "vue-router";
  4. import { renderToString } from "vue/server-renderer";
  5. import transpileResponse from "./transpile.ts";
  6. import { DOMParser } from "jsr:@b-fuze/deno-dom";
  7. import { join } from "jsr:@std/path/join";
  8. import { exists } from "jsr:@std/fs";
  9. import { type DJSSRContext } from "@/useDJSSRContext.ts";
  10. import { type DJAPIResult, type DJAPIResultMap } from "@/api.ts";
  11. const utf8Decoder = new TextDecoder("utf-8");
  12. const parser = new DOMParser();
  13. function appHeaderScript(params: { ssrContext: DJSSRContext, entryPath: string }) {
  14. return `<script type="importmap">
  15. {
  16. "imports": {
  17. "vue": "/deps/vue/dist/vue.esm-browser.prod.js",
  18. "vue-router": "/deps/vue-router/dist/vue-router.esm-browser.js",
  19. "vue/jsx-runtime": "/deps/vue/jsx-runtime/index.mjs",
  20. "@vue/devtools-api": "/app/devtools-shim.ts",
  21. "@/": "/app/"
  22. }
  23. }
  24. </script>
  25. <style>
  26. ${ Object.values(params.ssrContext.styles).join('\n') }
  27. </style>
  28. <script type="module">
  29. window.appstate = ${JSON.stringify(params.ssrContext.registry)};
  30. import('${params.entryPath}');
  31. </script>`;
  32. }
  33. async function* siteEntries(path: string): AsyncGenerator<string> {
  34. for await (const dirEnt of Deno.readDir(path)) {
  35. if (dirEnt.isDirectory) {
  36. yield* siteEntries(join(path, dirEnt.name));
  37. } else if (dirEnt.name === "index_template.html") {
  38. yield path.split("/")[1] ?? "";
  39. }
  40. }
  41. }
  42. const publicFiles = siteEntries("public");
  43. const sites: string[] = [];
  44. for await (const path of publicFiles) {
  45. sites.push(path);
  46. }
  47. async function getAPIResponse(apiReq: Request): Promise<Response> {
  48. let jsonResponse: DJAPIResult | { error: string } | null = null;
  49. let status = 200;
  50. const pathname = URL.parse(apiReq.url)?.pathname;
  51. if (!pathname) {
  52. jsonResponse = { error: "Invalid Route" };
  53. status = 400;
  54. }
  55. if (!jsonResponse && pathname) {
  56. const apiPath = pathname.split("/api")[1];
  57. if (apiPath === "/rp-articles") {
  58. const paths: string[] = [];
  59. const contentDir = './public/generative-energy/content/';
  60. for await (const dirEnt of Deno.readDir(contentDir)) {
  61. if (dirEnt.isFile && dirEnt.name.endsWith('.html')) {
  62. paths.push(`${contentDir}${dirEnt.name}`);
  63. }
  64. }
  65. const result: DJAPIResultMap['/rp-articles'] = [];
  66. for (const filePath of paths) {
  67. const content = await Deno.readTextFile(filePath);
  68. const dom = parser.parseFromString(content, 'text/html');
  69. const metadata = { title: '', tags: [] as string[], slug: '' };
  70. const metaTags = dom.querySelectorAll('meta') as unknown as NodeListOf<HTMLMetaElement>;
  71. for (const metaTag of metaTags) {
  72. const name = metaTag.attributes.getNamedItem('name')?.value ?? '';
  73. const content = metaTag.attributes.getNamedItem('content')?.value ?? '';
  74. if (name === 'title') {
  75. metadata.title = content;
  76. } else if (name === 'tags') {
  77. metadata.tags = content ? content.split(",") : [];
  78. } else if (name === 'slug') {
  79. metadata.slug = content;
  80. }
  81. }
  82. result.push(metadata);
  83. }
  84. jsonResponse = result;
  85. console.log(result);
  86. }
  87. }
  88. if (!jsonResponse) {
  89. jsonResponse = { error: `API route ${ apiPath } not found.` };
  90. status = 404;
  91. }
  92. const headers = new Headers();
  93. headers.set("Content-Type", "application/json");
  94. return new Response(JSON.stringify(jsonResponse), {
  95. status,
  96. headers,
  97. });
  98. }
  99. Deno.serve({
  100. port: 8080,
  101. hostname: "0.0.0.0",
  102. onListen({ port, hostname }) {
  103. console.log(`Listening on port http://${hostname}:${port}/`);
  104. },
  105. }, async (req, _conn) => {
  106. const timeStart = new Date().getTime();
  107. let response: Response | null = null;
  108. const url = URL.parse(req.url);
  109. if (req.method === "GET") {
  110. const pathname = url?.pathname ?? "/";
  111. if (pathname.startsWith("/api/")) {
  112. response = await getAPIResponse(req);
  113. }
  114. // Public/static files
  115. if (!response) {
  116. let filepath = join(".", "public", pathname);
  117. if (filepath.endsWith("/")) {
  118. filepath = join(filepath, "index.html");
  119. }
  120. if (await exists(filepath, { isFile: true })) {
  121. response = await serveFile(req, filepath);
  122. }
  123. }
  124. // NPM Vendor deps
  125. if (response === null && pathname.startsWith("/deps")) {
  126. response = await serveFile(req, `node_modules/${pathname.split("/deps")[1]}`);
  127. }
  128. // Transpile Code
  129. if (
  130. response === null &&
  131. pathname.startsWith("/app") &&
  132. (pathname.endsWith(".ts") || pathname.endsWith(".tsx")) &&
  133. !pathname.endsWith("server.ts")
  134. ) {
  135. response = await serveFile(req, "./" + pathname);
  136. response = await transpileResponse(response, req.url, pathname);
  137. }
  138. // SSR
  139. if (response === null) {
  140. const baseDirectoryName = pathname.split("/")[1] ?? "";
  141. if (sites.includes(baseDirectoryName)) {
  142. const appLocation = baseDirectoryName === "" ? "home" : baseDirectoryName;
  143. const siteTemplate = join("public", baseDirectoryName, "index_template.html");
  144. const siteEntry = join("app", appLocation, "server.ts");
  145. const clientEntry = join("@", appLocation, "client.ts");
  146. const { app, router } = (await import("./" + siteEntry)).default() as {
  147. app: App;
  148. router: Router | null;
  149. };
  150. app.provide("dom-parse", (innerHTML: string) => {
  151. return parser.parseFromString(innerHTML, "text/html").documentElement;
  152. });
  153. const ssrContext: DJSSRContext = { styles: {}, registry: {}, head: { title: "" } };
  154. if (router) {
  155. await router.replace(pathname.split("/generative-energy")[1]);
  156. await router.isReady();
  157. }
  158. const rendered = await renderToString(app, ssrContext);
  159. const content = utf8Decoder.decode(await Deno.readFile(siteTemplate))
  160. .replace(`<!-- SSR OUTLET -->`, rendered)
  161. .replaceAll("%TITLE%", toValue(ssrContext.head?.title) ?? "Site")
  162. .replace(
  163. `<!-- SSR HEAD OUTLET -->`,
  164. appHeaderScript({ ssrContext, entryPath: clientEntry }),
  165. );
  166. response = new Response(content, { headers: { "Content-Type": "text/html" } });
  167. }
  168. }
  169. } else {
  170. response = new Response("Only GET allowed.", { status: 500 });
  171. }
  172. response ??= new Response("Not found.", { status: 404 })
  173. const timeEnd = new Date().getTime();
  174. console.log(`Request ${ url?.pathname ?? 'malformed' }\tStatus ${ response.status }, Duration ${ timeEnd - timeStart }ms`);
  175. return response;
  176. });
  177. Deno.addSignalListener("SIGINT", () => {
  178. console.info("Shutting down (received SIGINT)");
  179. Deno.exit();
  180. });