| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- // apps/box-app-api/src/rabbitmq/rabbitmq-status.controller.ts
- import { Controller, Get, UseGuards } from '@nestjs/common';
- import { ConfigService } from '@nestjs/config';
- import { RabbitmqPublisherService } from './rabbitmq-publisher.service';
- /**
- * Internal-only endpoint for RabbitMQ publisher status monitoring.
- * Guarded by environment check to prevent public access in production.
- */
- @Controller('api/v1/internal/rabbitmq')
- export class RabbitmqStatusController {
- constructor(
- private readonly publisher: RabbitmqPublisherService,
- private readonly config: ConfigService,
- ) {}
- /**
- * GET /api/v1/internal/rabbitmq/status
- * Returns circuit breaker status and connection health
- *
- * Only accessible when NODE_ENV !== 'production' or ENABLE_INTERNAL_ENDPOINTS=true
- */
- @Get('status')
- getStatus(): any {
- // Environment guard - prevent access in production unless explicitly enabled
- const nodeEnv = this.config.get<string>('NODE_ENV');
- const enableInternalEndpoints = this.config.get<string>(
- 'ENABLE_INTERNAL_ENDPOINTS',
- );
- if (nodeEnv === 'production' && enableInternalEndpoints !== 'true') {
- return {
- error: 'Internal endpoints are disabled in production',
- hint: 'Set ENABLE_INTERNAL_ENDPOINTS=true to enable',
- };
- }
- const status = this.publisher.getCircuitStatus();
- return {
- timestamp: new Date().toISOString(),
- rabbitmq: {
- circuitBreaker: {
- state: status.state,
- failureCount: status.failureCount,
- successCount: status.successCount,
- nextAttemptTime: status.nextAttemptTime
- ? new Date(status.nextAttemptTime).toISOString()
- : null,
- },
- connection: {
- hasConnection: status.hasConnection,
- hasChannel: status.hasChannel,
- isReconnecting: status.isReconnecting,
- },
- health:
- status.hasConnection && status.hasChannel && status.state === 'CLOSED'
- ? 'healthy'
- : status.isReconnecting
- ? 'reconnecting'
- : 'unhealthy',
- },
- };
- }
- }
|