summaryrefslogtreecommitdiff
path: root/src/app.ts
blob: 7ed535cb31e7b91ffc96ce5807b642c68d2344fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import express from "express";
import cookieParser from "cookie-parser";
import { create as createHandlebars, ExpressHandlebars } from "express-handlebars";

import routes from "./routes.js";
import frontend from "./routes/frontend.js";
import activitypub from "./routes/activitypub.js";
import event from "./routes/event.js";
import group from "./routes/group.js";
import staticPages from "./routes/static.js";
import magicLink from "./routes/magicLink.js";
import {
    activityPubContentType,
    alternateActivityPubContentType,
} from "./lib/activitypub.js";
import { EmailService } from "./lib/email.js";
import getConfig from "./lib/config.js";

const app = express();
const config = getConfig();

const hbsInstance = createHandlebars({
    defaultLayout: "main",
    partialsDir: ["views/partials/"],
    layoutsDir: "views/layouts/",
    helpers: {
        plural: function (number: number, text: string) {
            const 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)
            const 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: object) {
            return JSON.stringify(context);
        },
    },
});

const emailService = new EmailService(config, hbsInstance);
emailService.verify();

app.use((req: express.Request, _: express.Response, next: express.NextFunction) => {
    req.hbsInstance = hbsInstance;
    req.emailService = emailService;
    next()
    return
})

// View engine //
app.engine("handlebars", hbsInstance.engine);
app.set("view engine", "handlebars");
app.set("hbsInstance", hbsInstance);

// Static files //
app.use(express.static("public"));

// Body parser //
app.use(express.json({ type: alternateActivityPubContentType }));
app.use(express.json({ type: activityPubContentType }));
app.use(express.json({ type: "application/json" }));
app.use(express.urlencoded({ extended: true }));

// Cookies //
app.use(cookieParser());

// Router //
app.use("/", staticPages);
app.use("/", frontend);
app.use("/", activitypub);
app.use("/", event);
app.use("/", group);
app.use("/", magicLink);
app.use("/", routes);

export default app;