-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2d1726e
commit 21ce138
Showing
1 changed file
with
17 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,26 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { ThrottlerGuard } from '@nestjs/throttler'; | ||
import { Request } from 'express'; | ||
|
||
@Injectable() | ||
export class RateLimiterGuard extends ThrottlerGuard { | ||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const request = context.switchToHttp().getRequest(); | ||
console.log("canActivate") | ||
console.log(request.context.config.url) | ||
|
||
// Check if the request path is "/metrics" | ||
if (request.context.config.url === '/metrics') { | ||
return true; // Bypass rate limiting for "/metrics" | ||
} | ||
|
||
// For other routes, apply rate limiting | ||
return super.canActivate(context); | ||
} | ||
|
||
async getKey(request: Request): Promise<string> { | ||
// In this example, we're using the IP address as the key for rate limiting. | ||
return request.ip; | ||
} | ||
} | ||
} | ||
|