Skip to content

Commit

Permalink
Merge pull request #29 from dekiakbar/28-add-rate-limit
Browse files Browse the repository at this point in the history
Add rate limit #28
  • Loading branch information
dekiakbar authored Sep 27, 2022
2 parents ba23df9 + 6dac0ba commit 442db1c
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 12 deletions.
8 changes: 7 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ ALLOWED_FILE_TYPE=.jpeg .jpg .png
MAX_FILE_UPLOAD=5

# max upload size in KB
MAX_UPLOAD_SIZE=8192
MAX_UPLOAD_SIZE=8192

# Throttle config
# ttl : time to live (second)
# limit : the maximum number of requests within the ttl
THROTTLE_TTL=60
THROTTLE_LIMIT=20
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
- ```bash
# Install node modules and dependencies
npm install
```
```

- ```bash
# Run app
Expand Down
74 changes: 70 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@nestjs/core": "^9.0.11",
"@nestjs/platform-express": "^9.0.11",
"@nestjs/swagger": "^6.1.0",
"@nestjs/throttler": "^3.0.0",
"@types/aws-sdk": "^2.7.0",
"@types/sharp": "^0.28.5",
"aws-sdk": "^2.1207.0",
Expand Down
29 changes: 28 additions & 1 deletion src/app.module.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { INestApplication } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { Test, TestingModule } from '@nestjs/testing';
import { AppModule } from './app.module';
import request from 'supertest';
import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';
import { APP_GUARD } from '@nestjs/core';

describe('App Module', () => {
let app: INestApplication;
Expand All @@ -14,8 +16,22 @@ describe('App Module', () => {
isGlobal: true,
envFilePath: ['.env.example'],
}),
ThrottlerModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
ttl: config.get('THROTTLE_TTL'),
limit: config.get('THROTTLE_LIMIT'),
}),
}),
AppModule,
],
providers: [
{
provide: APP_GUARD,
useClass: ThrottlerGuard,
},
],
}).compile();

app = appModule.createNestApplication();
Expand Down Expand Up @@ -76,4 +92,15 @@ describe('App Module', () => {
});
});
});

/**
* close app when test is finished.
* prevent warning :
*
* A worker process has failed to exit gracefully and has been force exited.
* This is likely caused by tests leaking due to improper teardown. Try running with --detectOpenHandles to find leaks.
*/
afterAll(() => {
app.close();
});
});
18 changes: 17 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { RouterModule } from 'nest-router';
import { OptimizeModule } from './optimize/optimize.module';
import { routes } from './routes';
import { ConfigApiModule } from './config-api/config-api.module';
import { StorageModule } from './storage/storage.module';
import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';
import { APP_GUARD } from '@nestjs/core';

@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
}),
ThrottlerModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
ttl: config.get('THROTTLE_TTL'),
limit: config.get('THROTTLE_LIMIT'),
}),
}),
RouterModule.forRoutes(routes),
OptimizeModule,
ConfigApiModule,
StorageModule,
],
providers: [
{
provide: APP_GUARD,
useClass: ThrottlerGuard,
},
],
})
export class AppModule {}
8 changes: 6 additions & 2 deletions src/optimize/optimize.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
BadRequestException,
Body,
Controller,
Post,
Expand All @@ -10,7 +9,12 @@ import {
import { FilesInterceptor } from '@nestjs/platform-express';
import { ImageValidationPipe } from './pipes/image-validation.pipe';
import { OptimizeService } from './optimize.service';
import { ApiBadRequestResponse, ApiBody, ApiConsumes, ApiTags } from '@nestjs/swagger';
import {
ApiBadRequestResponse,
ApiBody,
ApiConsumes,
ApiTags,
} from '@nestjs/swagger';
import { UploadResponseDto } from '../storage/dto/upload-response.dto';

@Controller('optimize')
Expand Down
29 changes: 27 additions & 2 deletions test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,29 @@ import { INestApplication } from '@nestjs/common';
import request from 'supertest';
import { AppModule } from './../src/app.module';
import { StorageService } from './../src/storage/storage.service';
import { ConfigModule } from '@nestjs/config';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';
import { APP_GUARD } from '@nestjs/core';

describe('AppController (e2e)', () => {
let app: INestApplication;
let storageService: StorageService;

beforeEach(async () => {
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: ['.env.example'],
}),
ThrottlerModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
ttl: config.get('THROTTLE_TTL'),
limit: config.get('THROTTLE_LIMIT'),
}),
}),
AppModule,
],
providers: [
Expand All @@ -29,6 +39,10 @@ describe('AppController (e2e)', () => {
};
},
},
{
provide: APP_GUARD,
useClass: ThrottlerGuard,
},
],
}).compile();

Expand Down Expand Up @@ -96,4 +110,15 @@ describe('AppController (e2e)', () => {
});
});
});

/**
* close app when test is finished.
* prevent warning :
*
* This usually means that there are asynchronous operations that weren't stopped in your tests.
* Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.
*/
afterAll(() => {
app.close();
});
});

0 comments on commit 442db1c

Please sign in to comment.