-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.ts
109 lines (89 loc) · 2.75 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
import express from 'express';
import helmet from 'helmet';
import path from 'path';
import prometheus from '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() as any);
server.use(
helmet({
contentSecurityPolicy: false,
}) as any
);
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);
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.use(setupProxy(authClient, issuer));
server.get('/health/isAlive', (req, res) => {
res.sendStatus(200);
});
server.get('/health/isReady', (req, res) => {
res.sendStatus(200);
});
server.get('/actuator/metrics', (req, res) => {
res.set('Content-Type', prometheus.register.contentType);
res.end(prometheus.register.metrics());
});
server.get(
['*', '/syfooversikt/?', /^\/syfooversikt\/(?!(resources|img)).*$/],
[nocache, redirectIfUnauthorized],
(req: express.Request, res: express.Response) => {
res.sendFile(HTML_FILE);
httpRequestDurationMicroseconds.labels(req.path).observe(10);
}
);
const port = process.env.PORT || 8080;
server.listen(port, () => {
console.log(`App listening on port: ${port}`);
});
};
setupServer();