This repository has been archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.ts
159 lines (134 loc) · 4.63 KB
/
app.ts
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
require("source-map-support").install();
import express = require("express");
import i18next from "i18next";
import i18nextHttpMiddleware from "i18next-http-middleware";
import i18nextFsBackend from "i18next-fs-backend";
import path = require("path");
import config from "config";
import bodyParser from "body-parser";
import h5pStartup from "./h5p/h5pServer";
import h5pApi from "./routes/h5pApi";
import Context from "./Context";
import { LayoutDisplay, ErrorModel } from "./models/types";
import InitAuthentication from "./routes/Authentication";
import AddRequestLogger from "./requestLogger";
process.on("unhandledRejection", console.log);
const start = async () => {
const translationFunction = await i18next
.use(i18nextFsBackend)
.use(i18nextHttpMiddleware.LanguageDetector) // This will add the
// properties language and languages to the req object.
// See https://github.com/i18next/i18next-http-middleware#adding-own-detection-functionality
// how to detect language in your own fashion. You can also choose not
// to add a detector if you only want to use one language.
.init({
detection: {
// order and from where user language should be detected
order: ["querystring", "cookie", "header"],
// keys or params to lookup language from
lookupQuerystring: "lng",
lookupCookie: "i18next",
lookupHeader: "accept-language",
lookupHeaderRegex: /(([a-z]{2})-?([A-Z]{2})?)\s*;?\s*(q=([0-9.]+))?/gi,
lookupSession: "lng",
lookupPath: "lng",
lookupFromPathIndex: 0,
},
backend: {
loadPath: "assets/translations/{{ns}}/{{lng}}.json",
},
debug: process.env.DEBUG && process.env.DEBUG.includes("i18n"),
defaultNS: "server",
fallbackLng: "en",
ns: [
"client",
"copyright-semantics",
"metadata-semantics",
"mongo-s3-content-storage",
"s3-temporary-storage",
"server",
"storage-file-implementations",
],
preload: ["en", "de"], // If you don't use a language detector of
// i18next, you must preload all languages you want to use!
});
const app = express();
AddRequestLogger(app);
app.use(bodyParser.json({ limit: "500mb" }));
app.use(
bodyParser.urlencoded({
extended: true,
})
);
Context.setup(app);
const pathPrefix = config.get("server.pathPrefix") as string;
const router = express.Router();
// The i18nextExpressMiddleware injects the function t(...) into the req
// object. This function must be there for the Express adapter
// (H5P.adapters.express) to function properly.
app.use(i18nextHttpMiddleware.handle(i18next));
//view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "pug");
router.get("/favicon.ico", (req, res) => {
res.statusCode = 404;
res.end();
});
router.use(express.static(path.join(__dirname, "public")));
InitAuthentication(router);
router.use("/:UserToken/api", h5pApi());
await h5pStartup(router);
// catch 404 and forward to error handler
router.use((req, res, next) => {
const err = new Error("Not Found");
err["status"] = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get("env") === "development") {
router.use((err: Error, req, res, next) => {
// eslint-disable-line @typescript-eslint/no-unused-vars
const context = Context.current();
const model: ErrorModel = {
title: "Error",
message: err.message,
error: err,
baseUrl: pathPrefix,
User: context.User,
display: config.get("display") as LayoutDisplay,
};
res.status(err["status"] || 500);
res.render("error", model);
});
}
// production error handler
// no stacktraces leaked to user
router.use((err, req, res, next) => {
// eslint-disable-line @typescript-eslint/no-unused-vars
const context = Context.current();
const model: ErrorModel = {
title: "Error",
message: err.message,
error: {},
baseUrl: pathPrefix,
User: context.User,
display: config.get("display") as LayoutDisplay,
};
res.status(err.status || 500);
res.render("error", model);
console.error(
`${req.method} ${req.originalUrl}; ${res.statusCode} ${
res.statusMessage
}; ${JSON.stringify(err)}`
);
});
app.use(pathPrefix, router);
app.set("port", process.env.PORT || 1338);
const server = app.listen(app.get("port"), function () {
console.info("Express server listening on port " + server.address().port);
});
// server.timeout = 1000;
};
start();