Skip to content

Commit

Permalink
add config dotenv support + refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
michavie committed Jan 9, 2024
1 parent 91bcf01 commit c5b1192
Show file tree
Hide file tree
Showing 50 changed files with 408 additions and 756 deletions.
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
web: npm run start:mainnet
web: npm run start
26 changes: 26 additions & 0 deletions apps/api/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const config = () => ({
features: {
publicApi: {
enabled: process.env.PUBLIC_API !== 'false',
port: parseInt(process.env.PRIVATE_API_PORT || process.env.PORT || '3000'),
},
privateApi: {
enabled: process.env.PRIVATE_API !== 'false',
port: parseInt(process.env.PRIVATE_API_PORT || '4000'),
},
},
services: {
swagger: {
urls: [process.env.SWAGGER_URL || 'https://aggregator.example.com'],
},
chain: {
apiUrl: process.env.CHAIN_API_URL || 'https://api.multiversx.com',
nativeAuth: {
acceptedOrigins: ['https://utils.multiversx.com'],
},
},
redis: {
url: process.env.REDIS_URL || '127.0.0.1',
},
},
})
33 changes: 0 additions & 33 deletions apps/api/config/config.devnet.yaml

This file was deleted.

33 changes: 0 additions & 33 deletions apps/api/config/config.mainnet.yaml

This file was deleted.

33 changes: 0 additions & 33 deletions apps/api/config/config.testnet.yaml

This file was deleted.

9 changes: 0 additions & 9 deletions apps/api/config/configuration.ts

This file was deleted.

13 changes: 3 additions & 10 deletions apps/api/src/endpoints/endpoints.controllers.module.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import { Module } from '@nestjs/common'
import { config } from 'apps/api/config'
import { AuthController } from './auth/auth.controller'
import configuration from '../../../api/config/configuration'
import { EndpointsServicesModule } from './endpoints.services.module'
import { CollectionsController } from './collections/collections.controller'
import {
AppService,
ContractService,
ApiConfigModule,
DelegationService,
DynamicModuleUtils,
HealthCheckController,
} from '@mvx-monorepo/common'
import { AppService, ContractService, DelegationService, DynamicModuleUtils, HealthCheckController } from '@mvx-monorepo/common'

@Module({
imports: [EndpointsServicesModule, ApiConfigModule.forRoot(configuration), DynamicModuleUtils.getCachingModule(configuration)],
imports: [EndpointsServicesModule, DynamicModuleUtils.getCachingModule(config)],
providers: [DynamicModuleUtils.getNestJsApiConfigService(), AppService, DelegationService, ContractService],
controllers: [AuthController, HealthCheckController, CollectionsController],
})
Expand Down
38 changes: 18 additions & 20 deletions apps/api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as bodyParser from 'body-parser'
import { PublicAppModule } from './public.app.module'
import { PrivateAppModule } from './private.app.module'
import { Logger, NestInterceptor } from '@nestjs/common'
import { config } from 'apps/transactions-processor/config'
import { HttpAdapterHost, NestFactory } from '@nestjs/core'
import { PubSubListenerModule } from '@mvx-monorepo/common'
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston'
Expand All @@ -14,14 +15,13 @@ import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'
import { LoggerInitializer } from '@multiversx/sdk-nestjs-common'
import { MicroserviceOptions, Transport } from '@nestjs/microservices'
import { CacheService, CachingInterceptor } from '@multiversx/sdk-nestjs-cache'
import { ApiConfigService, SdkNestjsConfigServiceImpl } from '@mvx-monorepo/common'
import { AppConfigService, SdkNestjsConfigServiceImpl } from '@mvx-monorepo/common'
import { LoggingInterceptor, MetricsService } from '@multiversx/sdk-nestjs-monitoring'

import '@multiversx/sdk-nestjs-common/lib/utils/extensions/array.extensions'
import '@multiversx/sdk-nestjs-common/lib/utils/extensions/date.extensions'
import '@multiversx/sdk-nestjs-common/lib/utils/extensions/number.extensions'
import '@multiversx/sdk-nestjs-common/lib/utils/extensions/string.extensions'
import configuration from '../config/configuration'

