import { A_Component, A_CONCEPT_ENV, A_Inject } from "@adaas/a-concept"; import { A_ServerRouter } from "@adaas/a-server/router/A-ServerRouter.component"; import { A_Request } from "@adaas/a-server/request/A-Request.entity"; import { A_Response } from "@adaas/a-server/response/A-Response.entity"; import { A_Config } from "@adaas/a-utils/a-config"; import { A_ServerLogger } from "@adaas/a-server/logger/A-ServerLogger.component"; import { A_HttpServerError } from "../../lib/A-Server/A-HttpServer.error"; import fs from "fs"; import path from "path"; export class A_ServerHealthMonitor extends A_Component { // ======================================================= // ================ Method Definition===================== // ======================================================= @A_ServerRouter.Get({ path: '/', prefix: 'health', version: 'v1', }) async get( @A_Inject(A_Config) config: A_Config<['VERSION_PATH', 'EXPOSED_PROPERTIES']>, @A_Inject(A_Request) request: A_Request, @A_Inject(A_Response) response: A_Response, @A_Inject(A_ServerLogger) logger: A_ServerLogger ): Promise { const rootFolder = config.get('A_CONCEPT_ROOT_FOLDER') || A_CONCEPT_ENV.A_CONCEPT_ROOT_FOLDER || process.cwd(); const pkgPath = path.join(rootFolder, 'package.json'); let packageJSON: Record; try { packageJSON = JSON.parse(fs.readFileSync(pkgPath, 'utf-8')); } catch { throw new A_HttpServerError({ status: 500, description: `Could not read package.json at "${pkgPath}"`, }); } const exposedProperties: Array = config.get('EXPOSED_PROPERTIES')?.split(',') || [ 'name', 'version', 'description', ]; exposedProperties.forEach(prop => response.add(prop, packageJSON[prop])); } }