generated from hmcts/expressjs-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.ts
105 lines (83 loc) · 3.21 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
import * as bodyParser from "body-parser";
import cookieParser from "cookie-parser";
import express from "express";
import * as path from "path";
import { RouterFinder } from "./api/router/routerFinder";
import { HttpError } from "./api/model/httpError";
import csrf from "csurf";
import * as propertiesVolume from "@hmcts/properties-volume";
import { swaggerDocument } from "./api/swagger";
import * as appInsights from "applicationinsights";
import * as swaggerUi from "swagger-ui-express";
import { Express, Logger } from "@hmcts/nodejs-logging";
import { EmWebPubEventHandlerOptions } from "./api/em-web-pub-event-handler-options";
import { WebPubSubEventHandler } from "@azure/web-pubsub-express";
const healthcheck = require("./api/routes/health");
const helmet = require("helmet");
const noCache = require("nocache");
const config = require("config");
const rateLimit = require("express-rate-limit");
const env = process.env.NODE_ENV || "development";
propertiesVolume.addTo(config);
const APP_INSIGHTS_KEY = config.secrets ? config.secrets["em-icp"]["AppInsightsInstrumentationKey"] : undefined;
const primaryConnectionstring = config.secrets ? config.secrets["em-icp"]["em-icp-web-pubsub-primary-connection-string"] : undefined;
const logger = Logger.getLogger("app");
export const app = express();
app.locals.ENV = env;
const limiter = rateLimit({
windowMs: config.rateLimit.time,
max: config.rateLimit.max,
});
if (APP_INSIGHTS_KEY) {
appInsights.setup(APP_INSIGHTS_KEY)
.setAutoCollectConsole(true, true)
.setDistributedTracingMode(appInsights.DistributedTracingModes.AI_AND_W3C)
.setSendLiveMetrics(true)
.start();
appInsights.defaultClient.context.tags[appInsights.defaultClient.context.keys.cloudRole] = "em-icp";
}
const appInsightClient = appInsights.defaultClient;
const webPubSubOptions = new EmWebPubEventHandlerOptions(primaryConnectionstring, appInsightClient);
const handler = new WebPubSubEventHandler("hub", {
path: "/eventhandler",
handleConnect: webPubSubOptions.handleConnect,
handleUserEvent: webPubSubOptions.handleUserEvent,
onConnected: webPubSubOptions.onConnected,
onDisconnected: webPubSubOptions.onDisconnected,
});
app.use(handler.getMiddleware());
app.use(limiter);
app.use(Express.accessLogger());
app.use("/swagger", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.use(noCache());
app.use(helmet());
app.use(helmet.xssFilter({ setOnOldIE: true }));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use((req, res, next) => {
res.setHeader(
"Cache-Control",
"no-cache, max-age=0, must-revalidate, no-store",
);
next();
});
app.use("/", RouterFinder.findAll(path.join(__dirname, "api/routes")));
app.use((req, res) => {
res.status(404);
res.send("not-found");
});
app.use((err: HttpError, req: express.Request, res: express.Response) => {
logger.error(`${err.stack || err}`);
res.locals.message = err.message;
res.locals.error = env === "development" ? err : {};
res.status(err.status || 500);
res.send("error");
});
if (config.app.useCSRFProtection === "true") {
app.use(csrf(), (req, res, next) => {
res.locals.csrfToken = req.csrfToken();
next();
});
}
app.use("/health", healthcheck);