rabbitmq-status.controller.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // apps/box-app-api/src/rabbitmq/rabbitmq-status.controller.ts
  2. import { Controller, Get, UseGuards } from '@nestjs/common';
  3. import { ConfigService } from '@nestjs/config';
  4. import { RabbitmqPublisherService } from './rabbitmq-publisher.service';
  5. /**
  6. * Internal-only endpoint for RabbitMQ publisher status monitoring.
  7. * Guarded by environment check to prevent public access in production.
  8. */
  9. @Controller('api/v1/internal/rabbitmq')
  10. export class RabbitmqStatusController {
  11. constructor(
  12. private readonly publisher: RabbitmqPublisherService,
  13. private readonly config: ConfigService,
  14. ) {}
  15. /**
  16. * GET /api/v1/internal/rabbitmq/status
  17. * Returns circuit breaker status and connection health
  18. *
  19. * Only accessible when NODE_ENV !== 'production' or ENABLE_INTERNAL_ENDPOINTS=true
  20. */
  21. @Get('status')
  22. getStatus(): any {
  23. // Environment guard - prevent access in production unless explicitly enabled
  24. const nodeEnv = this.config.get<string>('NODE_ENV');
  25. const enableInternalEndpoints = this.config.get<string>(
  26. 'ENABLE_INTERNAL_ENDPOINTS',
  27. );
  28. if (nodeEnv === 'production' && enableInternalEndpoints !== 'true') {
  29. return {
  30. error: 'Internal endpoints are disabled in production',
  31. hint: 'Set ENABLE_INTERNAL_ENDPOINTS=true to enable',
  32. };
  33. }
  34. const status = this.publisher.getCircuitStatus();
  35. return {
  36. timestamp: new Date().toISOString(),
  37. rabbitmq: {
  38. circuitBreaker: {
  39. state: status.state,
  40. failureCount: status.failureCount,
  41. successCount: status.successCount,
  42. nextAttemptTime: status.nextAttemptTime
  43. ? new Date(status.nextAttemptTime).toISOString()
  44. : null,
  45. },
  46. connection: {
  47. hasConnection: status.hasConnection,
  48. hasChannel: status.hasChannel,
  49. isReconnecting: status.isReconnecting,
  50. },
  51. health:
  52. status.hasConnection && status.hasChannel && status.state === 'CLOSED'
  53. ? 'healthy'
  54. : status.isReconnecting
  55. ? 'reconnecting'
  56. : 'unhealthy',
  57. },
  58. };
  59. }
  60. }