-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add health controller, exception handling and rate limit
- Loading branch information
1 parent
e357c18
commit a5221ea
Showing
12 changed files
with
272 additions
and
57 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 |
---|---|---|
|
@@ -10,5 +10,6 @@ RUN pnpm install | |
COPY . . | ||
|
||
RUN pnpm run build | ||
EXPOSE 3000 | ||
|
||
CMD [ "pnpm", "start:dev" ] |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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 +1,20 @@ | ||
export class CreateAlertDto {} | ||
import { IsString, IsNumber, IsEmail, IsIn } from 'class-validator'; | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
|
||
export class CreateAlertDto { | ||
@ApiProperty({ | ||
description: 'The blockchain to monitor', | ||
enum: ['ethereum', 'polygon'], | ||
}) | ||
@IsString() | ||
@IsIn(['ethereum', 'polygon']) | ||
chain: string; | ||
|
||
@ApiProperty({ description: 'The target price to trigger the alert' }) | ||
@IsNumber() | ||
targetPrice: number; | ||
|
||
@ApiProperty({ description: 'The email address to send the alert to' }) | ||
@IsEmail() | ||
email: string; | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { | ||
ExceptionFilter, | ||
Catch, | ||
ArgumentsHost, | ||
HttpException, | ||
HttpStatus, | ||
Logger, | ||
} from '@nestjs/common'; | ||
import { Request, Response } from 'express'; | ||
|
||
@Catch() | ||
export class HttpExceptionFilter implements ExceptionFilter { | ||
private readonly logger = new Logger(HttpExceptionFilter.name); | ||
|
||
catch(exception: unknown, host: ArgumentsHost) { | ||
const ctx = host.switchToHttp(); | ||
const response = ctx.getResponse<Response>(); | ||
const request = ctx.getRequest<Request>(); | ||
const status = | ||
exception instanceof HttpException | ||
? exception.getStatus() | ||
: HttpStatus.INTERNAL_SERVER_ERROR; | ||
|
||
const message = | ||
exception instanceof HttpException | ||
? exception.getResponse() | ||
: 'Internal server error'; | ||
|
||
this.logger.error( | ||
`Http Status: ${status} Error Message: ${JSON.stringify(message)}`, | ||
exception instanceof Error ? exception.stack : undefined, | ||
`${request.method} ${request.url}`, | ||
); | ||
|
||
response.status(status).json({ | ||
statusCode: status, | ||
timestamp: new Date().toISOString(), | ||
path: request.url, | ||
message: message, | ||
}); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { | ||
Injectable, | ||
CanActivate, | ||
ExecutionContext, | ||
HttpException, | ||
HttpStatus, | ||
} from '@nestjs/common'; | ||
import { Observable } from 'rxjs'; | ||
import { RateLimiterMemory } from 'rate-limiter-flexible'; | ||
|
||
@Injectable() | ||
export class RateLimitingGuard implements CanActivate { | ||
private rateLimiter: RateLimiterMemory; | ||
|
||
constructor() { | ||
this.rateLimiter = new RateLimiterMemory({ | ||
points: 10, // Number of points | ||
duration: 1, // Per second | ||
}); | ||
} | ||
|
||
canActivate( | ||
context: ExecutionContext, | ||
): boolean | Promise<boolean> | Observable<boolean> { | ||
const request = context.switchToHttp().getRequest(); | ||
const key = request.ip; | ||
|
||
return this.rateLimiter | ||
.consume(key) | ||
.then(() => true) | ||
.catch(() => { | ||
throw new HttpException( | ||
'Too Many Requests', | ||
HttpStatus.TOO_MANY_REQUESTS, | ||
); | ||
}); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger'; | ||
|
||
@ApiTags('health') | ||
@Controller('health') | ||
export class HealthController { | ||
@Get() | ||
@ApiOperation({ summary: 'Check application health' }) | ||
@ApiResponse({ status: 200, description: 'Application is healthy' }) | ||
check() { | ||
return { status: 'ok' }; | ||
} | ||
} |
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
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