Skip to content

Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍

7.9 KB
Raw Blame
import { EventsModule } from '@ghostfolio/api/events/events.module';
import { PortfolioSnapshotComputationExceptionFilter } from '@ghostfolio/api/filters/portfolio-snapshot-computation-exception.filter';
import { ImpersonationWriteGuard } from '@ghostfolio/api/guards/impersonation-write.guard';
import { getRedisConnectionOptions } from '@ghostfolio/api/helper/redis.helper';
import { BullBoardAuthMiddleware } from '@ghostfolio/api/middlewares/bull-board-auth.middleware';
import { HtmlTemplateMiddleware } from '@ghostfolio/api/middlewares/html-template.middleware';
import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module';
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service';
import { CronModule } from '@ghostfolio/api/services/cron/cron.module';
import { DataProviderModule } from '@ghostfolio/api/services/data-provider/data-provider.module';
import { ExchangeRateDataModule } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.module';
import { I18nService } from '@ghostfolio/api/services/i18n/i18n.service';
import { PrismaModule } from '@ghostfolio/api/services/prisma/prisma.module';
import { PropertyModule } from '@ghostfolio/api/services/property/property.module';
import { DataGatheringQueueModule } from '@ghostfolio/api/services/queues/data-gathering/data-gathering.module';
import { PortfolioSnapshotQueueModule } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.module';
import { RedisCacheModule } from '@ghostfolio/api/services/redis-cache/redis-cache.module';
import {
  BULL_BOARD_ROUTE,
  THROTTLE_DEFAULT_LIMIT,
  THROTTLE_DEFAULT_TTL
} from '@ghostfolio/common/config';

import { ExpressAdapter } from '@bull-board/express';
import { BullBoardModule } from '@bull-board/nestjs';
import { ThrottlerStorageRedisService } from '@nest-lab/throttler-storage-redis';
import { BullModule } from '@nestjs/bull';
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { APP_FILTER, APP_GUARD } from '@nestjs/core';
import { EventEmitterModule } from '@nestjs/event-emitter';
import { ScheduleModule } from '@nestjs/schedule';
import { ServeStaticModule } from '@nestjs/serve-static';
import { ThrottlerModule } from '@nestjs/throttler';
import { getReasonPhrase, StatusCodes } from 'http-status-codes';
import { join } from 'node:path';

import { AdminModule } from './admin/admin.module';
import { AppController } from './app.controller';
import { AccessesModule } from './endpoints/accesses/accesses.module';
import { AccountsModule } from './endpoints/accounts/accounts.module';
import { ActivitiesModule } from './endpoints/activities/activities.module';
import { AiModule } from './endpoints/ai/ai.module';
import { ApiKeysModule } from './endpoints/api-keys/api-keys.module';
import { AssetProfilesModule } from './endpoints/asset-profiles/asset-profiles.module';
import { AssetsModule } from './endpoints/assets/assets.module';
import { AuthDevicesModule } from './endpoints/auth-devices/auth-devices.module';
import { AuthModule } from './endpoints/auth/auth.module';
import { BenchmarksModule } from './endpoints/benchmarks/benchmarks.module';
import { CacheModule } from './endpoints/cache/cache.module';
import { GhostfolioModule } from './endpoints/data-providers/ghostfolio/ghostfolio.module';
import { ExchangeRatesModule } from './endpoints/exchange-rates/exchange-rates.module';
import { ExportModule } from './endpoints/export/export.module';
import { HealthModule } from './endpoints/health/health.module';
import { ImportModule } from './endpoints/import/import.module';
import { InfoModule } from './endpoints/info/info.module';
import { LogoModule } from './endpoints/logo/logo.module';
import { MarketDataModule } from './endpoints/market-data/market-data.module';
import { McpModule } from './endpoints/mcp/mcp.module';
import { PlatformsModule } from './endpoints/platforms/platforms.module';
import { PublicModule } from './endpoints/public/public.module';
import { SitemapModule } from './endpoints/sitemap/sitemap.module';
import { SubscriptionModule } from './endpoints/subscription/subscription.module';
import { SymbolsModule } from './endpoints/symbols/symbols.module';
import { TagsModule } from './endpoints/tags/tags.module';
import { UsersModule } from './endpoints/users/users.module';
import { WatchlistModule } from './endpoints/watchlist/watchlist.module';
import { PortfolioModule } from './portfolio/portfolio.module';

@Module({
  controllers: [AppController],
  imports: [
    AdminModule,
    AccessesModule,
    AccountsModule,
    ActivitiesModule,
    AiModule,
    ApiKeysModule,
    AssetProfilesModule,
    AssetsModule,
    AuthDevicesModule,
    AuthModule,
    BenchmarksModule,
    BullBoardModule.forRoot({
      adapter: ExpressAdapter,
      boardOptions: {
        uiConfig: {
          boardLogo: {
            height: 0,
            path: '',
            width: 0
          },
          boardTitle: 'Job Queues',
          favIcon: {
            alternative: '/assets/favicon-32x32.png',
            default: '/assets/favicon-32x32.png'
          }
        }
      },
      middleware: BullBoardAuthMiddleware,
      route: BULL_BOARD_ROUTE
    }),
    BullModule.forRootAsync({
      imports: [ConfigurationModule],
      inject: [ConfigurationService],
      useFactory: (configurationService: ConfigurationService) => {
        return {
          redis: getRedisConnectionOptions(configurationService)
        };
      }
    }),
    CacheModule,
    ConfigModule.forRoot(),
    ConfigurationModule,
    CronModule,
    DataGatheringQueueModule,
    DataProviderModule,
    EventEmitterModule.forRoot(),
    EventsModule,
    ExchangeRateDataModule,
    ExchangeRatesModule,
    ExportModule,
    GhostfolioModule,
    HealthModule,
    ImportModule,
    InfoModule,
    LogoModule,
    MarketDataModule,
    McpModule,
    PlatformsModule,
    PortfolioModule,
    PortfolioSnapshotQueueModule,
    PrismaModule,
    PropertyModule,
    PublicModule,
    RedisCacheModule,
    ScheduleModule.forRoot(),
    ServeStaticModule.forRoot({
      exclude: [
        `${BULL_BOARD_ROUTE}/*wildcard`,
        '/.well-known/*wildcard',
        '/api/*wildcard',
        '/sitemap.xml'
      ],
      rootPath: join(__dirname, '..', 'client')
    }),
    ServeStaticModule.forRoot({
      rootPath: join(__dirname, '..', 'client', '.well-known'),
      serveRoot: '/.well-known'
    }),
    SitemapModule,
    SubscriptionModule,
    SymbolsModule,
    TagsModule,
    ThrottlerModule.forRootAsync({
      imports: [ConfigurationModule],
      inject: [ConfigurationService],
      useFactory: (configurationService: ConfigurationService) => {
        const isRateLimitingEnabled = configurationService.get(
          'ENABLE_FEATURE_RATE_LIMITING'
        );

        return {
          errorMessage: getReasonPhrase(StatusCodes.TOO_MANY_REQUESTS),
          skipIf: () => {
            return !isRateLimitingEnabled;
          },
          storage: isRateLimitingEnabled
            ? new ThrottlerStorageRedisService({
                ...getRedisConnectionOptions(configurationService),
                // Reject commands immediately while Redis is unavailable
                enableOfflineQueue: false,
                maxRetriesPerRequest: 1
              })
            : undefined,
          throttlers: [
            {
              limit: THROTTLE_DEFAULT_LIMIT,
              ttl: THROTTLE_DEFAULT_TTL
            }
          ]
        };
      }
    }),
    UsersModule,
    WatchlistModule
  ],
  providers: [
    I18nService,
    {
      provide: APP_FILTER,
      useClass: PortfolioSnapshotComputationExceptionFilter
    },
    {
      provide: APP_GUARD,
      useClass: ImpersonationWriteGuard
    }
  ]
})
export class AppModule implements NestModule {
  public configure(consumer: MiddlewareConsumer) {
    consumer.apply(HtmlTemplateMiddleware).forRoutes('*wildcard');
  }
}