Created
January 22, 2017 23:17
-
-
Save crizstian/ade8df01d93b8506d3e14de6d84add15 to your computer and use it in GitHub Desktop.
Example for booting movie-service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
// we load all the depencies we need | |
const {EventEmitter} = require('events') | |
const server = require('./server/server') | |
const repository = require('./repository/repository') | |
const config = require('./config/') | |
const mediator = new EventEmitter() | |
// verbose logging when we are starting the server | |
console.log('--- Movies Service ---') | |
console.log('Connecting to movies repository...') | |
// log unhandled execpetions | |
process.on('uncaughtException', (err) => { | |
console.error('Unhandled Exception', err) | |
}) | |
process.on('uncaughtRejection', (err, promise) => { | |
console.error('Unhandled Rejection', err) | |
}) | |
// event listener when the repository has been connected | |
mediator.on('db.ready', (db) => { | |
let rep | |
repository.connect(db) | |
.then(repo => { | |
console.log('Repository Connected. Starting Server') | |
rep = repo | |
return server.start({ | |
port: config.serverSettings.port, | |
repo | |
}) | |
}) | |
.then(app => { | |
console.log(`Server started succesfully, running on port: ${config.serverSettings.port}.`) | |
app.on('close', () => { | |
rep.disconnect() | |
}) | |
}) | |
}) | |
mediator.on('db.error', (err) => { | |
console.error(err) | |
}) | |
// we load the connection to the repository | |
config.db.connect(config.dbSettings, mediator) | |
// init the repository connection, and the event listener will handle the rest | |
mediator.emit('boot.ready') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment