File size: 3,776 Bytes
eacfb4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import {
  Controller,
  Post,
  Body,
  Get,
  HttpCode,
  HttpStatus,
  Logger,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBody } from '@nestjs/swagger';
import { DirectService } from './direct.service';
import {
  DirectDecodeHashDto,
  DirectDecodeHashResponseDto,
  DirectHealthStatsDto,
} from './direct.dto';

@ApiTags('direct')
@Controller('direct')
export class DirectController {
  private readonly logger = new Logger(DirectController.name);

  constructor(private readonly directService: DirectService) {}

  @Post('decode')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({
    summary: 'Decode a hash using direct connection',
    description:
      'Decode a hash string using direct connection to the API without proxies',
  })
  @ApiResponse({
    status: 200,
    description: 'Hash decoded successfully',
    type: DirectDecodeHashResponseDto,
  })
  @ApiResponse({
    status: 400,
    description: 'Invalid hash format or empty hash',
  })
  @ApiResponse({
    status: 404,
    description: 'Hash not found',
  })
  @ApiResponse({
    status: 429,
    description: 'Rate limit exceeded',
  })
  @ApiResponse({
    status: 500,
    description: 'Internal server error',
  })
  @ApiBody({ type: DirectDecodeHashDto })
  async decodeHash(
    @Body() directDecodeHashDto: DirectDecodeHashDto,
  ): Promise<DirectDecodeHashResponseDto> {
    this.logger.log(
      `πŸ“ Direct decode request received for hash: ${directDecodeHashDto.hash.substring(0, 8)}...`,
    );

    try {
      const result = await this.directService.decodeHash(directDecodeHashDto);
      this.logger.log(
        `βœ… Direct decode successful for hash: ${directDecodeHashDto.hash.substring(0, 8)}... (${result.responseTime}ms)`,
      );
      return result;
    } catch (error) {
      this.logger.error(
        `❌ Direct decode failed for hash: ${directDecodeHashDto.hash.substring(0, 8)}... - ${error.message}`,
      );
      throw error;
    }
  }

  @Get('health')
  @ApiOperation({
    summary: 'Get direct service health statistics',
    description:
      'Retrieve comprehensive health and performance statistics for the direct decoder service',
  })
  @ApiResponse({
    status: 200,
    description: 'Health statistics retrieved successfully',
    type: DirectHealthStatsDto,
  })
  getHealthStats(): DirectHealthStatsDto {
    this.logger.log('πŸ“Š Direct service health stats requested');
    return this.directService.getHealthStats();
  }

  @Post('reset-stats')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({
    summary: 'Reset service statistics',
    description: 'Reset all performance statistics for the direct service',
  })
  @ApiResponse({
    status: 200,
    description: 'Statistics reset successfully',
  })
  resetStats(): { message: string; previousStats: any } {
    this.logger.log('πŸ”„ Resetting direct service statistics');
    return this.directService.resetStats();
  }

  @Get('status')
  @ApiOperation({
    summary: 'Get service status',
    description:
      'Get current status and basic information about the direct service',
  })
  @ApiResponse({
    status: 200,
    description: 'Service status retrieved successfully',
  })
  getStatus(): {
    service: string;
    status: string;
    connectionMethod: string;
    features: string[];
    timestamp: string;
  } {
    this.logger.log('πŸ“‹ Direct service status requested');

    return {
      service: 'DirectService',
      status: 'operational',
      connectionMethod: 'direct',
      features: [
        'Direct API connection',
        'No proxy dependencies',
        'Built-in rate limiting',
        'Response time tracking',
        'Automatic retries',
        'SSL/TLS support',
      ],
      timestamp: new Date().toISOString(),
    };
  }
}