-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathhealthcheck-controller.js
56 lines (46 loc) · 1.79 KB
/
healthcheck-controller.js
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
import Boom from '@hapi/boom';
import { databaseConnections } from '../../../../db/database-connections.js';
import packageJSON from '../../../../package.json' with { type: 'json' };
import * as network from '../../../identity-access-management/infrastructure/utils/network.js';
import { config } from '../../config.js';
import { redisMonitor } from '../../infrastructure/utils/redis-monitor.js';
const get = function (request) {
return {
name: packageJSON.name,
version: packageJSON.version,
description: packageJSON.description,
environment: config.environment,
'container-version': process.env.CONTAINER_VERSION,
'container-app-name': process.env.APP,
'current-lang': request.i18n.getLocale(),
};
};
const checkDbStatus = async function () {
try {
await databaseConnections.checkStatuses();
return { message: 'Connection to databases ok' };
} catch (error) {
throw Boom.serverUnavailable(`Connection to databases failed: ${error.message}`);
}
};
const checkRedisStatus = async function () {
try {
await redisMonitor.ping();
return { message: 'Connection to Redis ok' };
} catch {
throw Boom.serverUnavailable('Connection to Redis failed');
}
};
const checkForwardedOriginStatus = async function (request, h) {
let forwardedOrigin;
try {
// network.getForwardedOrigin throws ForwardedOriginError which maps to a HTTP status code 400,
// but for monitoring purpose we want this error to produce a 500.
forwardedOrigin = network.getForwardedOrigin(request.headers);
} catch {
return h.response('Obtaining Forwarded Origin failed').code(500);
}
return h.response(forwardedOrigin).code(200);
};
const healthcheckController = { get, checkDbStatus, checkRedisStatus, checkForwardedOriginStatus };
export { healthcheckController };