diff --git a/package.json b/package.json index a6a9c3b..e9969ba 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/health/HealthCheck.ts b/src/health/HealthCheck.ts new file mode 100644 index 0000000..b729ef4 --- /dev/null +++ b/src/health/HealthCheck.ts @@ -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 + } + } +} \ No newline at end of file diff --git a/src/health/HealthCheckRoute.ts b/src/health/HealthCheckRoute.ts new file mode 100644 index 0000000..6618413 --- /dev/null +++ b/src/health/HealthCheckRoute.ts @@ -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 { + return { + '/_health/readiness': this.readiness.bind(this) + } + } + + public getPOSTRoute(): Record { + return { } + } + + public getPUTRoute(): Record { + return { } + } + + public getOPTIONSRoute(): Record { + return { } + } + + private async readiness(_request: BunRequest, _server: BunServer): Promise { + const health = await this._healthCheck.checkHealth(); + + return new Response(JSON.stringify(health), {status: 200, headers: {'Content-Type': 'application/json'}}); + } +} \ No newline at end of file