Skip to content

Commit ae76de0

Browse files
committed
feat(app): implement throttling guard for enhanced request management
1 parent ab1cca6 commit ae76de0

6 files changed

Lines changed: 25 additions & 23 deletions

File tree

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ PORT=3000
33
NODE_ENV=development
44
COOKIE_SECRET=same-serious-secret
55
CORS_ALLOWED_ORIGINS=http://localhost:3000,http://127.0.0.1:3000
6+
SKIP_THROTTLE=false
67

78
# --- POSTGRES ---
89
DB_USERNAME=admin

libs/bootstrap/src/bootstrap.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { Logger } from '@nestjs/common';
22
import { ConfigService } from '@nestjs/config';
33
import { NestFactory } from '@nestjs/core';
4-
import { setupThrottler } from './setups/throttler';
5-
import { DEFAULT_THROTTLER_OPTIONS } from './configs/throttler';
64
import { setupCors, setupSwagger } from './setups';
75
import { FastifyAdapter, type NestFastifyApplication } from '@nestjs/platform-fastify';
86
import type { BootstrapOptions } from './interfaces/options.interface';
@@ -30,17 +28,10 @@ export async function bootstrapApp(options: BootstrapOptions) {
3028
setupApp,
3129
useCookieParser = true,
3230
useCors = true,
33-
throttlerOptions = DEFAULT_THROTTLER_OPTIONS,
3431
swaggerOptions,
3532
} = options;
3633

37-
let rootModule = appModule;
38-
39-
if (throttlerOptions) {
40-
rootModule = setupThrottler(rootModule, throttlerOptions);
41-
}
42-
43-
const app = await NestFactory.create<NestFastifyApplication>(rootModule, adapter, {
34+
const app = await NestFactory.create<NestFastifyApplication>(appModule, adapter, {
4435
rawBody: true,
4536
});
4637
const logger = new Logger(serviceName[0].toUpperCase() + serviceName.slice(1));

libs/bootstrap/src/configs/throttler.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

libs/config/src/config.schema.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ export const ConfigSchema = z.object({
99
PORT: z.coerce.number().default(3000),
1010
NODE_ENV: z.enum(['development', 'production', 'test']).default('development'),
1111
COOKIE_SECRET: z.string({ error: 'COOKIE_SECRET is missing' }),
12+
SKIP_THROTTLE: z
13+
.preprocess((value) => {
14+
if (typeof value === 'string') {
15+
return value.toLowerCase() === 'true';
16+
}
17+
return value;
18+
}, z.boolean())
19+
.default(false),
1220
DB_USERNAME: z.string({ error: 'DB_USERNAME is missing' }),
1321
DB_PASSWORD: z.string({ error: 'DB_PASSWORD is missing' }),
1422
DB_DATABASE: z.string({ error: 'DB_DATABASE is missing' }),

pnpm-lock.yaml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/modules/app/app.module.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ConfigModule } from '@libs/config';
33
import { DatabaseModule } from '@libs/database';
44
import { ConfigService } from '@nestjs/config';
55
import * as schema from '../../shared/entities';
6-
import { APP_FILTER, APP_PIPE } from '@nestjs/core';
6+
import { APP_FILTER, APP_GUARD, APP_PIPE } from '@nestjs/core';
77
import { ZodValidationPipe } from 'nestjs-zod';
88
import { PrometheusModule } from '@willsoto/nestjs-prometheus';
99
import { HealthModule } from '@libs/health';
@@ -18,10 +18,20 @@ import { MailAdapter } from '@shared/adapters/mail';
1818
import { MigrationService } from '@shared/migration';
1919
import { TeamsModule } from '../teams';
2020
import { ProjectsModule } from '../projects';
21+
import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';
2122

2223
@Module({
2324
imports: [
2425
ConfigModule,
26+
ThrottlerModule.forRootAsync({
27+
inject: [ConfigService],
28+
useFactory: (cfg: ConfigService) => [
29+
{
30+
ttl: 60000,
31+
limit: cfg.get<boolean>('SKIP_THROTTLE') ? 999999 : 100,
32+
},
33+
],
34+
}),
2535
PrometheusModule.registerAsync({
2636
useFactory: () => ({
2737
path: 'dump',
@@ -61,6 +71,10 @@ import { ProjectsModule } from '../projects';
6171
HealthModule.register('gateway'),
6272
],
6373
providers: [
74+
{
75+
provide: APP_GUARD,
76+
useClass: ThrottlerGuard,
77+
},
6478
MigrationService,
6579
{
6680
provide: 'IMailPort',

0 commit comments

Comments
 (0)