diff --git a/express/basic/login.ts b/express/basic/login.ts index abbaee9..7977a2b 100644 --- a/express/basic/login.ts +++ b/express/basic/login.ts @@ -2,7 +2,7 @@ import { SESSION_COOKIE_NAME } from "../auth/token"; import { tokenLifetimes } from "../auth/types"; import { services } from "../services"; import type { Call, Result, Route } from "../types"; -import { html, redirect, render } from "../util"; +import { html, redirect, render } from "../request/util"; const loginHandler = async (call: Call): Promise => { if (call.method === "GET") { diff --git a/express/basic/logout.ts b/express/basic/logout.ts index a1f7dc8..85a3e80 100644 --- a/express/basic/logout.ts +++ b/express/basic/logout.ts @@ -1,7 +1,7 @@ import { SESSION_COOKIE_NAME } from "../auth/token"; import { services } from "../services"; import type { Call, Result, Route } from "../types"; -import { redirect } from "../util"; +import { redirect } from "../request/util"; const logoutHandler = async (call: Call): Promise => { // Extract token from cookie and invalidate the session diff --git a/express/basic/routes.ts b/express/basic/routes.ts index d5b1f20..b68b139 100644 --- a/express/basic/routes.ts +++ b/express/basic/routes.ts @@ -1,7 +1,7 @@ import { DateTime } from "ts-luxon"; import { services } from "../services"; import type { Call, Result, Route } from "../types"; -import { html, render } from "../util"; +import { html, render } from "../request/util"; import { loginRoute } from "./login"; import { logoutRoute } from "./logout"; diff --git a/express/request/util.ts b/express/request/util.ts new file mode 100644 index 0000000..e5b445d --- /dev/null +++ b/express/request/util.ts @@ -0,0 +1,46 @@ +import { contentTypes } from "../content-types"; +import { executionContext } from "../execution-context"; +import { httpCodes } from "../http-codes"; +import type { RedirectResult, Result } from "../types"; +import {services} from '../services' + +import{loadFile}from'../util' + + +type NoUser={ + [key: string]: unknown; +} & { + user?: never; +} + +const render = async (path: string, ctx?: NoUser): Promise => { + const fullPath = `${executionContext.diachron_root}/templates/${path}.html.njk`; + const template = await loadFile(fullPath); + const user = services.session.getUser(); + const context = {user, ...ctx} + const engine = services.conf.templateEngine() + const retval = engine.renderTemplate(template, context); + + return retval; +}; + +const html = (payload: string): Result => { + const retval: Result = { + code: httpCodes.success.OK, + result: payload, + contentType: contentTypes.text.html, + }; + + return retval; +}; + +const redirect = (location: string): RedirectResult => { + return { + code: httpCodes.redirection.SeeOther, + contentType: contentTypes.text.plain, + result: "", + redirect: location, + }; +}; + +export { render, html, redirect }; diff --git a/express/services/index.ts b/express/services/index.ts index 3a9ff69..daed08f 100644 --- a/express/services/index.ts +++ b/express/services/index.ts @@ -5,6 +5,17 @@ import { getCurrentUser } from "../context"; import { db, migrate, migrationStatus, PostgresAuthStore } from "../database"; import { getLogs, log } from "../logging"; import type { MaybeUser } from "../user"; +import nunjucks from 'nunjucks' + +const conf = { + templateEngine: () => { + return { + renderTemplate: (template: string, context: object) => { + return nunjucks.renderString(template, context); + }, + } + } +}; const database = { db, @@ -42,6 +53,7 @@ const auth = new AuthService(authStore); // Keep this asciibetically sorted const services = { auth, + conf, database, logging, misc, diff --git a/express/util.ts b/express/util.ts index c80b7a7..6aba4e9 100644 --- a/express/util.ts +++ b/express/util.ts @@ -1,9 +1,5 @@ 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 { RedirectResult, Result } from "./types"; // FIXME: Handle the error here const loadFile = async (path: string): Promise => { @@ -13,35 +9,8 @@ const loadFile = async (path: string): Promise => { 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 c = ctx === undefined ? {} : ctx; - - const retval = nunjucks.renderString(template, c); - - return retval; -}; - -const html = (payload: string): Result => { - const retval: Result = { - code: httpCodes.success.OK, - result: payload, - contentType: contentTypes.text.html, - }; - - return retval; -}; - -const redirect = (location: string): RedirectResult => { - return { - code: httpCodes.redirection.SeeOther, - contentType: contentTypes.text.plain, - result: "", - redirect: location, - }; -}; - -export { render, html, redirect }; +export { + loadFile, +}