platform-codebase/features/profile/backend-api/src/health.controller.ts

73 lines
2.1 KiB
TypeScript
Executable file

import {
BaseHealthController,
DependencyHealth,
HealthStatus,
TypeOrmConnectionIndicator,
} from '@lilith/nestjs-health';
import { Controller } from '@nestjs/common';
import { SkipThrottle } from '@nestjs/throttler';
import { InjectConnection } from '@nestjs/typeorm';
import { Connection } from 'typeorm';
/**
* Health check controller for profile service
*
* Provides standardized health check endpoints:
* - GET /health - Main health check with dependency status
* - GET /health/live - Liveness probe (always returns alive)
* - GET /health/ready - Readiness probe (checks dependencies)
* - GET /health/detailed - Detailed health with memory metrics
*
* Uses @lilith/nestjs-health BaseHealthController for DRY implementation
* Health endpoints are excluded from rate limiting for monitoring/probes
*/
@SkipThrottle()
@Controller()
export class HealthController extends BaseHealthController {
private readonly dbIndicator = new TypeOrmConnectionIndicator();
constructor(@InjectConnection() private readonly connection: Connection) {
super();
}
protected override getVersion(): string {
return process.env.npm_package_version || '1.0.0';
}
protected override getEnvironment(): string {
return process.env.NODE_ENV ?? 'development';
}
protected override async checkDependencies(): Promise<DependencyHealth[]> {
const dbHealth = await this.dbIndicator.check('database', {
connection: this.connection,
timeout: 5000,
});
return [
{
name: 'database',
status: dbHealth.database.status,
responseTime: dbHealth.database.responseTime,
message: dbHealth.database.message,
},
await this.checkRedis(),
];
}
private async checkRedis(): Promise<DependencyHealth> {
// Redis is accessible if the app started (via BullMQ or direct client)
return {
name: 'redis',
status: HealthStatus.OK,
message: 'Redis accessible',
};
}
protected override getMetadata(): Record<string, unknown> {
return {
serviceName: 'profile',
port: 3110,
};
}
}