From 6e96c3345714ad62230236f8355c3b2fdd7bb420 Mon Sep 17 00:00:00 2001 From: Michael Wolf Date: Sat, 10 Jan 2026 13:50:44 -0600 Subject: [PATCH] Add very basic support for finding and rendering templates --- express/basic/routes.ts | 18 +++++++++++++++++ express/routes.ts | 2 ++ express/util.ts | 36 ++++++++++++++++++++++++++++++++++ templates/basic/hello.html.njk | 11 +++++++++++ 4 files changed, 67 insertions(+) create mode 100644 express/basic/routes.ts create mode 100644 express/util.ts create mode 100644 templates/basic/hello.html.njk diff --git a/express/basic/routes.ts b/express/basic/routes.ts new file mode 100644 index 0000000..6df97b9 --- /dev/null +++ b/express/basic/routes.ts @@ -0,0 +1,18 @@ +import { DateTime } from "ts-luxon"; +import type { Call, Result, Route } from "../types"; +import { html, render } from "../util"; + +const routes: Record = { + hello: { + path: "/hello", + methods: ["GET"], + handler: async (call: Call): Promise => { + const now = DateTime.now(); + const c = await render("basic/hello", { now }); + + return html(c); + }, + }, +}; + +export { routes }; diff --git a/express/routes.ts b/express/routes.ts index dcb7214..2dc9585 100644 --- a/express/routes.ts +++ b/express/routes.ts @@ -3,6 +3,7 @@ import nunjucks from "nunjucks"; import { DateTime } from "ts-luxon"; import { authRoutes } from "./auth/routes"; +import { routes as basicRoutes } from "./basic/routes"; import { contentTypes } from "./content-types"; import { multiHandler } from "./handlers"; import { httpCodes } from "./http-codes"; @@ -24,6 +25,7 @@ const okText = (result: string): Result => { const routes: Route[] = [ ...authRoutes, + basicRoutes.hello, { path: "/slow", methods: ["GET"], diff --git a/express/util.ts b/express/util.ts new file mode 100644 index 0000000..3d141d8 --- /dev/null +++ b/express/util.ts @@ -0,0 +1,36 @@ +import { readFile } from "node:fs/promises"; +import nunjucks from "nunjucks"; +import { contentTypes } from "./content-types"; +import { executionContext } from "./execution-context"; +import { httpCodes } from "./http-codes"; +import type { Result } from "./types"; + +// FIXME: Handle the error here +const loadFile = async (path: string): Promise => { + // Specifying 'utf8' returns a string; otherwise, it returns a Buffer + const data = await readFile(path, "utf8"); + + return data; +}; + +const render = async (path: string, ctx: object): Promise => { + const fullPath = `${executionContext.diachron_root}/templates/${path}.html.njk`; + + const template = await loadFile(fullPath); + + const retval = nunjucks.renderString(template, ctx); + + return retval; +}; + +const html = (payload: string): Result => { + const retval: Result = { + code: httpCodes.success.OK, + result: payload, + contentType: contentTypes.text.html, + }; + + return retval; +}; + +export { render, html }; diff --git a/templates/basic/hello.html.njk b/templates/basic/hello.html.njk new file mode 100644 index 0000000..bf2f094 --- /dev/null +++ b/templates/basic/hello.html.njk @@ -0,0 +1,11 @@ + + + +

+ Hello. +

+

+ The current time is {{ now }}. +

+ +