1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00
unleash.unleash/lib/server-impl.js

55 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-06-18 21:53:18 +02:00
'use strict';
2016-10-26 10:43:11 +02:00
const { EventEmitter } = require('events');
const logger = require('./logger');
const migrator = require('../migrator');
2016-11-13 21:09:00 +01:00
const getApp = require('./app');
const { startMonitoring } = require('./metrics');
const { createStores } = require('./db');
const { createOptions } = require('./options');
function createApp (options) {
// Database dependecies (statefull)
const stores = createStores(options);
const eventBus = new EventEmitter();
2016-06-18 21:53:18 +02:00
const config = {
baseUriPath: options.baseUriPath,
serverMetrics: options.serverMetrics,
enableRequestLogger: options.enableRequestLogger,
port: options.port,
publicFolder: options.publicFolder,
stores,
eventBus,
};
2016-11-13 15:15:52 +01:00
const app = getApp(config);
2016-06-18 21:53:18 +02:00
const server = app.listen(app.get('port'), () => {
logger.info(`Unleash started on ${app.get('port')}`);
});
startMonitoring(options.serverMetrics, eventBus);
return { app, server, eventBus };
}
function start (opts) {
const options = createOptions(opts);
2016-12-03 13:45:22 +01:00
return migrator({ databaseUrl: options.databaseUrl })
2016-11-13 20:43:29 +01:00
.catch(err => logger.error('failed to migrate db', err))
.then(() => createApp(options))
2016-11-13 20:43:29 +01:00
.catch(err => logger.error('failed creating app', err));
}
2016-06-18 21:53:18 +02:00
process.on('uncaughtException', err => {
logger.error('Uncaught Exception:', err.message);
logger.error(err.stack);
});
module.exports = {
2016-06-18 21:55:46 +02:00
start,
};