-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
111 lines (91 loc) · 2.69 KB
/
server.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
import express from "express";
import helmet from "helmet";
import path from "path";
import prometheus from "prom-client";
import * as Config from "./server/config";
import { getOpenIdClient, getOpenIdIssuer } from "./server/authUtils";
import { setupProxy } from "./server/proxy";
import { setupSession } from "./server/session";
// Prometheus metrics
const collectDefaultMetrics = prometheus.collectDefaultMetrics;
collectDefaultMetrics({ timeout: 5000 });
const httpRequestDurationMicroseconds = new prometheus.Histogram({
name: "http_request_duration_ms",
help: "Duration of HTTP requests in ms",
labelNames: ["route"],
// buckets for response time from 0.1ms to 500ms
buckets: [0.1, 5, 15, 50, 100, 200, 300, 400, 500],
});
const server = express();
server.use(express.json());
server.use(
helmet({
contentSecurityPolicy: false,
})
);
const nocache = (
req: express.Request,
res: express.Response,
next: express.NextFunction
) => {
res.header("Cache-Control", "private, no-cache, no-store, must-revalidate");
res.header("Expires", "-1");
res.header("Pragma", "no-cache");
next();
};
const redirectIfUnauthorized = async (
req: express.Request,
res: express.Response,
next: express.NextFunction
) => {
if (req.headers["authorization"]) {
next();
} else {
res.redirect(`/oauth2/login?redirect=${req.originalUrl}`);
}
};
const setupServer = async () => {
setupSession(server);
const issuer = await getOpenIdIssuer();
const authClient = await getOpenIdClient(issuer);
server.use(setupProxy(authClient, issuer));
server.get("/actuator/metrics", (req, res) => {
res.set("Content-Type", prometheus.register.contentType);
res.end(prometheus.register.metrics());
});
server.get("/health/isAlive", (req, res) => {
res.sendStatus(200);
});
server.get("/health/isReady", (req, res) => {
res.sendStatus(200);
});
const DIST_DIR = path.join(__dirname, "dist");
const HTML_FILE = path.join(DIST_DIR, "index.html");
server.use(
"/syfomoteoversikt",
express.static(path.join(__dirname, "..", "build"))
);
server.use(
"/syfomoteoversikt/img",
express.static(path.resolve(__dirname, "img"))
);
server.get(
[
"/",
"/syfomoteoversikt",
"/syfomoteoversikt/*",
/^\/syfomoteoversikt\/(?!(resources|img)).*$/,
],
[nocache, redirectIfUnauthorized],
(req: express.Request, res: express.Response) => {
res.sendFile(HTML_FILE);
httpRequestDurationMicroseconds.labels(req.route.path).observe(10);
}
);
server.use("/static", express.static(DIST_DIR));
const port = 8080;
server.listen(port, () => {
console.log(`App listening on port: ${port}`);
});
};
setupServer();