diff --git a/Dockerfile b/Dockerfile index d0a9adb..a81df0f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,9 +4,6 @@ WORKDIR /app COPY . /app -# Absolutely no idea why, but piping the output directly to the file makes it empty. -RUN mkdir -p /tmp/templates && mv /app/templates/*.html /tmp/templates/ -RUN bunx html-minifier --collapse-whitespace --remove-comments --remove-optional-tags --remove-redundant-attributes --remove-script-type-attributes --remove-tag-whitespace --use-short-doctype --minify-css true --minify-js true /tmp/templates/index.html > /app/templates/index.html RUN bun install --production EXPOSE 3000 diff --git a/bun.lockb b/bun.lockb index 5b268d4..75248b3 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index b9a2da3..7142c71 100644 --- a/package.json +++ b/package.json @@ -8,11 +8,13 @@ "dependencies": { "hono": "^4.6.9", "ip": "^2.0.1", + "nunjucks": "^3.2.4", "zod": "^3.23.8" }, "devDependencies": { "@types/bun": "latest", "@types/ip": "^1.1.3", + "@types/nunjucks": "^3.2.6", "prettier": "^3.3.3" } } diff --git a/src/index.ts b/src/index.ts index 1556924..f853de1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,11 @@ import { Hono } from "hono"; import { portRoute } from "./routes/port"; import { rootRoute } from "./routes/root"; import { serveStatic } from "hono/bun"; +import * as nunjucks from "nunjucks"; + +nunjucks.configure("templates", { + dev: process.env.NODE_ENV === "development", +}); const app = new Hono() .use("/static/*", serveStatic({ root: "./" })) diff --git a/src/utils/renderer.ts b/src/utils/renderer.ts index ff75bb3..9e56dd3 100644 --- a/src/utils/renderer.ts +++ b/src/utils/renderer.ts @@ -1,46 +1,27 @@ import { Context } from "hono"; import { PresentationType } from "../middlewares/presentation"; +import * as nunjucks from "nunjucks"; export default async function render( - request: Context, + context: Context, templateName: "index", ctx: Record, ) { - const presentation = request.get("presentation"); - let content = await getTemplate(templateName, presentation); - - for (const [key, value] of Object.entries(ctx)) { - content = content.replaceAll(`{{${key}}}`, value); - } - - switch (presentation) { - case "browser": { - return request.html(content); - } - case "terminal": { - return request.text(content); - } - } -} - -const _templateCache: Record = {}; - -async function getTemplate( - templateName: "index", - presentation: PresentationType, -): Promise { + const presentation = context.get("presentation") as PresentationType; const extension = { browser: ".html", terminal: ".txt", }[presentation]; - const key = templateName + extension; + const key = templateName + extension + ".njk"; - if (_templateCache[key]) { - return _templateCache[key]; + const content = nunjucks.render(key, ctx); + + switch (presentation) { + case "browser": + return context.html(content); + break; + case "terminal": + return context.text(content); + break; } - - const currentPath = await Bun.resolve(`./templates/${key}`, process.cwd()); - _templateCache[key] = await Bun.file(currentPath).text(); - - return _templateCache[key]; } diff --git a/templates/index.html b/templates/index.html.njk similarity index 98% rename from templates/index.html rename to templates/index.html.njk index a7bbe4a..847db48 100644 --- a/templates/index.html +++ b/templates/index.html.njk @@ -4,6 +4,7 @@ + amiopen.now