71 lines
2.2 KiB
TypeScript
71 lines
2.2 KiB
TypeScript
import { Module, NestModule, MiddlewareConsumer } from '@nestjs/common';
|
|
import { PrismaModule } from './prisma/prisma.module';
|
|
import { ProductsModule } from './products/products.module';
|
|
import { UsersModule } from './users/users.module';
|
|
import { RedisModule } from './redis/redis.module';
|
|
import { RedisService } from './redis/redis.service';
|
|
import { AuthModule } from './auth/auth.module';
|
|
import { PetsModule } from './pets/pets.module';
|
|
import { OrdersModule } from './orders/orders.module';
|
|
import { SettingsModule } from './settings/settings.module';
|
|
import { ThrottlerModule, ThrottlerGuard } from '@nestjs/throttler';
|
|
import { APP_GUARD } from '@nestjs/core';
|
|
import { MetricsController } from './common/metrics.controller';
|
|
import { AdminModule } from './admin/admin.module';
|
|
import { HomeModule } from './home/home.module';
|
|
import { BlogsModule } from './blogs/blogs.module';
|
|
import { WikiModule } from './wiki/wiki.module';
|
|
import { SeoModule } from './seo/seo.module';
|
|
import { CmsModule } from './cms/cms.module';
|
|
import { WholesaleModule } from './wholesale/wholesale.module';
|
|
import { VideosModule } from './videos/videos.module';
|
|
|
|
@Module({
|
|
imports: [
|
|
PrismaModule,
|
|
RedisModule,
|
|
ProductsModule,
|
|
UsersModule,
|
|
AuthModule,
|
|
PetsModule,
|
|
OrdersModule,
|
|
SettingsModule,
|
|
ThrottlerModule.forRoot([{
|
|
ttl: 60000,
|
|
limit: 100,
|
|
}]),
|
|
AdminModule,
|
|
HomeModule,
|
|
BlogsModule,
|
|
WikiModule,
|
|
SeoModule,
|
|
CmsModule,
|
|
WholesaleModule,
|
|
VideosModule,
|
|
],
|
|
controllers: [MetricsController],
|
|
providers: [
|
|
{
|
|
provide: APP_GUARD,
|
|
useClass: ThrottlerGuard,
|
|
},
|
|
RedisService,
|
|
],
|
|
})
|
|
export class AppModule implements NestModule {
|
|
constructor(private readonly redisService: RedisService) {}
|
|
|
|
configure(consumer: MiddlewareConsumer) {
|
|
consumer
|
|
.apply((req: any, res: any, next: () => void) => {
|
|
MetricsController.incrementRequestCount();
|
|
// Increment daily visits counter in Redis (fire-and-forget)
|
|
const todayKey = `visits:${new Date().toISOString().split('T')[0]}`;
|
|
this.redisService.incr(todayKey, 86400).catch(() => {}); // TTL = 24h
|
|
next();
|
|
})
|
|
.exclude('metrics')
|
|
.forRoutes('*');
|
|
}
|
|
}
|