import { Definition } from 'node-dependency-injection' ; import { DomainEvent } from '../../../Contexts/Shared/domain/DomainEvent' ; import { DomainEventSubscriber } from '../../../Contexts/Shared/domain/DomainEventSubscriber' ; import { EventBus } from '../../../Contexts/Shared/domain/EventBus' ; import { DomainEventMapping } from '../../../Contexts/Shared/infrastructure/EventBus/DomainEventMapping' ; import container from './dependency-injection' ; import { Server } from './server' ; export class MoocBackendApp { server ? : Server ; async start ( ) { const port = process . env . PORT || '5000' ; this . server = new Server ( port ) ; await this . registerSubscribers ( ) ; return this . server . listen ( ) ; } async stop ( ) { return this . server ?. stop ( ) ; } get httpServer ( ) { return this . server ?. getHTTPServer ( ) ; } private async registerSubscribers ( ) { const eventBus = container . get ( 'Shared.EventBus' ) as EventBus ; const subscriberDefinitions = container . findTaggedServiceIds ( 'domainEventSubscriber' ) as Map < String , Definition > ; const subscribers = [ ] ; subscriberDefinitions . forEach ( ( value , key ) => subscribers . push ( container . get ( key ) ) ) ; const domainEventMapping = new DomainEventMapping ( subscribers ) ; eventBus . setDomainEventMapping ( domainEventMapping ) ; eventBus . addSubscribers ( subscribers ) ; await eventBus . start ( ) ; } }