diff --git a/backend/src/health.controller.ts b/backend/src/health.controller.ts index adadf64..10833c4 100644 --- a/backend/src/health.controller.ts +++ b/backend/src/health.controller.ts @@ -1,28 +1,44 @@ -import { Controller, Get } from "@nestjs/common"; +import { CACHE_MANAGER } from "@nestjs/cache-manager"; +import { Controller, Get, Inject } from "@nestjs/common"; +import { Cache } from "cache-manager"; import { sql } from "drizzle-orm"; import { DatabaseService } from "./database/database.service"; @Controller("health") export class HealthController { - constructor(private readonly databaseService: DatabaseService) {} + constructor( + private readonly databaseService: DatabaseService, + @Inject(CACHE_MANAGER) private cacheManager: Cache, + ) {} @Get() async check() { + const health: any = { + status: "ok", + timestamp: new Date().toISOString(), + }; + try { // Check database connection await this.databaseService.db.execute(sql`SELECT 1`); - return { - status: "ok", - database: "connected", - timestamp: new Date().toISOString(), - }; + health.database = "connected"; } catch (error) { - return { - status: "error", - database: "disconnected", - message: error.message, - timestamp: new Date().toISOString(), - }; + health.status = "error"; + health.database = "disconnected"; + health.databaseError = error.message; } + + try { + // Check Redis connection via cache-manager + // We try to set a temporary key to verify the connection + await this.cacheManager.set("health-check", "ok", 1000); + health.redis = "connected"; + } catch (error) { + health.status = "error"; + health.redis = "disconnected"; + health.redisError = error.message; + } + + return health; } }