-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
71 lines (56 loc) · 2.2 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
var express = require('express');
var ConnectionPool = require('./lib/droonga-protocol/connection-pool').ConnectionPool;
var httpAdapter = require('./lib/adapter/http');
var socketIoAdapter = require('./lib/adapter/socket.io');
var dashboardUI = require('./lib/ui/dashboard');
var ConsoleLogger = require('./lib/console-logger').ConsoleLogger;
var LOG_PREFIX = '[global] ';
function droonga(application, params) {
params = params || {};
params.logger = params.logger || new ConsoleLogger();
params.connectionPool = params.connectionPool || new ConnectionPool(params);
var connectionPool = params.connectionPool;
params.prefix = params.prefix || '';
params.prefix = params.prefix.replace(/\/$/, '');
httpAdapter.register(application, params);
if (params.server) {
socketIoAdapter.register(application, params.server, params);
params.server.on('error', function(error) {
params.logger.error(LOG_PREFIX + 'Unhandled Error', error);
if (typeof connectionPool.shutdown == 'function')
connectionPool.shutdown();
});
params.server.on('close', function() {
if (typeof connectionPool.shutdown == 'function')
connectionPool.shutdown();
});
}
dashboardUI.register(application, params);
application.connectionPool = connectionPool;
if (params.syncHostNames &&
typeof connectionPool.startSyncHostNamesFromCluster == 'function') {
params.server.on('listening', function() {
connectionPool.startSyncHostNamesFromCluster();
});
}
application.get(params.prefix + '/engines', function(request, response, next) {
response.jsonp({
clusterId: connectionPool.clusterId,
hostNames: connectionPool.hostNames
});
});
application.get(params.prefix + '/connections', function(request, response, next) {
response.jsonp(connectionPool.getStatus());
});
}
exports.initialize = droonga;
express.application.droonga = function(params) {
droonga(this, params);
};
require('./lib/adapter/api').exportTo(exports);
exports.command = require('./lib/adapter/command');
exports.Cache = require('./lib/cache');
exports.middleware = {
cacheStatistics: require('./lib/middleware/cache-statistics'),
cache: require('./lib/middleware/cache')
};