Merge pull request #349 from SyncrowIOT/SP-1315-BE-API-rate-limit

added health check
This commit is contained in:
hannathkadher
2025-04-24 12:26:19 +04:00
committed by GitHub
8 changed files with 288 additions and 2 deletions

View File

@ -12,7 +12,7 @@ import { UserNotificationModule } from './user-notification/user-notification.mo
import { DeviceMessagesSubscriptionModule } from './device-messages/device-messages.module';
import { SceneModule } from './scene/scene.module';
import { DoorLockModule } from './door-lock/door.lock.module';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { APP_GUARD, APP_INTERCEPTOR } from '@nestjs/core';
import { LoggingInterceptor } from './interceptors/logging.interceptor';
import { AutomationModule } from './automation/automation.module';
import { RegionModule } from './region/region.module';
@ -33,12 +33,18 @@ import { ClientModule } from './client/client.module';
import { DeviceCommissionModule } from './commission-device/commission-device.module';
import { PowerClampModule } from './power-clamp/power-clamp.module';
import { WinstonModule } from 'nest-winston';
import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';
import { HealthModule } from './health/health.module';
import { winstonLoggerOptions } from '../libs/common/src/logger/services/winston.logger';
@Module({
imports: [
ConfigModule.forRoot({
load: config,
}),
ThrottlerModule.forRoot({
throttlers: [{ ttl: 60000, limit: 10 }],
}),
WinstonModule.forRoot(winstonLoggerOptions),
ClientModule,
AuthenticationModule,
@ -70,12 +76,17 @@ import { winstonLoggerOptions } from '../libs/common/src/logger/services/winston
TagModule,
DeviceCommissionModule,
PowerClampModule,
HealthModule,
],
providers: [
{
provide: APP_INTERCEPTOR,
useClass: LoggingInterceptor,
},
{
provide: APP_GUARD,
useClass: ThrottlerGuard,
},
],
})
export class AppModule {}

View File

@ -0,0 +1,37 @@
import { Controller, Get } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import {
HealthCheck,
HealthCheckService,
TypeOrmHealthIndicator,
DiskHealthIndicator,
MemoryHealthIndicator,
HttpHealthIndicator,
} from '@nestjs/terminus';
@ApiTags('Health Module')
@Controller('health')
export class HealthController {
constructor(
private health: HealthCheckService,
private db: TypeOrmHealthIndicator,
private memory: MemoryHealthIndicator,
private disk: DiskHealthIndicator,
private http: HttpHealthIndicator,
) {}
@Get()
@HealthCheck()
check() {
return this.health.check([
() => this.db.pingCheck('database'),
() =>
this.disk.checkStorage('disk', {
thresholdPercent: 0.9,
path: '/',
}),
() => this.memory.checkHeap('memory_heap', 300 * 1024 * 1024),
() => this.http.pingCheck('tuya', process.env.TUYA_EU_URL),
]);
}
}

View File

@ -0,0 +1 @@
export * from './health.controller';

View File

@ -0,0 +1,13 @@
// src/health/health.module.ts
import { Module } from '@nestjs/common';
import { TerminusModule } from '@nestjs/terminus';
import { TypeOrmModule } from '@nestjs/typeorm';
import { HealthController } from './controllers';
import { HttpModule } from '@nestjs/axios';
import { HealthService } from './services';
@Module({
imports: [TerminusModule, HttpModule, TypeOrmModule],
controllers: [HealthController],
providers: [HealthService],
})
export class HealthModule {}

View File

@ -0,0 +1,33 @@
import { Injectable } from '@nestjs/common';
import {
HealthCheckService,
TypeOrmHealthIndicator,
DiskHealthIndicator,
MemoryHealthIndicator,
HttpHealthIndicator,
HealthCheckResult,
} from '@nestjs/terminus';
@Injectable()
export class HealthService {
constructor(
private health: HealthCheckService,
private db: TypeOrmHealthIndicator,
private memory: MemoryHealthIndicator,
private disk: DiskHealthIndicator,
private http: HttpHealthIndicator,
) {}
check(): Promise<HealthCheckResult> {
return this.health.check([
() => this.db.pingCheck('database'),
() =>
this.disk.checkStorage('disk', {
thresholdPercent: 0.9,
path: '/',
}),
() => this.memory.checkHeap('memory_heap', 300 * 1024 * 1024),
() => this.http.pingCheck('tuya', process.env.TUYA_EU_URL),
]);
}
}

View File

@ -0,0 +1 @@
export * from './health.service';