-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
62 lines (52 loc) · 1.74 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict';
require(!process.env.TEST ? './bootstrap' : './bootstrap.test');
var logger = require('./helpers/logger');
var config = require('./config')(logger);
var template = require('./template')(config);
/**
* @param {Function} f(app, config, logger, es, amqp)
* @param {Function} fRouteError(err, method, url)
*/
function getConfiguredAPP(f, fRouteError, options) {
var connectAndCheckES = require('./helpers/elasticsearch')(config.elasticsearch);
var connectAndCheckAMQP = require('./helpers/amqp')(config.amqp, logger, options);
async.parallel({
es: connectAndCheckES,
amqp: _.partial(connectAndCheckAMQP)
}, function (err, results) {
if (err) {
logger.error(err);
throw err;
}
logger.info('AMQP & ES ready');
var es = results.es;
var amqp = results.amqp;
amqp.connection.exchange(config.amqp.publish.exchange, {
passive: true,
confirm: true
}, function (exchange) {
amqp.publishExchange = exchange;
// configure the api
var app = require('./api')(config, logger, es, amqp, template, fRouteError);
_.defer(f, app, config, logger, es, amqp);
});
});
}
function defaultAppHandler(app, config, logger /*, es, amqp */ ) {
app.listen(config.statwarn.monitoring.api.port, function () {
logger.info('API listening on ' + config.statwarn.monitoring.api.port);
});
}
function defaultRouteError(err, method, url) {
// @todo track that into stathat/starwarn & co
logger.error(method + url, err);
}
if (module.parent === null) {
// the server was directly launched
getConfiguredAPP(defaultAppHandler, defaultRouteError, {
amqpError: true
});
} else {
// otherwise the server was required by another module
module.exports = getConfiguredAPP;
}