mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 07:34:54 +00:00
Merge branch 'dev' into SP-1355-powr-clap-historical-data
This commit is contained in:
@ -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 { winstonLoggerOptions } from './common/filters/http-exception/logger/winston.logger';
|
||||
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: 100000, limit: 30 }],
|
||||
}),
|
||||
WinstonModule.forRoot(winstonLoggerOptions),
|
||||
ClientModule,
|
||||
AuthenticationModule,
|
||||
@ -70,12 +76,17 @@ import { winstonLoggerOptions } from './common/filters/http-exception/logger/win
|
||||
TagModule,
|
||||
DeviceCommissionModule,
|
||||
PowerClampModule,
|
||||
HealthModule,
|
||||
],
|
||||
providers: [
|
||||
{
|
||||
provide: APP_INTERCEPTOR,
|
||||
useClass: LoggingInterceptor,
|
||||
},
|
||||
{
|
||||
provide: APP_GUARD,
|
||||
useClass: ThrottlerGuard,
|
||||
},
|
||||
],
|
||||
})
|
||||
export class AppModule {}
|
||||
|
||||
@ -1,24 +0,0 @@
|
||||
import { utilities as nestWinstonModuleUtilities } from 'nest-winston';
|
||||
import * as winston from 'winston';
|
||||
|
||||
export const winstonLoggerOptions: winston.LoggerOptions = {
|
||||
transports: [
|
||||
new winston.transports.Console({
|
||||
format: winston.format.combine(
|
||||
winston.format.timestamp(),
|
||||
nestWinstonModuleUtilities.format.nestLike('MyApp', {
|
||||
prettyPrint: true,
|
||||
}),
|
||||
),
|
||||
}),
|
||||
new winston.transports.File({
|
||||
filename: 'logs/error.log',
|
||||
level: 'error',
|
||||
format: winston.format.json(),
|
||||
}),
|
||||
new winston.transports.File({
|
||||
filename: 'logs/combined.log',
|
||||
format: winston.format.json(),
|
||||
}),
|
||||
],
|
||||
};
|
||||
@ -582,6 +582,9 @@ export class DeviceService {
|
||||
|
||||
return { successResults, failedResults };
|
||||
} catch (error) {
|
||||
if (error instanceof HttpException) {
|
||||
throw error;
|
||||
}
|
||||
throw new HttpException(
|
||||
error.message || 'Device Not Found',
|
||||
error.status || HttpStatus.NOT_FOUND,
|
||||
@ -685,7 +688,7 @@ export class DeviceService {
|
||||
return {
|
||||
...rest,
|
||||
productUuid: product.uuid,
|
||||
name: deviceDetails.name,
|
||||
name: deviceDetails?.name,
|
||||
productName: product.name,
|
||||
} as GetDeviceDetailsInterface;
|
||||
} catch (error) {
|
||||
@ -1116,6 +1119,9 @@ export class DeviceService {
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof HttpException) {
|
||||
throw error;
|
||||
}
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
|
||||
37
src/health/controllers/health.controller.ts
Normal file
37
src/health/controllers/health.controller.ts
Normal 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),
|
||||
]);
|
||||
}
|
||||
}
|
||||
1
src/health/controllers/index.ts
Normal file
1
src/health/controllers/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './health.controller';
|
||||
13
src/health/health.module.ts
Normal file
13
src/health/health.module.ts
Normal 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 {}
|
||||
33
src/health/services/health.service.ts
Normal file
33
src/health/services/health.service.ts
Normal 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),
|
||||
]);
|
||||
}
|
||||
}
|
||||
1
src/health/services/index.ts
Normal file
1
src/health/services/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './health.service';
|
||||
13
src/main.ts
13
src/main.ts
@ -7,6 +7,9 @@ import { ValidationPipe } from '@nestjs/common';
|
||||
import { json, urlencoded } from 'body-parser';
|
||||
import { SeederService } from '@app/common/seed/services/seeder.service';
|
||||
import { HttpExceptionFilter } from './common/filters/http-exception/http-exception.filter';
|
||||
import { Logger } from '@nestjs/common';
|
||||
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
|
||||
import { RequestContextMiddleware } from '@app/common/middleware/request-context.middleware';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
@ -18,6 +21,8 @@ async function bootstrap() {
|
||||
app.use(urlencoded({ limit: '1mb', extended: true }));
|
||||
app.useGlobalFilters(new HttpExceptionFilter());
|
||||
|
||||
app.use(new RequestContextMiddleware().use);
|
||||
|
||||
app.use(
|
||||
rateLimit({
|
||||
windowMs: 5 * 60 * 1000,
|
||||
@ -43,14 +48,16 @@ async function bootstrap() {
|
||||
);
|
||||
|
||||
const seederService = app.get(SeederService);
|
||||
const logger = app.get<Logger>(WINSTON_MODULE_NEST_PROVIDER);
|
||||
|
||||
try {
|
||||
await seederService.seed();
|
||||
console.log('Seeding complete!');
|
||||
logger.log('Seeding complete!');
|
||||
} catch (error) {
|
||||
console.error('Seeding failed!', error);
|
||||
logger.error('Seeding failed!', error.stack || error);
|
||||
}
|
||||
|
||||
console.log('Starting auth at port ...', process.env.PORT || 4000);
|
||||
logger.log('Starting auth at port ...', process.env.PORT || 4000);
|
||||
await app.listen(process.env.PORT || 4000);
|
||||
}
|
||||
bootstrap();
|
||||
|
||||
Reference in New Issue
Block a user