-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhealth.ts
More file actions
73 lines (63 loc) · 1.71 KB
/
health.ts
File metadata and controls
73 lines (63 loc) · 1.71 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
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
import * as http from "http";
import pino from "pino";
const log = pino({ name: "specsync-health" });
export class HealthChecker {
private startTime: number;
private requestCount: number;
constructor() {
this.startTime = Date.now();
this.requestCount = 0;
}
/**
* Liveness + readiness HTTP server (same port for small deployments).
* GET /health — process is up (liveness).
* GET /ready — app finished startup (readiness); extend when adding DB/queues.
*/
createHealthEndpoint(port: number): http.Server {
const server = http.createServer((req, res) => {
this.requestCount++;
if (req.url === "/health") {
const uptime = Date.now() - this.startTime;
res.writeHead(200, { "Content-Type": "application/json" });
res.end(
JSON.stringify({
status: "healthy",
uptime,
requests: this.requestCount,
timestamp: new Date().toISOString(),
})
);
return;
}
if (req.url === "/ready") {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(
JSON.stringify({
status: "ready",
timestamp: new Date().toISOString(),
})
);
return;
}
res.writeHead(404);
res.end("Not Found");
});
server.listen(port, () => {
log.info({ port }, "Health endpoints listening");
});
return server;
}
getStatus(): {
status: string;
uptime: number;
requests: number;
timestamp: string;
} {
return {
status: "healthy",
uptime: Date.now() - this.startTime,
requests: this.requestCount,
timestamp: new Date().toISOString(),
};
}
}