-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initialize agent on startup, add caching config
Signed-off-by: Bryce McMath <[email protected]>
- Loading branch information
1 parent
f82ba19
commit 1c4b534
Showing
5 changed files
with
70 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
], | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters