Skip to content

Commit

Permalink
feat: initialize agent on startup, add caching config
Browse files Browse the repository at this point in the history
Signed-off-by: Bryce McMath <[email protected]>
  • Loading branch information
bryce-mcmath committed Sep 20, 2024
1 parent f82ba19 commit 1c4b534
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 41 deletions.
63 changes: 29 additions & 34 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,39 @@
import { Module } from '@nestjs/common'
import { DynamicModule, Module } from '@nestjs/common'
import { ThrottlerModule, ThrottlerGuard } from '@nestjs/throttler'
import { APP_GUARD } from '@nestjs/core'
import { AppController } from './app.controller'
import { AppService } from './app.service'
import { ConsoleLogger } from '@credo-ts/core'
import { IndyVdrPoolConfig } from '@credo-ts/indy-vdr'
import { Logger } from '@nestjs/common'
import { IndyVdrProxyModule } from 'credo-ts-indy-vdr-proxy-server'
import { readFileSync } from 'fs'
import { IndyVdrProxyAgent, IndyVdrProxyModule } from 'credo-ts-indy-vdr-proxy-server'
import 'dotenv/config'

import { setupAgent } from './helpers/agent'

const configPath = process.env.INDY_VDR_PROXY_CONFIG_PATH ?? './res/app.config.json'
Logger.log(`Registering Indy VDR Proxy Module with config file ${configPath}`)
const config = JSON.parse(readFileSync(configPath, { encoding: 'utf-8' }))
const networks = config.networks as [IndyVdrPoolConfig, ...IndyVdrPoolConfig[]]
const logLevel = process.env.LOG_LEVEL ? parseInt(process.env.LOG_LEVEL) : 2
const ttl = process.env.THROTTLE_TTL ? parseInt(process.env.THROTTLE_TTL) : 60000
const limit = process.env.THROTTLE_LIMIT ? parseInt(process.env.THROTTLE_LIMIT) : 2000
Logger.log(`Using Credo log level ${logLevel}, throttle TTL ${ttl}, and throttle limit ${limit}`)
Logger.log(`Using throttle TTL ${ttl}, and throttle limit ${limit}`)

@Module({
imports: [
// Limit requests per user to {limit} number of requests every {ttl} milliseconds
ThrottlerModule.forRoot([
{
ttl,
limit,
},
]),
IndyVdrProxyModule.register(setupAgent({ networks, logger: new ConsoleLogger(logLevel) })),
],
controllers: [AppController],
providers: [
AppService,
{
provide: APP_GUARD,
useClass: ThrottlerGuard,
},
],
})
export class AppModule {}
@Module({})
export class AppModule {
static register(agent: IndyVdrProxyAgent): DynamicModule {
return {
module: AppModule,
imports: [
// Limit requests per user to {limit} number of requests every {ttl} milliseconds
ThrottlerModule.forRoot([
{
ttl,
limit,
},
]),
IndyVdrProxyModule.register(agent),
],
controllers: [AppController],
providers: [
AppService,
{
provide: APP_GUARD,
useClass: ThrottlerGuard,
},
],
}
}
}
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const BASE_DATA_PATH = '/var/credo/data'
export const BASE_CACHE_PATH = '/var/credo/cache'
export const BASE_TEMP_PATH = '/var/credo/tmp'
7 changes: 4 additions & 3 deletions src/helpers/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ import { ariesAskar } from '@hyperledger/aries-askar-nodejs'
import { indyVdr } from '@hyperledger/indy-vdr-nodejs'
import { EventEmitter } from 'events'
import { WebSocket } from 'ws'
import { BASE_CACHE_PATH, BASE_DATA_PATH, BASE_TEMP_PATH } from '../constants'

class CustomFileSystem extends NodeFileSystem {
public constructor() {
super({
baseDataPath: `/var/credo/data`,
baseCachePath: `/var/credo/cache`,
baseTempPath: `/var/credo/tmp`,
baseDataPath: BASE_DATA_PATH,
baseCachePath: BASE_CACHE_PATH,
baseTempPath: BASE_TEMP_PATH,
})
}
}
Expand Down
27 changes: 25 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
import { ConsoleLogger } from '@credo-ts/core'
import { IndyVdrPoolConfig } from '@credo-ts/indy-vdr'
import { NestFactory } from '@nestjs/core'
import { Logger } from '@nestjs/common'
import { AppModule } from './app.module'
import { setupAgent } from './helpers/agent'
import { readFileSync } from 'fs'
import 'dotenv/config'
import { BASE_CACHE_PATH } from './constants'

async function bootstrap() {
const app = await NestFactory.create(AppModule)
const configPath = process.env.INDY_VDR_PROXY_CONFIG_PATH ?? './res/app.config.json'
const config = JSON.parse(readFileSync(configPath, { encoding: 'utf-8' }))
const logLevel = process.env.LOG_LEVEL ? parseInt(process.env.LOG_LEVEL) : 2
const logger = new ConsoleLogger(logLevel)
logger.info(`Registering App Module with config file ${configPath}`)
const networks = config.networks as [IndyVdrPoolConfig, ...IndyVdrPoolConfig[]]
const capacity = process.env.CACHE_CAPACITY ? parseInt(process.env.CACHE_CAPACITY) : 1000
const expiryMs = process.env.CACHE_EXPIRY_MS
? parseInt(process.env.CACHE_EXPIRY_MS)
: 1000 * 60 * 60 * 24 * 7
const path = process.env.CACHE_PATH ?? `${BASE_CACHE_PATH}/txn-cache`
const agent = setupAgent({ networks, logger, cache: { capacity, expiryMs, path } })
await agent.initialize()
logger.info('Agent initialized')

const app = await NestFactory.create(AppModule.register(agent))
await app.listen(process.env.PORT ?? 3000)
}
bootstrap()
bootstrap().catch((e) => {
Logger.error('Error initializing server', e)
})
11 changes: 9 additions & 2 deletions test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { IndyVdrPoolConfig } from '@credo-ts/indy-vdr'
import { Test, TestingModule } from '@nestjs/testing'
import { INestApplication } from '@nestjs/common'
import * as request from 'supertest'
import { AppModule } from './../src/app.module'
import { AppModule } from '../src/app.module'
import { setupAgent } from '../src/helpers/agent'
import { readFileSync } from 'fs'

const configPath = process.env.INDY_VDR_PROXY_CONFIG_PATH ?? './res/app.config.json'
const config = JSON.parse(readFileSync(configPath, { encoding: 'utf-8' }))
const networks = config.networks as [IndyVdrPoolConfig, ...IndyVdrPoolConfig[]]

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

beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
imports: [AppModule.register(setupAgent({ networks }))],
}).compile()

app = moduleFixture.createNestApplication()
Expand Down

0 comments on commit 1c4b534

Please sign in to comment.