import { EventEmitter } from 'events' ; import * as server from './server' ; import * as repository from './repository' ; import { CONFIG } from './config' ; const mediator = new EventEmitter ( ) ; console . log ( '--- API Gateway Service ---' ) ; process . on ( 'uncaughtException' , ( err ) => { console . error ( 'Unhandled Exception' , err ) ; } ) ; mediator . on ( 'db.ready' , ( db ) => { let rep ; repository . connect ( db ) . then ( async ( repo ) => { console . log ( '--- Repository connected to database ---' ) ; console . log ( '--- Starting Server ---' ) ; rep = repo ; return await server . start ( { serverConfig : CONFIG . serverConfig , repo } ) . catch ( err => err ) ; } ) . then ( service => { console . log ( `Server started succesfully, running on port: ${ CONFIG . serverConfig . port }  🎉` ) ; console . log ( `NODE_ENV: ${ CONFIG . serverConfig . env_name }` ) ; service . server . on ( 'close' , ( ) => { console . log ( '[API GATEWAY] Server close()' ) ; } ) ; } ) . catch ( err => { console . log ( '[ERROR] ' , err ) ; } ) ; } ) ; CONFIG . db . connect ( CONFIG . dbSettings , mediator ) ; mediator . emit ( 'boot.ready' ) ;