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:
2025-11-21 03:03:41 -05:00
parent ab34b614dd
commit f66266b218
3 changed files with 64 additions and 1 deletions

View File

@@ -8,7 +8,8 @@
"format": "prettier --write .",
"lint": "eslint --max-warnings 0 './src'",
"prelint:fix": "bun run format",
"lint:fix": "eslint --fix './src'"
"lint:fix": "eslint --fix './src'",
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"@eslint/css": "0.14.1",

23
src/health/HealthCheck.ts Normal file
View 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
}
}
}

View 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'}});
}
}