summaryrefslogtreecommitdiff
path: root/src/app.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/app.ts')
-rwxr-xr-xsrc/app.ts68
1 files changed, 3 insertions, 65 deletions
diff --git a/src/app.ts b/src/app.ts
index 85ee64e..5f7c024 100755
--- a/src/app.ts
+++ b/src/app.ts
@@ -1,5 +1,4 @@
import express from "express";
-import hbs from "express-handlebars";
import cookieParser from "cookie-parser";
import routes from "./routes.js";
@@ -15,77 +14,16 @@ import {
activityPubContentType,
alternateActivityPubContentType,
} from "./lib/activitypub.js";
-import getConfig from "./lib/config.js";
+import { HandlebarsSingleton } from "./lib/handlebars.js";
const app = express();
-const config = getConfig();
initEmailService().then((sendEmails) => (app.locals.sendEmails = sendEmails));
// View engine //
-const hbsInstance = hbs.create({
- defaultLayout: "main",
- partialsDir: ["views/partials/"],
- layoutsDir: "views/layouts/",
- runtimeOptions: {
- data: {
- domain: config.general.domain,
- contactEmail: config.general.email,
- siteName: config.general.site_name,
- mailService: config.general.mail_service,
- siteLogo: config.general.email_logo_url,
- isFederated: config.general.is_federated || true,
- },
- },
- helpers: {
- plural: function (number: number, text: string) {
- var singular = number === 1;
- // If no text parameter was given, just return a conditional s.
- if (typeof text !== "string") return singular ? "" : "s";
- // Split with regex into group1/group2 or group1(group3)
- var match = text.match(/^([^()\/]+)(?:\/(.+))?(?:\((\w+)\))?/);
- // If no match, just append a conditional s.
- if (!match) return text + (singular ? "" : "s");
- // We have a good match, so fire away
- return (
- (singular && match[1]) || // Singular case
- match[2] || // Plural case: 'bagel/bagels' --> bagels
- match[1] + (match[3] || "s")
- ); // Plural case: 'bagel(s)' or 'bagel' --> bagels
- },
- json: function (context: any) {
- return JSON.stringify(context);
- },
- },
-});
-app.locals.renderEmail = async function renderEmail(
- template: string,
- data: object
-) {
- const [html, text] = await Promise.all([
- hbsInstance.renderView(
- `./views/emails/${template}Html.handlebars`,
- {
- cache: true,
- layout: "email.handlebars",
- ...data,
- }
- ),
- hbsInstance.renderView(
- `./views/emails/${template}Text.handlebars`,
- {
- cache: true,
- layout: "email.handlebars",
- ...data,
- }
- ),
- ]);
- return { html, text }
-}
-
-app.engine("handlebars", hbsInstance.engine);
+app.engine("handlebars", HandlebarsSingleton.instance.engine);
app.set("view engine", "handlebars");
-app.set("hbsInstance", hbsInstance);
+app.set("hbsInstance", HandlebarsSingleton.instance);
// Static files //
app.use(express.static("public"));