-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhealthCheckController.ts
More file actions
46 lines (35 loc) · 1.06 KB
/
healthCheckController.ts
File metadata and controls
46 lines (35 loc) · 1.06 KB
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
import { Request, Response } from "express";
import * as mongoose from "mongoose";
const mongooseConnectionStates = {
0: "disconnected",
1: "connected",
2: "connecting",
3: "disconnecting"
}
export class HealthCheckController {
public check = (req: Request, res: Response) => {
let isServerHealthy = true
let mongooseState = mongoose.connection.readyState;
if (mongooseState != 1)
isServerHealthy = false
// Add additional checks here
let healthStatus = {
'status': isServerHealthy ? 'healthy' : 'unhealthy',
'services': {
'db_connection': mongooseConnectionStates[mongooseState]
}
}
if (isServerHealthy) {
return res
.status(200)
.type('json')
.send(healthStatus)
} else {
return res
.status(500)
.type('json')
.send(healthStatus)
}
};
}
export const healthCheckController = new HealthCheckController();