|
1 |
| -import { Module } from '@nestjs/common' |
| 1 | +import { DynamicModule, Module } from '@nestjs/common' |
2 | 2 | import { ThrottlerModule, ThrottlerGuard } from '@nestjs/throttler'
|
3 | 3 | import { APP_GUARD } from '@nestjs/core'
|
4 | 4 | import { AppController } from './app.controller'
|
5 | 5 | import { AppService } from './app.service'
|
6 |
| -import { ConsoleLogger } from '@credo-ts/core' |
7 |
| -import { IndyVdrPoolConfig } from '@credo-ts/indy-vdr' |
8 | 6 | import { Logger } from '@nestjs/common'
|
9 |
| -import { IndyVdrProxyModule } from 'credo-ts-indy-vdr-proxy-server' |
10 |
| -import { readFileSync } from 'fs' |
| 7 | +import { IndyVdrProxyAgent, IndyVdrProxyModule } from 'credo-ts-indy-vdr-proxy-server' |
11 | 8 | import 'dotenv/config'
|
12 | 9 |
|
13 |
| -import { setupAgent } from './helpers/agent' |
14 |
| - |
15 |
| -const configPath = process.env.INDY_VDR_PROXY_CONFIG_PATH ?? './res/app.config.json' |
16 |
| -Logger.log(`Registering Indy VDR Proxy Module with config file ${configPath}`) |
17 |
| -const config = JSON.parse(readFileSync(configPath, { encoding: 'utf-8' })) |
18 |
| -const networks = config.networks as [IndyVdrPoolConfig, ...IndyVdrPoolConfig[]] |
19 |
| -const logLevel = process.env.LOG_LEVEL ? parseInt(process.env.LOG_LEVEL) : 2 |
20 | 10 | const ttl = process.env.THROTTLE_TTL ? parseInt(process.env.THROTTLE_TTL) : 60000
|
21 | 11 | const limit = process.env.THROTTLE_LIMIT ? parseInt(process.env.THROTTLE_LIMIT) : 2000
|
22 |
| -Logger.log(`Using Credo log level ${logLevel}, throttle TTL ${ttl}, and throttle limit ${limit}`) |
| 12 | +Logger.log(`Using throttle TTL ${ttl}, and throttle limit ${limit}`) |
23 | 13 |
|
24 |
| -@Module({ |
25 |
| - imports: [ |
26 |
| - // Limit requests per user to {limit} number of requests every {ttl} milliseconds |
27 |
| - ThrottlerModule.forRoot([ |
28 |
| - { |
29 |
| - ttl, |
30 |
| - limit, |
31 |
| - }, |
32 |
| - ]), |
33 |
| - IndyVdrProxyModule.register(setupAgent({ networks, logger: new ConsoleLogger(logLevel) })), |
34 |
| - ], |
35 |
| - controllers: [AppController], |
36 |
| - providers: [ |
37 |
| - AppService, |
38 |
| - { |
39 |
| - provide: APP_GUARD, |
40 |
| - useClass: ThrottlerGuard, |
41 |
| - }, |
42 |
| - ], |
43 |
| -}) |
44 |
| -export class AppModule {} |
| 14 | +@Module({}) |
| 15 | +export class AppModule { |
| 16 | + static register(agent: IndyVdrProxyAgent): DynamicModule { |
| 17 | + return { |
| 18 | + module: AppModule, |
| 19 | + imports: [ |
| 20 | + // Limit requests per user to {limit} number of requests every {ttl} milliseconds |
| 21 | + ThrottlerModule.forRoot([ |
| 22 | + { |
| 23 | + ttl, |
| 24 | + limit, |
| 25 | + }, |
| 26 | + ]), |
| 27 | + IndyVdrProxyModule.register(agent), |
| 28 | + ], |
| 29 | + controllers: [AppController], |
| 30 | + providers: [ |
| 31 | + AppService, |
| 32 | + { |
| 33 | + provide: APP_GUARD, |
| 34 | + useClass: ThrottlerGuard, |
| 35 | + }, |
| 36 | + ], |
| 37 | + } |
| 38 | + } |
| 39 | +} |
0 commit comments