123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611 |
- import * as winston from 'winston'
- import { Container, interfaces } from 'inversify'
- import { Env } from './Env'
- import TYPES from './Types'
- import { AppDataSource } from './DataSource'
- import { SNSClient, SNSClientConfig } from '@aws-sdk/client-sns'
- import { ItemRepositoryInterface } from '../Domain/Item/ItemRepositoryInterface'
- import { TypeORMItemRepository } from '../Infra/TypeORM/TypeORMItemRepository'
- import { Repository } from 'typeorm'
- import { Item } from '../Domain/Item/Item'
- import {
- DirectCallDomainEventPublisher,
- DirectCallEventMessageHandler,
- SNSDomainEventPublisher,
- SQSDomainEventSubscriberFactory,
- SQSEventMessageHandler,
- SQSNewRelicEventMessageHandler,
- } from '@standardnotes/domain-events-infra'
- import { DomainEventFactoryInterface } from '../Domain/Event/DomainEventFactoryInterface'
- import { DomainEventFactory } from '../Domain/Event/DomainEventFactory'
- import { Timer, TimerInterface } from '@standardnotes/time'
- import { ItemTransferCalculatorInterface } from '../Domain/Item/ItemTransferCalculatorInterface'
- import { ItemTransferCalculator } from '../Domain/Item/ItemTransferCalculator'
- import { ItemConflict } from '../Domain/Item/ItemConflict'
- import { ContentFilter } from '../Domain/Item/SaveRule/ContentFilter'
- import { ContentTypeFilter } from '../Domain/Item/SaveRule/ContentTypeFilter'
- import { OwnershipFilter } from '../Domain/Item/SaveRule/OwnershipFilter'
- import { TimeDifferenceFilter } from '../Domain/Item/SaveRule/TimeDifferenceFilter'
- import { ItemSaveValidator } from '../Domain/Item/SaveValidator/ItemSaveValidator'
- import { ItemSaveValidatorInterface } from '../Domain/Item/SaveValidator/ItemSaveValidatorInterface'
- import { SyncResponseFactory20161215 } from '../Domain/Item/SyncResponse/SyncResponseFactory20161215'
- import { SyncResponseFactory20200115 } from '../Domain/Item/SyncResponse/SyncResponseFactory20200115'
- import { SyncResponseFactoryResolver } from '../Domain/Item/SyncResponse/SyncResponseFactoryResolver'
- import { SyncResponseFactoryResolverInterface } from '../Domain/Item/SyncResponse/SyncResponseFactoryResolverInterface'
- import { CheckIntegrity } from '../Domain/UseCase/Syncing/CheckIntegrity/CheckIntegrity'
- import { GetItem } from '../Domain/UseCase/Syncing/GetItem/GetItem'
- import { SyncItems } from '../Domain/UseCase/Syncing/SyncItems/SyncItems'
- import { InversifyExpressAuthMiddleware } from '../Infra/InversifyExpressUtils/Middleware/InversifyExpressAuthMiddleware'
- import { S3Client } from '@aws-sdk/client-s3'
- import { SQSClient, SQSClientConfig } from '@aws-sdk/client-sqs'
- import { ContentDecoder } from '@standardnotes/common'
- import {
- DomainEventMessageHandlerInterface,
- DomainEventHandlerInterface,
- DomainEventSubscriberFactoryInterface,
- DomainEventPublisherInterface,
- } from '@standardnotes/domain-events'
- import axios, { AxiosInstance } from 'axios'
- import { AuthHttpServiceInterface } from '../Domain/Auth/AuthHttpServiceInterface'
- import { ExtensionsHttpService } from '../Domain/Extension/ExtensionsHttpService'
- import { ExtensionsHttpServiceInterface } from '../Domain/Extension/ExtensionsHttpServiceInterface'
- import { AccountDeletionRequestedEventHandler } from '../Domain/Handler/AccountDeletionRequestedEventHandler'
- import { DuplicateItemSyncedEventHandler } from '../Domain/Handler/DuplicateItemSyncedEventHandler'
- import { EmailBackupRequestedEventHandler } from '../Domain/Handler/EmailBackupRequestedEventHandler'
- import { ItemRevisionCreationRequestedEventHandler } from '../Domain/Handler/ItemRevisionCreationRequestedEventHandler'
- import { ItemBackupServiceInterface } from '../Domain/Item/ItemBackupServiceInterface'
- import { FSItemBackupService } from '../Infra/FS/FSItemBackupService'
- import { AuthHttpService } from '../Infra/HTTP/AuthHttpService'
- import { S3ItemBackupService } from '../Infra/S3/S3ItemBackupService'
- import { ControllerContainer, ControllerContainerInterface, MapperInterface } from '@standardnotes/domain-core'
- import { HomeServerItemsController } from '../Infra/InversifyExpressUtils/HomeServer/HomeServerItemsController'
- import { Transform } from 'stream'
- import { TypeORMItem } from '../Infra/TypeORM/TypeORMItem'
- import { ItemPersistenceMapper } from '../Mapping/Persistence/ItemPersistenceMapper'
- import { ItemHttpRepresentation } from '../Mapping/Http/ItemHttpRepresentation'
- import { ItemHttpMapper } from '../Mapping/Http/ItemHttpMapper'
- import { SavedItemHttpRepresentation } from '../Mapping/Http/SavedItemHttpRepresentation'
- import { SavedItemHttpMapper } from '../Mapping/Http/SavedItemHttpMapper'
- import { ItemConflictHttpRepresentation } from '../Mapping/Http/ItemConflictHttpRepresentation'
- import { ItemConflictHttpMapper } from '../Mapping/Http/ItemConflictHttpMapper'
- import { ItemBackupRepresentation } from '../Mapping/Backup/ItemBackupRepresentation'
- import { ItemBackupMapper } from '../Mapping/Backup/ItemBackupMapper'
- import { SaveNewItem } from '../Domain/UseCase/Syncing/SaveNewItem/SaveNewItem'
- import { UpdateExistingItem } from '../Domain/UseCase/Syncing/UpdateExistingItem/UpdateExistingItem'
- import { GetItems } from '../Domain/UseCase/Syncing/GetItems/GetItems'
- import { SaveItems } from '../Domain/UseCase/Syncing/SaveItems/SaveItems'
- import { ItemHashHttpMapper } from '../Mapping/Http/ItemHashHttpMapper'
- import { ItemHash } from '../Domain/Item/ItemHash'
- import { ItemHashHttpRepresentation } from '../Mapping/Http/ItemHashHttpRepresentation'
- import { TypeORMKeySystemAssociation } from '../Infra/TypeORM/TypeORMKeySystemAssociation'
- import { SharedVaultAssociation } from '../Domain/SharedVault/SharedVaultAssociation'
- import { TypeORMSharedVaultAssociation } from '../Infra/TypeORM/TypeORMSharedVaultAssociation'
- import { SharedVaultAssociationPersistenceMapper } from '../Mapping/Persistence/SharedVaultAssociationPersistenceMapper'
- import { TypeORMKeySystemAssociationRepository } from '../Infra/TypeORM/TypeORMKeySystemAssociationRepository'
- import { SharedVaultAssociationRepositoryInterface } from '../Domain/SharedVault/SharedVaultAssociationRepositoryInterface'
- import { TypeORMSharedVaultAssociationRepository } from '../Infra/TypeORM/TypeORMSharedVaultAssociationRepository'
- import { KeySystemAssociation } from '../Domain/KeySystem/KeySystemAssociation'
- import { KeySystemAssociationRepositoryInterface } from '../Domain/KeySystem/KeySystemAssociationRepositoryInterface'
- import { KeySystemAssociationPersistenceMapper } from '../Mapping/Persistence/KeySystemAssociationPersistenceMapper'
- export class ContainerConfigLoader {
- private readonly DEFAULT_CONTENT_SIZE_TRANSFER_LIMIT = 10_000_000
- private readonly DEFAULT_MAX_ITEMS_LIMIT = 300
- private readonly DEFAULT_FILE_UPLOAD_PATH = `${__dirname}/../../uploads`
- async load(configuration?: {
- controllerConatiner?: ControllerContainerInterface
- directCallDomainEventPublisher?: DirectCallDomainEventPublisher
- logger?: Transform
- environmentOverrides?: { [name: string]: string }
- }): Promise<Container> {
- const directCallDomainEventPublisher =
- configuration?.directCallDomainEventPublisher ?? new DirectCallDomainEventPublisher()
- const env: Env = new Env(configuration?.environmentOverrides)
- env.load()
- const container = new Container({
- defaultScope: 'Singleton',
- })
- let logger: winston.Logger
- if (configuration?.logger) {
- logger = configuration.logger as winston.Logger
- } else {
- const winstonFormatters = [winston.format.splat(), winston.format.json()]
- if (env.get('NEW_RELIC_ENABLED', true) === 'true') {
- await import('newrelic')
- // eslint-disable-next-line @typescript-eslint/no-var-requires
- const newrelicFormatter = require('@newrelic/winston-enricher')
- const newrelicWinstonFormatter = newrelicFormatter(winston)
- winstonFormatters.push(newrelicWinstonFormatter())
- }
- logger = winston.createLogger({
- level: env.get('LOG_LEVEL', true) || 'info',
- format: winston.format.combine(...winstonFormatters),
- transports: [new winston.transports.Console({ level: env.get('LOG_LEVEL', true) || 'info' })],
- defaultMeta: { service: 'syncing-server' },
- })
- }
- container.bind<winston.Logger>(TYPES.Sync_Logger).toConstantValue(logger)
- const appDataSource = new AppDataSource(env)
- await appDataSource.initialize()
- logger.debug('Database initialized')
- container.bind<TimerInterface>(TYPES.Sync_Timer).toConstantValue(new Timer())
- const isConfiguredForHomeServer = env.get('MODE', true) === 'home-server'
- container.bind<Env>(TYPES.Sync_Env).toConstantValue(env)
- if (isConfiguredForHomeServer) {
- container
- .bind<DomainEventPublisherInterface>(TYPES.Sync_DomainEventPublisher)
- .toConstantValue(directCallDomainEventPublisher)
- } else {
- container.bind(TYPES.Sync_SNS_TOPIC_ARN).toConstantValue(env.get('SNS_TOPIC_ARN'))
- container.bind(TYPES.Sync_SNS_AWS_REGION).toConstantValue(env.get('SNS_AWS_REGION', true))
- container.bind(TYPES.Sync_SQS_QUEUE_URL).toConstantValue(env.get('SQS_QUEUE_URL'))
- container.bind(TYPES.Sync_S3_AWS_REGION).toConstantValue(env.get('S3_AWS_REGION', true))
- container.bind(TYPES.Sync_S3_BACKUP_BUCKET_NAME).toConstantValue(env.get('S3_BACKUP_BUCKET_NAME', true))
- container.bind(TYPES.Sync_EXTENSIONS_SERVER_URL).toConstantValue(env.get('EXTENSIONS_SERVER_URL', true))
- container.bind<SNSClient>(TYPES.Sync_SNS).toDynamicValue((context: interfaces.Context) => {
- const env: Env = context.container.get(TYPES.Sync_Env)
- const snsConfig: SNSClientConfig = {
- apiVersion: 'latest',
- region: env.get('SNS_AWS_REGION', true),
- }
- if (env.get('SNS_ENDPOINT', true)) {
- snsConfig.endpoint = env.get('SNS_ENDPOINT', true)
- }
- if (env.get('SNS_ACCESS_KEY_ID', true) && env.get('SNS_SECRET_ACCESS_KEY', true)) {
- snsConfig.credentials = {
- accessKeyId: env.get('SNS_ACCESS_KEY_ID', true),
- secretAccessKey: env.get('SNS_SECRET_ACCESS_KEY', true),
- }
- }
- return new SNSClient(snsConfig)
- })
- container
- .bind<DomainEventPublisherInterface>(TYPES.Sync_DomainEventPublisher)
- .toDynamicValue((context: interfaces.Context) => {
- return new SNSDomainEventPublisher(
- context.container.get(TYPES.Sync_SNS),
- context.container.get(TYPES.Sync_SNS_TOPIC_ARN),
- )
- })
- container.bind<SQSClient>(TYPES.Sync_SQS).toDynamicValue((context: interfaces.Context) => {
- const env: Env = context.container.get(TYPES.Sync_Env)
- const sqsConfig: SQSClientConfig = {
- region: env.get('SQS_AWS_REGION'),
- }
- if (env.get('SQS_ENDPOINT', true)) {
- sqsConfig.endpoint = env.get('SQS_ENDPOINT', true)
- }
- if (env.get('SQS_ACCESS_KEY_ID', true) && env.get('SQS_SECRET_ACCESS_KEY', true)) {
- sqsConfig.credentials = {
- accessKeyId: env.get('SQS_ACCESS_KEY_ID', true),
- secretAccessKey: env.get('SQS_SECRET_ACCESS_KEY', true),
- }
- }
- return new SQSClient(sqsConfig)
- })
- container.bind<S3Client | undefined>(TYPES.Sync_S3).toDynamicValue((context: interfaces.Context) => {
- const env: Env = context.container.get(TYPES.Sync_Env)
- let s3Client = undefined
- if (env.get('S3_AWS_REGION', true)) {
- s3Client = new S3Client({
- apiVersion: 'latest',
- region: env.get('S3_AWS_REGION', true),
- })
- }
- return s3Client
- })
- }
- // Mapping
- container
- .bind<MapperInterface<Item, TypeORMItem>>(TYPES.Sync_ItemPersistenceMapper)
- .toConstantValue(new ItemPersistenceMapper())
- container
- .bind<MapperInterface<ItemHash, ItemHashHttpRepresentation>>(TYPES.Sync_ItemHashHttpMapper)
- .toConstantValue(new ItemHashHttpMapper())
- container
- .bind<MapperInterface<Item, ItemHttpRepresentation>>(TYPES.Sync_ItemHttpMapper)
- .toConstantValue(new ItemHttpMapper(container.get(TYPES.Sync_Timer)))
- container
- .bind<MapperInterface<Item, SavedItemHttpRepresentation>>(TYPES.Sync_SavedItemHttpMapper)
- .toConstantValue(new SavedItemHttpMapper(container.get(TYPES.Sync_Timer)))
- container
- .bind<MapperInterface<ItemConflict, ItemConflictHttpRepresentation>>(TYPES.Sync_ItemConflictHttpMapper)
- .toConstantValue(
- new ItemConflictHttpMapper(
- container.get(TYPES.Sync_ItemHttpMapper),
- container.get(TYPES.Sync_ItemHashHttpMapper),
- ),
- )
- container
- .bind<MapperInterface<Item, ItemBackupRepresentation>>(TYPES.Sync_ItemBackupMapper)
- .toConstantValue(new ItemBackupMapper(container.get(TYPES.Sync_Timer)))
- container
- .bind<MapperInterface<KeySystemAssociation, TypeORMKeySystemAssociation>>(
- TYPES.Sync_KeySystemAssociationPersistenceMapper,
- )
- .toConstantValue(new KeySystemAssociationPersistenceMapper())
- container
- .bind<MapperInterface<SharedVaultAssociation, TypeORMSharedVaultAssociation>>(
- TYPES.Sync_SharedVaultAssociationPersistenceMapper,
- )
- .toConstantValue(new SharedVaultAssociationPersistenceMapper())
- // ORM
- container
- .bind<Repository<TypeORMItem>>(TYPES.Sync_ORMItemRepository)
- .toDynamicValue(() => appDataSource.getRepository(TypeORMItem))
- container
- .bind<Repository<TypeORMSharedVaultAssociation>>(TYPES.Sync_ORMSharedVaultAssociationRepository)
- .toConstantValue(appDataSource.getRepository(TypeORMSharedVaultAssociation))
- container
- .bind<Repository<TypeORMKeySystemAssociation>>(TYPES.Sync_ORMKeySystemAssociationRepository)
- .toConstantValue(appDataSource.getRepository(TypeORMKeySystemAssociation))
- // Repositories
- container
- .bind<KeySystemAssociationRepositoryInterface>(TYPES.Sync_KeySystemAssociationRepository)
- .toConstantValue(
- new TypeORMKeySystemAssociationRepository(
- container.get(TYPES.Sync_ORMKeySystemAssociationRepository),
- container.get(TYPES.Sync_KeySystemAssociationPersistenceMapper),
- ),
- )
- container
- .bind<SharedVaultAssociationRepositoryInterface>(TYPES.Sync_SharedVaultAssociationRepository)
- .toConstantValue(
- new TypeORMSharedVaultAssociationRepository(
- container.get(TYPES.Sync_ORMSharedVaultAssociationRepository),
- container.get(TYPES.Sync_SharedVaultAssociationPersistenceMapper),
- ),
- )
- container
- .bind<ItemRepositoryInterface>(TYPES.Sync_ItemRepository)
- .toConstantValue(
- new TypeORMItemRepository(
- container.get(TYPES.Sync_ORMItemRepository),
- container.get(TYPES.Sync_ItemPersistenceMapper),
- container.get(TYPES.Sync_KeySystemAssociationRepository),
- container.get(TYPES.Sync_SharedVaultAssociationRepository),
- ),
- )
- container
- .bind<DomainEventFactoryInterface>(TYPES.Sync_DomainEventFactory)
- .toDynamicValue((context: interfaces.Context) => {
- return new DomainEventFactory(context.container.get(TYPES.Sync_Timer))
- })
- container
- .bind<ItemTransferCalculatorInterface>(TYPES.Sync_ItemTransferCalculator)
- .toDynamicValue((context: interfaces.Context) => {
- return new ItemTransferCalculator(
- context.container.get(TYPES.Sync_ItemRepository),
- context.container.get(TYPES.Sync_Logger),
- )
- })
- // Middleware
- container
- .bind<InversifyExpressAuthMiddleware>(TYPES.Sync_AuthMiddleware)
- .toDynamicValue((context: interfaces.Context) => {
- return new InversifyExpressAuthMiddleware(
- context.container.get(TYPES.Sync_AUTH_JWT_SECRET),
- context.container.get(TYPES.Sync_Logger),
- )
- })
- // env vars
- container.bind(TYPES.Sync_AUTH_JWT_SECRET).toConstantValue(env.get('AUTH_JWT_SECRET'))
- container
- .bind(TYPES.Sync_REVISIONS_FREQUENCY)
- .toConstantValue(env.get('REVISIONS_FREQUENCY', true) ? +env.get('REVISIONS_FREQUENCY', true) : 300)
- container.bind(TYPES.Sync_NEW_RELIC_ENABLED).toConstantValue(env.get('NEW_RELIC_ENABLED', true))
- container.bind(TYPES.Sync_VERSION).toConstantValue(env.get('VERSION', true) ?? 'development')
- container
- .bind(TYPES.Sync_CONTENT_SIZE_TRANSFER_LIMIT)
- .toConstantValue(
- env.get('CONTENT_SIZE_TRANSFER_LIMIT', true)
- ? +env.get('CONTENT_SIZE_TRANSFER_LIMIT', true)
- : this.DEFAULT_CONTENT_SIZE_TRANSFER_LIMIT,
- )
- container
- .bind(TYPES.Sync_MAX_ITEMS_LIMIT)
- .toConstantValue(
- env.get('MAX_ITEMS_LIMIT', true) ? +env.get('MAX_ITEMS_LIMIT', true) : this.DEFAULT_MAX_ITEMS_LIMIT,
- )
- container.bind<OwnershipFilter>(TYPES.Sync_OwnershipFilter).toConstantValue(new OwnershipFilter())
- container
- .bind<TimeDifferenceFilter>(TYPES.Sync_TimeDifferenceFilter)
- .toConstantValue(new TimeDifferenceFilter(container.get(TYPES.Sync_Timer)))
- container.bind<ContentTypeFilter>(TYPES.Sync_ContentTypeFilter).toConstantValue(new ContentTypeFilter())
- container.bind<ContentFilter>(TYPES.Sync_ContentFilter).toConstantValue(new ContentFilter())
- container
- .bind<ItemSaveValidatorInterface>(TYPES.Sync_ItemSaveValidator)
- .toConstantValue(
- new ItemSaveValidator([
- container.get(TYPES.Sync_OwnershipFilter),
- container.get(TYPES.Sync_TimeDifferenceFilter),
- container.get(TYPES.Sync_ContentTypeFilter),
- container.get(TYPES.Sync_ContentFilter),
- ]),
- )
- // use cases
- container
- .bind<GetItems>(TYPES.Sync_GetItems)
- .toConstantValue(
- new GetItems(
- container.get(TYPES.Sync_ItemRepository),
- container.get(TYPES.Sync_CONTENT_SIZE_TRANSFER_LIMIT),
- container.get(TYPES.Sync_ItemTransferCalculator),
- container.get(TYPES.Sync_Timer),
- container.get(TYPES.Sync_MAX_ITEMS_LIMIT),
- ),
- )
- container
- .bind<SaveNewItem>(TYPES.Sync_SaveNewItem)
- .toConstantValue(
- new SaveNewItem(
- container.get(TYPES.Sync_ItemRepository),
- container.get(TYPES.Sync_Timer),
- container.get(TYPES.Sync_DomainEventPublisher),
- container.get(TYPES.Sync_DomainEventFactory),
- ),
- )
- container
- .bind<UpdateExistingItem>(TYPES.Sync_UpdateExistingItem)
- .toConstantValue(
- new UpdateExistingItem(
- container.get(TYPES.Sync_ItemRepository),
- container.get(TYPES.Sync_Timer),
- container.get(TYPES.Sync_DomainEventPublisher),
- container.get(TYPES.Sync_DomainEventFactory),
- container.get(TYPES.Sync_REVISIONS_FREQUENCY),
- ),
- )
- container
- .bind<SaveItems>(TYPES.Sync_SaveItems)
- .toConstantValue(
- new SaveItems(
- container.get(TYPES.Sync_ItemSaveValidator),
- container.get(TYPES.Sync_ItemRepository),
- container.get(TYPES.Sync_Timer),
- container.get(TYPES.Sync_SaveNewItem),
- container.get(TYPES.Sync_UpdateExistingItem),
- container.get(TYPES.Sync_Logger),
- ),
- )
- container
- .bind<SyncItems>(TYPES.Sync_SyncItems)
- .toConstantValue(
- new SyncItems(
- container.get(TYPES.Sync_ItemRepository),
- container.get(TYPES.Sync_GetItems),
- container.get(TYPES.Sync_SaveItems),
- ),
- )
- container.bind<CheckIntegrity>(TYPES.Sync_CheckIntegrity).toDynamicValue((context: interfaces.Context) => {
- return new CheckIntegrity(context.container.get(TYPES.Sync_ItemRepository))
- })
- container.bind<GetItem>(TYPES.Sync_GetItem).toDynamicValue((context: interfaces.Context) => {
- return new GetItem(context.container.get(TYPES.Sync_ItemRepository))
- })
- // Services
- container
- .bind<SyncResponseFactory20161215>(TYPES.Sync_SyncResponseFactory20161215)
- .toConstantValue(new SyncResponseFactory20161215(container.get(TYPES.Sync_ItemHttpMapper)))
- container
- .bind<SyncResponseFactory20200115>(TYPES.Sync_SyncResponseFactory20200115)
- .toConstantValue(
- new SyncResponseFactory20200115(
- container.get(TYPES.Sync_ItemHttpMapper),
- container.get(TYPES.Sync_ItemConflictHttpMapper),
- container.get(TYPES.Sync_SavedItemHttpMapper),
- ),
- )
- container
- .bind<SyncResponseFactoryResolverInterface>(TYPES.Sync_SyncResponseFactoryResolver)
- .toDynamicValue((context: interfaces.Context) => {
- return new SyncResponseFactoryResolver(
- context.container.get(TYPES.Sync_SyncResponseFactory20161215),
- context.container.get(TYPES.Sync_SyncResponseFactory20200115),
- )
- })
- // env vars
- container
- .bind(TYPES.Sync_EMAIL_ATTACHMENT_MAX_BYTE_SIZE)
- .toConstantValue(
- env.get('EMAIL_ATTACHMENT_MAX_BYTE_SIZE', true) ? +env.get('EMAIL_ATTACHMENT_MAX_BYTE_SIZE', true) : 10485760,
- )
- container.bind(TYPES.Sync_NEW_RELIC_ENABLED).toConstantValue(env.get('NEW_RELIC_ENABLED', true))
- container
- .bind(TYPES.Sync_FILE_UPLOAD_PATH)
- .toConstantValue(
- env.get('FILE_UPLOAD_PATH', true) ? env.get('FILE_UPLOAD_PATH', true) : this.DEFAULT_FILE_UPLOAD_PATH,
- )
- // Handlers
- container
- .bind<DuplicateItemSyncedEventHandler>(TYPES.Sync_DuplicateItemSyncedEventHandler)
- .toDynamicValue((context: interfaces.Context) => {
- return new DuplicateItemSyncedEventHandler(
- context.container.get(TYPES.Sync_ItemRepository),
- context.container.get(TYPES.Sync_DomainEventFactory),
- context.container.get(TYPES.Sync_DomainEventPublisher),
- context.container.get(TYPES.Sync_Logger),
- )
- })
- container
- .bind<AccountDeletionRequestedEventHandler>(TYPES.Sync_AccountDeletionRequestedEventHandler)
- .toDynamicValue((context: interfaces.Context) => {
- return new AccountDeletionRequestedEventHandler(
- context.container.get(TYPES.Sync_ItemRepository),
- context.container.get(TYPES.Sync_Logger),
- )
- })
- container
- .bind<ItemRevisionCreationRequestedEventHandler>(TYPES.Sync_ItemRevisionCreationRequestedEventHandler)
- .toDynamicValue((context: interfaces.Context) => {
- return new ItemRevisionCreationRequestedEventHandler(
- context.container.get(TYPES.Sync_ItemRepository),
- context.container.get(TYPES.Sync_ItemBackupService),
- context.container.get(TYPES.Sync_DomainEventFactory),
- context.container.get(TYPES.Sync_DomainEventPublisher),
- )
- })
- // Services
- container.bind<ContentDecoder>(TYPES.Sync_ContentDecoder).toDynamicValue(() => new ContentDecoder())
- container.bind<AxiosInstance>(TYPES.Sync_HTTPClient).toDynamicValue(() => axios.create())
- container
- .bind<ExtensionsHttpServiceInterface>(TYPES.Sync_ExtensionsHttpService)
- .toDynamicValue((context: interfaces.Context) => {
- return new ExtensionsHttpService(
- context.container.get(TYPES.Sync_HTTPClient),
- context.container.get(TYPES.Sync_ItemRepository),
- context.container.get(TYPES.Sync_ContentDecoder),
- context.container.get(TYPES.Sync_DomainEventPublisher),
- context.container.get(TYPES.Sync_DomainEventFactory),
- context.container.get(TYPES.Sync_Logger),
- )
- })
- container
- .bind<ItemBackupServiceInterface>(TYPES.Sync_ItemBackupService)
- .toDynamicValue((context: interfaces.Context) => {
- const env: Env = context.container.get(TYPES.Sync_Env)
- if (env.get('S3_AWS_REGION', true)) {
- return new S3ItemBackupService(
- context.container.get(TYPES.Sync_S3_BACKUP_BUCKET_NAME),
- context.container.get(TYPES.Sync_ItemBackupMapper),
- context.container.get(TYPES.Sync_ItemHttpMapper),
- context.container.get(TYPES.Sync_Logger),
- context.container.get(TYPES.Sync_S3),
- )
- } else {
- return new FSItemBackupService(
- context.container.get(TYPES.Sync_FILE_UPLOAD_PATH),
- context.container.get(TYPES.Sync_ItemBackupMapper),
- context.container.get(TYPES.Sync_Logger),
- )
- }
- })
- const eventHandlers: Map<string, DomainEventHandlerInterface> = new Map([
- ['DUPLICATE_ITEM_SYNCED', container.get(TYPES.Sync_DuplicateItemSyncedEventHandler)],
- ['ACCOUNT_DELETION_REQUESTED', container.get(TYPES.Sync_AccountDeletionRequestedEventHandler)],
- ['ITEM_REVISION_CREATION_REQUESTED', container.get(TYPES.Sync_ItemRevisionCreationRequestedEventHandler)],
- ])
- if (!isConfiguredForHomeServer) {
- container.bind(TYPES.Sync_AUTH_SERVER_URL).toConstantValue(env.get('AUTH_SERVER_URL'))
- container
- .bind<AuthHttpServiceInterface>(TYPES.Sync_AuthHttpService)
- .toDynamicValue((context: interfaces.Context) => {
- return new AuthHttpService(
- context.container.get(TYPES.Sync_HTTPClient),
- context.container.get(TYPES.Sync_AUTH_SERVER_URL),
- )
- })
- container
- .bind<EmailBackupRequestedEventHandler>(TYPES.Sync_EmailBackupRequestedEventHandler)
- .toDynamicValue((context: interfaces.Context) => {
- return new EmailBackupRequestedEventHandler(
- context.container.get(TYPES.Sync_ItemRepository),
- context.container.get(TYPES.Sync_AuthHttpService),
- context.container.get(TYPES.Sync_ItemBackupService),
- context.container.get(TYPES.Sync_DomainEventPublisher),
- context.container.get(TYPES.Sync_DomainEventFactory),
- context.container.get(TYPES.Sync_EMAIL_ATTACHMENT_MAX_BYTE_SIZE),
- context.container.get(TYPES.Sync_ItemTransferCalculator),
- context.container.get(TYPES.Sync_S3_BACKUP_BUCKET_NAME),
- context.container.get(TYPES.Sync_Logger),
- )
- })
- eventHandlers.set('EMAIL_BACKUP_REQUESTED', container.get(TYPES.Sync_EmailBackupRequestedEventHandler))
- }
- if (isConfiguredForHomeServer) {
- const directCallEventMessageHandler = new DirectCallEventMessageHandler(
- eventHandlers,
- container.get(TYPES.Sync_Logger),
- )
- directCallDomainEventPublisher.register(directCallEventMessageHandler)
- container
- .bind<DomainEventMessageHandlerInterface>(TYPES.Sync_DomainEventMessageHandler)
- .toConstantValue(directCallEventMessageHandler)
- } else {
- container
- .bind<DomainEventMessageHandlerInterface>(TYPES.Sync_DomainEventMessageHandler)
- .toConstantValue(
- env.get('NEW_RELIC_ENABLED', true) === 'true'
- ? new SQSNewRelicEventMessageHandler(eventHandlers, container.get(TYPES.Sync_Logger))
- : new SQSEventMessageHandler(eventHandlers, container.get(TYPES.Sync_Logger)),
- )
- }
- container
- .bind<DomainEventSubscriberFactoryInterface>(TYPES.Sync_DomainEventSubscriberFactory)
- .toDynamicValue((context: interfaces.Context) => {
- return new SQSDomainEventSubscriberFactory(
- context.container.get(TYPES.Sync_SQS),
- context.container.get(TYPES.Sync_SQS_QUEUE_URL),
- context.container.get(TYPES.Sync_DomainEventMessageHandler),
- )
- })
- container
- .bind<ControllerContainerInterface>(TYPES.Sync_ControllerContainer)
- .toConstantValue(configuration?.controllerConatiner ?? new ControllerContainer())
- if (isConfiguredForHomeServer) {
- container
- .bind<HomeServerItemsController>(TYPES.Sync_HomeServerItemsController)
- .toConstantValue(
- new HomeServerItemsController(
- container.get(TYPES.Sync_SyncItems),
- container.get(TYPES.Sync_CheckIntegrity),
- container.get(TYPES.Sync_GetItem),
- container.get(TYPES.Sync_ItemHttpMapper),
- container.get(TYPES.Sync_SyncResponseFactoryResolver),
- container.get(TYPES.Sync_ControllerContainer),
- ),
- )
- }
- logger.debug('Configuration complete')
- return container
- }
- }
|