m-decode / src /direct /direct.controller.ts
GodfreyOwino's picture
adding direct service
eacfb4b
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(),
};
}
}