From 1c4b5348b54e18533750201fd2a8078e6616f763 Mon Sep 17 00:00:00 2001 From: Bryce McMath Date: Thu, 19 Sep 2024 17:07:17 -0700 Subject: [PATCH] feat: initialize agent on startup, add caching config Signed-off-by: Bryce McMath --- src/app.module.ts | 63 ++++++++++++++++++++------------------------ src/constants.ts | 3 +++ src/helpers/agent.ts | 7 ++--- src/main.ts | 27 +++++++++++++++++-- test/app.e2e-spec.ts | 11 ++++++-- 5 files changed, 70 insertions(+), 41 deletions(-) create mode 100644 src/constants.ts diff --git a/src/app.module.ts b/src/app.module.ts index 22cfdb5..83010de 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -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, + }, + ], + } + } +} diff --git a/src/constants.ts b/src/constants.ts new file mode 100644 index 0000000..78c7064 --- /dev/null +++ b/src/constants.ts @@ -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' diff --git a/src/helpers/agent.ts b/src/helpers/agent.ts index de38fc2..299e9e1 100644 --- a/src/helpers/agent.ts +++ b/src/helpers/agent.ts @@ -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, }) } } diff --git a/src/main.ts b/src/main.ts index fcb26fe..4411b7b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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) +}) diff --git a/test/app.e2e-spec.ts b/test/app.e2e-spec.ts index ffec68c..ee819b8 100644 --- a/test/app.e2e-spec.ts +++ b/test/app.e2e-spec.ts @@ -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()