mirror of
https://github.com/Myzel394/amiopen.now.git
synced 2025-06-18 07:25:27 +02:00
feat: Use nunjucks for rendering
This commit is contained in:
parent
82774d656d
commit
2a55890764
@ -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
|
||||
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
@ -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: "./" }))
|
||||
|
@ -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<string, any>,
|
||||
) {
|
||||
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<string, string> = {};
|
||||
|
||||
async function getTemplate(
|
||||
templateName: "index",
|
||||
presentation: PresentationType,
|
||||
): Promise<string> {
|
||||
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];
|
||||
}
|
||||
|
@ -4,6 +4,7 @@
|
||||
<meta charset="UTF-8" />
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="darkreader-lock">
|
||||
<title>amiopen.now</title>
|
||||
|
||||
<style type="text/css">
|
@ -1,8 +1,7 @@
|
||||
ip=$1
|
||||
user=$2
|
||||
|
||||
cd server &&
|
||||
docker buildx build --platform linux/amd64 . --tag amiopen:latest &&
|
||||
docker buildx build --platform linux/amd64 . --tag amiopen:latest &&
|
||||
docker save -o amiopen.tar amiopen:latest &&
|
||||
scp amiopen.tar $user@$ip:~/amiopen/amiopen.tar &&
|
||||
ssh $user@$ip "cd ~/amiopen && docker container stop amiopen && docker container rm amiopen && docker load -i amiopen.tar && docker compose up -d"
|
||||
|
Loading…
x
Reference in New Issue
Block a user