-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
92 lines (64 loc) · 2.85 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
'use strict';
var $extend = require('extend');
var $pubsub = require('node-pubsub');
var $bunyanLogger = require('./lib/bunyanLogger');
var $chainBuilderFactory = require('./lib/chainBuilderFactory');
var $constants = require('./lib/constants');
var $defaultSettings = require('./lib/defaultSettings');
var $errorHandler = require('./lib/errorHandler');
var $forwarder = require('./lib/forwarder');
var $httpMessageFactory = require('./lib/httpMessageFactory');
var $playback = require('./lib/playback');
var $pluginLoader = require('./lib/pluginLoader');
var $recorder = require('./lib/recorder');
var $requestContextFactory = require('./lib/requestContextFactory');
var $requestExtractor = require('./lib/requestExtractor');
var $responder = require('./lib/responder');
var $stats = require('./lib/stats');
var $utils = require('./lib/utils');
var $wrapperServer = require('./lib/wrapperServer');
module.exports = Nocca;
// the instance can carry state and allows multiple instances to run at the same time
function Nocca (config) {
// map this to self so there are no this-scope issues
var self = this;
self.initialized = false;
self.config = $extend(true, {}, $defaultSettings, config);
self.constants = $constants;
self.pubsub = $pubsub;
self.logger = new $bunyanLogger(self, self.config.logger);
self.pluginLoader = new $pluginLoader(self);
self.usePlugin = usePlugin;
self.getConfig = $utils.extractConfig;
// load all plugins from config so they can be used when parsing config
self.pluginLoader.registerPlugins();
self.playback = new $playback(self);
self.errorHandler = new $errorHandler(self);
self.requestContextFactory = new $requestContextFactory(self);
self.httpMessageFactory = new $httpMessageFactory(self);
self.requestChainer = new $chainBuilderFactory(self);
self.requestExtractor = new $requestExtractor(self);
self.forwarder = new $forwarder(self);
self.recorder = new $recorder(self);
self.responder = new $responder(self);
self.statsLogger = new $stats(self);
self.wrapperServer = new $wrapperServer(self);
// initialize repositories up front
self.config.repositories.forEach(function (repository) {
self.usePlugin(repository);
});
self.logger.info('---');
self.logger.info('>> Nocca v' + require(__dirname + '/package.json').version + ' initialized');
self.logger.info('---');
// inform all pubsub listeners that Nocca is initialized
self.pubsub.publish(self.constants.PUBSUB_NOCCA_INITIALIZE_PLUGIN);
// and mark ourselves as initialized
self.initialized = true;
function usePlugin (pluginId) {
if (!Array.isArray(pluginId)) {
pluginId = [pluginId];
}
self.logger.debug('Using plugin ' + pluginId[0]);
return self.pluginLoader.instantiatePlugin.apply(null, pluginId);
}
}