async function bootstrap() {
const publicApp = await NestFactory.create(PublicAppModule)
Expand All @@ -30,23 +30,23 @@ async function bootstrap() {
publicApp.useLogger(publicApp.get(WINSTON_MODULE_NEST_PROVIDER))
publicApp.use(cookieParser())

const apiConfigService = publicApp.get<ApiConfigService>(ApiConfigService)
const appConfigService = publicApp.get<AppConfigService>(AppConfigService)
const cachingService = publicApp.get<CacheService>(CacheService)
const metricsService = publicApp.get<MetricsService>(MetricsService)
const httpAdapterHostService = publicApp.get<HttpAdapterHost>(HttpAdapterHost)

if (apiConfigService.getIsAuthActive()) {
publicApp.useGlobalGuards(new NativeAuthGuard(new SdkNestjsConfigServiceImpl(apiConfigService), cachingService))
if (appConfigService.isAuthActive) {
publicApp.useGlobalGuards(new NativeAuthGuard(new SdkNestjsConfigServiceImpl(appConfigService), cachingService))
}

const httpServer = httpAdapterHostService.httpAdapter.getHttpServer()
httpServer.keepAliveTimeout = apiConfigService.getServerTimeout()
httpServer.headersTimeout = apiConfigService.getHeadersTimeout() //`keepAliveTimeout + server's expected response time`
httpServer.keepAliveTimeout = appConfigService.serverTimeout
httpServer.headersTimeout = appConfigService.headersTimeout //`keepAliveTimeout + server's expected response time`

const globalInterceptors: NestInterceptor[] = []
globalInterceptors.push(new LoggingInterceptor(metricsService))

if (apiConfigService.getUseCachingInterceptor()) {
if (appConfigService.useCachingInterceptor) {
const cachingInterceptor = new CachingInterceptor(cachingService, httpAdapterHostService, metricsService)

globalInterceptors.push(cachingInterceptor)
Expand All @@ -62,33 +62,31 @@ async function bootstrap() {
.setVersion('1.0.0')
.setExternalDoc('MultiversX Docs', 'https://docs.multiversx.com')

const apiUrls = apiConfigService.getSwaggerUrls()
const apiUrls = appConfigService.swaggerUrls
for (const apiUrl of apiUrls) {
documentBuilder = documentBuilder.addServer(apiUrl)
}

const config = documentBuilder.build()

const document = SwaggerModule.createDocument(publicApp, config)
const document = SwaggerModule.createDocument(publicApp, documentBuilder.build())
SwaggerModule.setup('', publicApp, document)

if (apiConfigService.getIsPublicApiFeatureActive()) {
await publicApp.listen(apiConfigService.getPublicApiFeaturePort())
if (appConfigService.isPublicApiFeatureActive) {
await publicApp.listen(appConfigService.publicApiFeaturePort)
}

if (apiConfigService.getIsPrivateApiFeatureActive()) {
if (appConfigService.isPrivateApiFeatureActive) {
const privateApp = await NestFactory.create(PrivateAppModule)
await privateApp.listen(apiConfigService.getPrivateApiFeaturePort())
await privateApp.listen(appConfigService.privateApiFeaturePort)
}

const logger = new Logger('Bootstrapper')

LoggerInitializer.initialize(logger)

const pubSubApp = await NestFactory.createMicroservice<MicroserviceOptions>(PubSubListenerModule.forRoot(configuration), {
const pubSubApp = await NestFactory.createMicroservice<MicroserviceOptions>(PubSubListenerModule.forRoot(config), {
transport: Transport.REDIS,
options: {
host: apiConfigService.getRedisUrl(),
host: appConfigService.redisUrl,
port: 6379,
retryAttempts: 100,
retryDelay: 1000,
Expand All @@ -99,8 +97,8 @@ async function bootstrap() {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
pubSubApp.listen()

logger.log(`Public API active: ${apiConfigService.getIsPublicApiFeatureActive()}`)
logger.log(`Private API active: ${apiConfigService.getIsPrivateApiFeatureActive()}`)
logger.log(`Public API active: ${appConfigService.isPublicApiFeatureActive}`)
logger.log(`Private API active: ${appConfigService.isPrivateApiFeatureActive}`)
}

// eslint-disable-next-line @typescript-eslint/no-floating-promises
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/private.app.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { config } from '../config'
import { Module } from '@nestjs/common'
import configuration from '../config/configuration'
import { LoggingModule } from '@multiversx/sdk-nestjs-common'
import { CacheController } from './endpoints/caching/cache.controller'
import { ApiMetricsModule, DynamicModuleUtils } from '@mvx-monorepo/common'
import { ApiMetricsController, HealthCheckController } from '@mvx-monorepo/common'

@Module({
imports: [LoggingModule, ApiMetricsModule, DynamicModuleUtils.getCachingModule(configuration)],
imports: [LoggingModule, ApiMetricsModule, DynamicModuleUtils.getCachingModule(config)],
providers: [DynamicModuleUtils.getNestJsApiConfigService(), DynamicModuleUtils.getPubSubService()],
controllers: [ApiMetricsController, CacheController, HealthCheckController],
})
Expand Down
20 changes: 20 additions & 0 deletions apps/cache-warmer/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const config = () => ({
features: {
cacheWarmer: {
enabled: process.env.CACHE_WARMER_ENABLED !== 'false',
port: 5201,
},
privateApi: {
enabled: process.env.PRIVATE_API_ENABLED !== 'false',
port: 4000,
},
},
contracts: {
aggregator: process.env.CONTRACT_AGGREGATOR || '####',
},
services: {
redis: {
url: process.env.REDIS_URL || '127.0.0.1',
},
},
})
12 changes: 0 additions & 12 deletions apps/cache-warmer/config/config.devnet.yaml

This file was deleted.

12 changes: 0 additions & 12 deletions apps/cache-warmer/config/config.mainnet.yaml

This file was deleted.

12 changes: 0 additions & 12 deletions apps/cache-warmer/config/config.testnet.yaml

This file was deleted.

11 changes: 0 additions & 11 deletions apps/cache-warmer/config/configuration.ts

This file was deleted.

4 changes: 2 additions & 2 deletions apps/cache-warmer/src/cache-warmer/cache.warmer.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Module } from '@nestjs/common'
import { ScheduleModule } from '@nestjs/schedule'
import configuration from '../../config/configuration'
import { config } from 'apps/cache-warmer/config'
import { DelegationModule } from '@mvx-monorepo/common'
import { CacheWarmerService } from './cache.warmer.service'
import { AppModule } from '@mvx-monorepo/common/app/app.module'
import { DynamicModuleUtils } from '@mvx-monorepo/common/utils/dynamic.module.utils'

@Module({
imports: [ScheduleModule.forRoot(), AppModule.forRoot(configuration), DelegationModule.forRoot(configuration)],
imports: [ScheduleModule.forRoot(), AppModule.forRoot(config), DelegationModule.forRoot(config)],
providers: [DynamicModuleUtils.getPubSubService(), CacheWarmerService],
})
export class CacheWarmerModule {}
Loading

0 comments on commit c5b1192

Please sign in to comment.