/
index.js
100 lines (92 loc) · 3.61 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var _ = require("lodash");
var winston = require('winston');
var Elasticsearch = require( 'winston-elasticsearch' );
function RoutesManager(options) {
var that = this;
that.options = _.merge({
before: null, // a function to run as a before hook, signature: function(body, headers, callback)
after: null, // a function to run as a after hook, signature: function(err, body, headers),
watch: false, // set to true to enable watching file resources (schemas, templates, etc)
log: { // main logger configuration. info=routes initialization, debug=all messages contents
active: true,
config: { // winston configuration, see https://github.com/flatiron/winston#working-with-multiple-loggers-in-winston
console: {
level: 'info',
colorize: 'true',
label: 'nsi.routes'
}
},
metadata: {} // add metadata to all logs
},
monitor: { // monitoring logger. Used to profile all routes durations.
active: true,
config: { // winston configuration, see https://github.com/flatiron/winston#working-with-multiple-loggers-in-winston
console: {
level: 'info',
colorize: 'true',
label: 'nsi.monitor'
},
elasticsearch: { // send monitoring logs to elasticsearch to maybe use kibana, see https://github.com/jackdbernier/winston-elasticsearch
level: 'error', // error by default = disabling it, just set level to 'info' and here we go
indexName: 'nsi-routes',
source: 'NSI - Routes',
disable_fields: true,
typeName: 'log'
}
},
metadata: {} // add metadata to all logs
}
}, options);
winston.loggers.add('nsi-routes', that.options.log.config);
that.logger = winston.loggers.get('nsi-routes');
that.log = function(level, message, metadata) {
if (that.options.log.active) that.logger.log(level, message, _.extend(metadata || {}, that.options.log.metadata));
};
winston.loggers.add('nsi-monitor', that.options.monitor.config);
that.monitorLogger = winston.loggers.get('nsi-monitor');
that.monitor = function(message, metadata) {
if (that.options.monitor.active) {
that.monitorLogger.info(message, _.extend(metadata, that.options.monitor.metadata), function(err) {
// log failure of the monitor logger in the log logger. This is quite arbitratyr but
// the monitor logger is more susceptible to fail as it can use elasticsearch
if (err) that.logger.error('Monitoring failure', {
originalMessage: message,
originalMetadata: metadata,
error: err
});
});
}
};
}
// For commons routes use getters that use require.
// This prevents loading all routes and their dependancies even if they are not needed.
RoutesManager.prototype.__defineGetter__('wrap', function() {
return require('./lib/wrap');
});
RoutesManager.prototype.__defineGetter__('jsonSchema', function() {
return require('./lib/json-schema');
});
RoutesManager.prototype.__defineGetter__('xsd', function() {
return require('./lib/xsd');
});
RoutesManager.prototype.__defineGetter__('to', function() {
return require('./lib/queues').to;
});
RoutesManager.prototype.__defineGetter__('inOut', function() {
return require('./lib/queues').inOut;
});
RoutesManager.prototype.__defineGetter__('http', function() {
return require('./lib/http');
});
RoutesManager.prototype.__defineGetter__('handlebars', function() {
return require('./lib/handlebars');
});
RoutesManager.prototype.__defineGetter__('xslt', function() {
return require('./lib/xslt');
});
module.exports = function(options) {
var routesManager = new RoutesManager(options);
// bind all functions to the routes manager so that they can be passed by reference
_.bindAll(routesManager);
return routesManager;
};