import { CACHE_MANAGER } from "@nestjs/cache-manager"; import { Controller, Get, Inject } from "@nestjs/common"; import type { 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, @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`); health.database = "connected"; } catch (error) { 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; } }