|
| 1 | +// app.module.ts |
| 2 | +import { Module } from '@nestjs/common'; |
| 3 | +import { TerminusModule } from '@nestjs/terminus'; |
| 4 | +import { ThrottlerModule, seconds } from '@nestjs/throttler'; |
| 5 | +import { |
| 6 | + RedisHealthIndicator, |
| 7 | + RedisModule, |
| 8 | + RedisThrottlerStorage, |
| 9 | + RedisToken, |
| 10 | + RedlockModule, |
| 11 | +} from '@nestjs-redis/kit'; |
| 12 | +import type { RedisClientType } from 'redis'; |
| 13 | +import { AppController } from './app.controller'; |
| 14 | +import { AppService } from './app.service'; |
| 15 | +import { HealthController } from './health.controller'; |
| 16 | + |
| 17 | +@Module({ |
| 18 | + imports: [ |
| 19 | + // Client |
| 20 | + RedisModule.forRoot({ |
| 21 | + isGlobal: true, |
| 22 | + options: { url: 'redis://localhost:6379' }, |
| 23 | + }), |
| 24 | + RedisModule.forRoot({ |
| 25 | + isGlobal: true, |
| 26 | + options: { url: 'redis://localhost:6380' }, |
| 27 | + connectionName: 'cluster', |
| 28 | + }), |
| 29 | + |
| 30 | + // Locking |
| 31 | + RedlockModule.forRootAsync({ |
| 32 | + inject: [RedisToken()], |
| 33 | + useFactory: (redis: RedisClientType) => ({ |
| 34 | + clients: [redis], |
| 35 | + redlockConfig: { retryDelayMs: 100, retryCount: 2 }, |
| 36 | + }), |
| 37 | + }), |
| 38 | + |
| 39 | + // Throttling |
| 40 | + ThrottlerModule.forRootAsync({ |
| 41 | + inject: [RedisToken()], |
| 42 | + useFactory: (redis: RedisClientType) => ({ |
| 43 | + throttlers: [{ limit: 5111, ttl: seconds(60) }], |
| 44 | + storage: new RedisThrottlerStorage(redis), |
| 45 | + }), |
| 46 | + }), |
| 47 | + |
| 48 | + // Health checks |
| 49 | + TerminusModule, |
| 50 | + ], |
| 51 | + controllers: [AppController, HealthController], |
| 52 | + providers: [RedisHealthIndicator, AppService], |
| 53 | +}) |
| 54 | +export class AppModule {} |
0 commit comments