Skip to content

Commit a3d696d

Browse files
authored
feat: initialize agent on startup, add caching config (#24)
Signed-off-by: Bryce McMath <bryce.j.mcmath@gmail.com>
1 parent f82ba19 commit a3d696d

File tree

5 files changed

+70
-41
lines changed

5 files changed

+70
-41
lines changed

src/app.module.ts

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,39 @@
1-
import { Module } from '@nestjs/common'
1+
import { DynamicModule, Module } from '@nestjs/common'
22
import { ThrottlerModule, ThrottlerGuard } from '@nestjs/throttler'
33
import { APP_GUARD } from '@nestjs/core'
44
import { AppController } from './app.controller'
55
import { AppService } from './app.service'
6-
import { ConsoleLogger } from '@credo-ts/core'
7-
import { IndyVdrPoolConfig } from '@credo-ts/indy-vdr'
86
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'
118
import 'dotenv/config'
129

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
2010
const ttl = process.env.THROTTLE_TTL ? parseInt(process.env.THROTTLE_TTL) : 60000
2111
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}`)
2313

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+
}

src/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const BASE_DATA_PATH = '/var/credo/data'
2+
export const BASE_CACHE_PATH = '/var/credo/cache'
3+
export const BASE_TEMP_PATH = '/var/credo/tmp'

src/helpers/agent.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ import { ariesAskar } from '@hyperledger/aries-askar-nodejs'
2020
import { indyVdr } from '@hyperledger/indy-vdr-nodejs'
2121
import { EventEmitter } from 'events'
2222
import { WebSocket } from 'ws'
23+
import { BASE_CACHE_PATH, BASE_DATA_PATH, BASE_TEMP_PATH } from '../constants'
2324

2425
class CustomFileSystem extends NodeFileSystem {
2526
public constructor() {
2627
super({
27-
baseDataPath: `/var/credo/data`,
28-
baseCachePath: `/var/credo/cache`,
29-
baseTempPath: `/var/credo/tmp`,
28+
baseDataPath: BASE_DATA_PATH,
29+
baseCachePath: BASE_CACHE_PATH,
30+
baseTempPath: BASE_TEMP_PATH,
3031
})
3132
}
3233
}

src/main.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
1+
import { ConsoleLogger } from '@credo-ts/core'
2+
import { IndyVdrPoolConfig } from '@credo-ts/indy-vdr'
13
import { NestFactory } from '@nestjs/core'
4+
import { Logger } from '@nestjs/common'
25
import { AppModule } from './app.module'
6+
import { setupAgent } from './helpers/agent'
7+
import { readFileSync } from 'fs'
38
import 'dotenv/config'
9+
import { BASE_CACHE_PATH } from './constants'
410

511
async function bootstrap() {
6-
const app = await NestFactory.create(AppModule)
12+
const configPath = process.env.INDY_VDR_PROXY_CONFIG_PATH ?? './res/app.config.json'
13+
const config = JSON.parse(readFileSync(configPath, { encoding: 'utf-8' }))
14+
const logLevel = process.env.LOG_LEVEL ? parseInt(process.env.LOG_LEVEL) : 2
15+
const logger = new ConsoleLogger(logLevel)
16+
logger.info(`Registering App Module with config file ${configPath}`)
17+
const networks = config.networks as [IndyVdrPoolConfig, ...IndyVdrPoolConfig[]]
18+
const capacity = process.env.CACHE_CAPACITY ? parseInt(process.env.CACHE_CAPACITY) : 1000
19+
const expiryMs = process.env.CACHE_EXPIRY_MS
20+
? parseInt(process.env.CACHE_EXPIRY_MS)
21+
: 1000 * 60 * 60 * 24 * 7
22+
const path = process.env.CACHE_PATH ?? `${BASE_CACHE_PATH}/txn-cache`
23+
const agent = setupAgent({ networks, logger, cache: { capacity, expiryMs, path } })
24+
await agent.initialize()
25+
logger.info('Agent initialized')
26+
27+
const app = await NestFactory.create(AppModule.register(agent))
728
await app.listen(process.env.PORT ?? 3000)
829
}
9-
bootstrap()
30+
bootstrap().catch((e) => {
31+
Logger.error('Error initializing server', e)
32+
})

test/app.e2e-spec.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1+
import { IndyVdrPoolConfig } from '@credo-ts/indy-vdr'
12
import { Test, TestingModule } from '@nestjs/testing'
23
import { INestApplication } from '@nestjs/common'
34
import * as request from 'supertest'
4-
import { AppModule } from './../src/app.module'
5+
import { AppModule } from '../src/app.module'
6+
import { setupAgent } from '../src/helpers/agent'
7+
import { readFileSync } from 'fs'
8+
9+
const configPath = process.env.INDY_VDR_PROXY_CONFIG_PATH ?? './res/app.config.json'
10+
const config = JSON.parse(readFileSync(configPath, { encoding: 'utf-8' }))
11+
const networks = config.networks as [IndyVdrPoolConfig, ...IndyVdrPoolConfig[]]
512

613
describe('AppModule (e2e)', () => {
714
let app: INestApplication
815

916
beforeAll(async () => {
1017
const moduleFixture: TestingModule = await Test.createTestingModule({
11-
imports: [AppModule],
18+
imports: [AppModule.register(setupAgent({ networks }))],
1219
}).compile()
1320

1421
app = moduleFixture.createNestApplication()

0 commit comments

Comments
 (0)