Add health check functionality and type checking to the project
* Introduced HealthCheck class for system health monitoring. * Added HealthCheckApiRoutes for readiness endpoint. * Updated package.json to include TypeScript type checking command.
This commit is contained in:
@@ -8,7 +8,8 @@
|
|||||||
"format": "prettier --write .",
|
"format": "prettier --write .",
|
||||||
"lint": "eslint --max-warnings 0 './src'",
|
"lint": "eslint --max-warnings 0 './src'",
|
||||||
"prelint:fix": "bun run format",
|
"prelint:fix": "bun run format",
|
||||||
"lint:fix": "eslint --fix './src'"
|
"lint:fix": "eslint --fix './src'",
|
||||||
|
"typecheck": "tsc --noEmit"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@eslint/css": "0.14.1",
|
"@eslint/css": "0.14.1",
|
||||||
|
|||||||
23
src/health/HealthCheck.ts
Normal file
23
src/health/HealthCheck.ts
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import { LoggerFactory } from "@techniker-me/logger";
|
||||||
|
import os from 'os';
|
||||||
|
|
||||||
|
export default class HealthCheck {
|
||||||
|
private readonly _logger = LoggerFactory.getLogger('HealthCheck');
|
||||||
|
|
||||||
|
|
||||||
|
public async checkHealth() {
|
||||||
|
return {
|
||||||
|
status: 'ok',
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
uptime: process.uptime(),
|
||||||
|
memory: process.memoryUsage(),
|
||||||
|
cpu: process.cpuUsage(),
|
||||||
|
memoryTotal: process.platform === 'linux' ? os.totalmem() : undefined,
|
||||||
|
memoryFree: process.platform === 'linux' ? os.freemem() : undefined,
|
||||||
|
cpuUsage: process.cpuUsage(),
|
||||||
|
network: process.platform === 'linux' ? os.networkInterfaces() : undefined,
|
||||||
|
os: process.platform,
|
||||||
|
version: process.version
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
39
src/health/HealthCheckRoute.ts
Normal file
39
src/health/HealthCheckRoute.ts
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import type { BunRouteHandler } from '../net/http/BunHttpServer';
|
||||||
|
import { LoggerFactory } from "@techniker-me/logger";
|
||||||
|
import type HealthCheck from './HealthCheck';
|
||||||
|
import type { BunRequest, Server as BunServer } from 'bun';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
export class HealthCheckApiRoutes {
|
||||||
|
private readonly _logger = LoggerFactory.getLogger('HealthCheckRoute');
|
||||||
|
private readonly _healthCheck: HealthCheck;
|
||||||
|
|
||||||
|
constructor(healthCheck: HealthCheck) {
|
||||||
|
this._healthCheck = healthCheck;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getGETRoute(): Record<string, BunRouteHandler> {
|
||||||
|
return {
|
||||||
|
'/_health/readiness': this.readiness.bind(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public getPOSTRoute(): Record<string, BunRouteHandler> {
|
||||||
|
return { }
|
||||||
|
}
|
||||||
|
|
||||||
|
public getPUTRoute(): Record<string, BunRouteHandler> {
|
||||||
|
return { }
|
||||||
|
}
|
||||||
|
|
||||||
|
public getOPTIONSRoute(): Record<string, BunRouteHandler> {
|
||||||
|
return { }
|
||||||
|
}
|
||||||
|
|
||||||
|
private async readiness(_request: BunRequest, _server: BunServer<unknown>): Promise<Response> {
|
||||||
|
const health = await this._healthCheck.checkHealth();
|
||||||
|
|
||||||
|
return new Response(JSON.stringify(health), {status: 200, headers: {'Content-Type': 'application/json'}});
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user