Skip to content

Commit

Permalink
feat: added healthcheck endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mckrava committed Apr 12, 2024
1 parent ef7f2d9 commit e74b169
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@nestjs/graphql": "^11.0.6",
"@nestjs/platform-express": "^9.4.2",
"@nestjs/schedule": "^4.0.0",
"@nestjs/throttler": "^5.1.2",
"@nestjs/typeorm": "^9.0.1",
"@polkadot/util-crypto": "^12.4.2",
"@subql/apollo-links": "^1.2.5",
Expand Down
7 changes: 7 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { ScheduleModule } from '@nestjs/schedule';
import { AccountSyncSchedulerModule } from './modules/accountSyncScheduler/accountSyncScheduler.module';
import { AggregatorStateManagerModule } from './modules/aggregatorStateManager/aggregatorStateManager.module';
import { RedisManagerModule } from './modules/redisManagerModule/redisManager.module';
import { ThrottlerModule } from '@nestjs/throttler';

dotenv.config();

Expand All @@ -40,6 +41,12 @@ dotenv.config();
}),
TypeOrmModule.forRootAsync(config.typeOrmModuleForRoot),
ScheduleModule.forRoot(),
ThrottlerModule.forRoot([
{
ttl: 1000,
limit: 2,
},
]),
DependencyServiceModule,
QueueProcessorModule,
ApiGatewayModule,
Expand Down
18 changes: 18 additions & 0 deletions src/common/filters/throttler-exception.filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ExceptionFilter, Catch, ArgumentsHost, HttpException, HttpStatus } from '@nestjs/common';
import { ThrottlerException } from '@nestjs/throttler';

@Catch(ThrottlerException)
export class ThrottlerExceptionFilter implements ExceptionFilter {
catch(exception: ThrottlerException, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse();

response
.status(HttpStatus.TOO_MANY_REQUESTS) // 429 status code
.json({
statusCode: HttpStatus.TOO_MANY_REQUESTS,
timestamp: new Date().toISOString(),
message: 'Too Many Requests',
});
}
}
2 changes: 2 additions & 0 deletions src/modules/apiGateway/apiGateway.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Transaction } from '../entities/transaction/entities/transaction.entity
import { TransactionsHistoryResolver } from './gql/transactionsHistory.resolver';
import { AccountAggregationFlowProducer } from '../queueProcessor/services/producers/accountAggregationFlow.producer';
import { SubIdAggregatorQueueName } from '../../constants/queues';
import { RestHealthcheckController } from './rest/healthcheck/restHealthcheck.controller';

@Module({
imports: [
Expand Down Expand Up @@ -46,6 +47,7 @@ import { SubIdAggregatorQueueName } from '../../constants/queues';
TransactionsHistoryResolver,
AccountAggregationFlowProducer,
],
controllers: [RestHealthcheckController],
exports: [ApiGatewayService],
})
export class ApiGatewayModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Controller, Get, UseFilters, UseGuards } from '@nestjs/common';
import { Throttle, ThrottlerGuard } from '@nestjs/throttler';
import { ThrottlerExceptionFilter } from 'src/common/filters/throttler-exception.filter';

@UseFilters(ThrottlerExceptionFilter)
@Controller('healthcheck')
export class RestHealthcheckController {
@Get('status')
@UseGuards(ThrottlerGuard)
status(): { operational: boolean } {
return { operational: true };
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2119,6 +2119,11 @@
dependencies:
tslib "2.5.3"

"@nestjs/throttler@^5.1.2":
version "5.1.2"
resolved "https://registry.yarnpkg.com/@nestjs/throttler/-/throttler-5.1.2.tgz#dc65634153c8b887329b1cc6061db2e556517dcb"
integrity sha512-60MqhSLYUqWOgc38P6C6f76JIpf6mVjly7gpuPBCKtVd0p5e8Fq855j7bJuO4/v25vgaOo1OdVs0U1qtgYioGw==

"@nestjs/typeorm@^9.0.1":
version "9.0.1"
resolved "https://registry.yarnpkg.com/@nestjs/typeorm/-/typeorm-9.0.1.tgz#f78bfc00e71731ea860288e4a03830107daf3d9c"
Expand Down

0 comments on commit e74b169

Please sign in to comment.