-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.ts
121 lines (103 loc) · 2.93 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
112
113
114
115
116
117
118
119
120
121
import express = require("express");
import helmet = require("helmet");
import path = require("path");
import prometheus = require("prom-client");
import { getOpenIdClient, getOpenIdIssuer } from "./server/authUtils";
import { setupProxy } from "./server/proxy";
import { setupSession } from "./server/session";
import unleash = require("./server/unleash");
// 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));
const DIST_DIR = path.join(__dirname, "dist");
const HTML_FILE = path.join(DIST_DIR, "index.html");
server.use("/static", express.static(DIST_DIR));
server.get(
"/unleash/toggles",
redirectIfUnauthorized,
(req: express.Request, res: express.Response) => {
const togglesResponse = unleash.getToggles(
req.query.veilederId,
req.query.enhetId
);
res.status(200).send(togglesResponse);
}
);
server.get(
[
"/",
"/sykefravaer",
"/sykefravaer/*",
/^\/sykefravaer\/(?!(resources)).*$/,
],
[nocache, redirectIfUnauthorized],
(req: express.Request, res: express.Response) => {
res.sendFile(HTML_FILE);
httpRequestDurationMicroseconds.labels(req.route.path).observe(10);
}
);
server.get(
"/actuator/metrics",
(req: express.Request, res: express.Response) => {
res.set("Content-Type", prometheus.register.contentType);
res.end(prometheus.register.metrics());
}
);
server.get(
"/health/isAlive",
(req: express.Request, res: express.Response) => {
res.sendStatus(200);
}
);
server.get(
"/health/isReady",
(req: express.Request, res: express.Response) => {
res.sendStatus(200);
}
);
const port = 8080;
server.listen(port, () => {
console.log(`App listening on port: ${port}`);
});
};
setupServer();