72 lines
1.8 KiB
TypeScript
72 lines
1.8 KiB
TypeScript
import { Controller, Get } from '@nestjs/common';
|
|
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
|
|
import { PrismaService } from '../../infrastructure/persistence/prisma/prisma.service';
|
|
import { RedisService } from '../../infrastructure/redis/redis.service';
|
|
import { Public } from '../../shared/guards/jwt-auth.guard';
|
|
|
|
interface HealthStatus {
|
|
status: 'healthy' | 'unhealthy';
|
|
timestamp: string;
|
|
services: {
|
|
database: 'up' | 'down';
|
|
redis: 'up' | 'down';
|
|
};
|
|
}
|
|
|
|
@Public()
|
|
@ApiTags('Health')
|
|
@Controller('health')
|
|
export class HealthController {
|
|
constructor(
|
|
private readonly prisma: PrismaService,
|
|
private readonly redis: RedisService,
|
|
) {}
|
|
|
|
@Get()
|
|
@ApiOperation({ summary: '健康检查' })
|
|
@ApiResponse({ status: 200, description: '服务健康' })
|
|
@ApiResponse({ status: 503, description: '服务不健康' })
|
|
async check(): Promise<HealthStatus> {
|
|
const status: HealthStatus = {
|
|
status: 'healthy',
|
|
timestamp: new Date().toISOString(),
|
|
services: {
|
|
database: 'up',
|
|
redis: 'up',
|
|
},
|
|
};
|
|
|
|
// 检查数据库连接
|
|
try {
|
|
await this.prisma.$queryRaw`SELECT 1`;
|
|
} catch {
|
|
status.services.database = 'down';
|
|
status.status = 'unhealthy';
|
|
}
|
|
|
|
// 检查 Redis 连接
|
|
try {
|
|
await this.redis.getClient().ping();
|
|
} catch {
|
|
status.services.redis = 'down';
|
|
status.status = 'unhealthy';
|
|
}
|
|
|
|
return status;
|
|
}
|
|
|
|
@Get('ready')
|
|
@ApiOperation({ summary: '就绪检查' })
|
|
@ApiResponse({ status: 200, description: '服务就绪' })
|
|
async ready(): Promise<{ ready: boolean }> {
|
|
return { ready: true };
|
|
}
|
|
|
|
@Get('live')
|
|
@ApiOperation({ summary: '存活检查' })
|
|
@ApiResponse({ status: 200, description: '服务存活' })
|
|
async live(): Promise<{ alive: boolean }> {
|
|
return { alive: true };
|
|
}
|
|
}
|