Skip to content

Commit

Permalink
feat: add rate limit for the requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Keemluvr committed Jun 17, 2024
1 parent 44226c2 commit ba93da7
Show file tree
Hide file tree
Showing 7 changed files with 265 additions and 2 deletions.
191 changes: 190 additions & 1 deletion package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"dotenv": "^16.4.5",
"express-rate-limit": "^7.3.1",
"helmet": "^7.1.0",
"pg": "^8.11.5",
"pg-hstore": "^2.3.4",
"pino": "^9.2.0",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1",
"sequelize": "^6.37.3",
Expand Down
9 changes: 8 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,24 @@ import { DatabaseModule } from './core/database/database.module';
import { CorsMiddleware } from './core/middleware/cors.middleware';
import { HelmetMiddleware } from './core/middleware/helmet.middleware';
import { LoggerMiddleware } from './core/middleware/logger.middleware';
import { RateLimitMiddleware } from './core/middleware/rate-limit.middleware';

@Module({
imports: [ConfigModule, DatabaseModule, AuthModule, UserModule],
controllers: [AppController, UserController],
providers: [AppService, AuthService],
exports: [AppService],
})
export class AppModule implements NestModule {
constructor() {}

configure(consumer: MiddlewareConsumer) {
const middlewares = [CorsMiddleware, HelmetMiddleware, LoggerMiddleware];
const middlewares = [
CorsMiddleware,
HelmetMiddleware,
LoggerMiddleware,
RateLimitMiddleware,
];

consumer
.apply(...middlewares)
Expand Down
34 changes: 34 additions & 0 deletions src/app.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
import { Injectable } from '@nestjs/common';
import pino from 'pino';

const logger = pino();

class LogData {
public message?: string;
public data?: any;
}

@Injectable()
export class AppService {
getHello(): string {
return 'Application is up and running! ✅';
}

private writeLog(logMethod: string, logData: LogData) {
logData = logData || { message: '', data: {} };
(logData as any).timeLocal = new Date().toISOString();
logger[logMethod](logData);
}

debug(logData: LogData) {
this.writeLog('debug', logData);
}

info(logData: LogData) {
this.writeLog('info', logData);
}

error(logData: LogData) {
this.writeLog('error', logData);
}

warn(logData: LogData) {
this.writeLog('warn', logData);
}

fatal(logData: LogData) {
this.writeLog('fatal', logData);
}
}
2 changes: 2 additions & 0 deletions src/config/cfg.default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ export default {
passSalt: 'u5o2law8xi',
tokenSecret: process.env.PRIVATE_KEY,
tokenMaxAge: process.env.TOKEN_EXPIRATION,
rateLimitWindowMs: 15 * 60 * 1000, // 15 minutes
rateLimitMax: 1000, // limit each IP to rateLimitMax requests per windowMs
},
};
2 changes: 2 additions & 0 deletions src/config/type/ServerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export class ServerConfig extends BaseConfig {
readonly passSalt: string;
readonly tokenSecret: string;
readonly tokenMaxAge: number;
readonly rateLimitWindowMs: number;
readonly rateLimitMax: number;

constructor(cfg) {
super(cfg);
Expand Down
Loading

0 comments on commit ba93da7

Please sign in to comment.