-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (39 loc) · 1.22 KB
/
index.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
/**
* Module dependencies
*/
var dmarshal = require('annex-marshal-msgpack-node');
var metric = require('connect-metric');
var createServer = require('./server');
var createClient = require('./client');
var envs = require('envs');
module.exports = function(opts) {
opts = opts || {};
opts.marshal = opts.marshal || dmarshal;
var app = createServer(opts);
app.use(metric((opts.metric||{}).context, (opts.metric||{}).options));
app.use(function logger(req, res, next) {
var start = Date.now();
res.on('end', function() {
req.metric({
at: 'info',
module: req.module,
method: req.method,
ip: req.ip,
service: (Date.now() - start) + 'ms',
status: res.statusCode,
bytes: res._headers['content-length']
});
})
next();
});
app.durga = createClient(opts.durgaUrl || envs('DURGA_URL'),
opts.serviceEnv || envs('SERVICE_ENV', 'development'),
opts.wsUrl || envs('WS_URL'));
app.on('register:call', function(mod, fn) {
if (app.durga) app.durga.register(0, mod, fn, []);
});
app.on('register:cast', function(mod, fn) {
if (app.durga) app.durga.register(1, mod, fn, []);
});
return app;
};