This repository has been archived by the owner on Aug 19, 2019. It is now read-only.
forked from cpsgroup/sarapis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsarapis.js
154 lines (136 loc) · 5.22 KB
/
sarapis.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
var hapi = require('hapi');
var Joi = require('joi');
var bunyan = require('bunyan');
//setup loggers
var cliLog = bunyan.createLogger({
name: 'sarapis-cli', level: 'debug', streams: [{
stream: process.stdout, level: 'debug'
}, {path: './sarapis-cli.log', level: 'debug'}]
});
var restLog = bunyan.createLogger({
name: 'sarapis-rest', level: 'debug', streams: [{
stream: process.stdout, level: 'debug'
}, {path: './sarapis-rest.log', level: 'debug'}]
});
if (process.env.NODE_ENV == 'test') {
cliLog.level('error');
restLog.level('error');
}
else if (process.env.NODE_ENV == 'production') {
cliLog.level('info');
restLog.level('info');
}
//process command line arguments
//skip this step if we're running tests
var argv = [];
if (process.env.NODE_ENV != 'test') {
argv = require('yargs')
.usage('Usage: $0 --solr-host [string] --solr-port [num]')
.demand(['solr-host', 'solr-port'])
.describe('solr-host', 'The host address of a Solr instance to connect to.')
.describe('solr-port', 'The port of a Solr instance to connect to.')
.describe('solr-basepath', 'The basepath to the solr instance (Default: /solr).')
.describe('solr-core', 'The core name of a Solr instance to connect to.')
.describe('valid-subpaths', 'Comma seperated subpaths that should be accessible over the proxy (Default: /select')
.describe('solr-allow', 'The methods to be allowed on the Solr instance(s).')
.describe('solr-deny', 'The parameters to be prohibited on the Solr instance(s).')
.describe('proxy-port', 'The port of the Solr proxy.')
.describe('sarapis-port', 'The port of this Sarapis server instance.')
.describe('query-parser', 'The query-parser that should be used (Default one of solr-server will be used).')
.version(function () {
return require('./package.json').version;
})
.epilog('Copyright 2015 CPS Group\nhttp://cpsgroup.github.io/')
.argv;
}
cliLog.info(argv);
//setup some sensible defaults for our variables
var SOLR_HOST = argv.solrHost || 'localhost';
var SOLR_PORT = argv.solrPort || 8983;
var SOLR_BASEPATH = argv.solrBasepath || ['/solr'];
var SOLR_CORE = argv.solrCore;
var VALID_SUBPATHS = argv.validSubpaths;
var SOLR_VALID_METHODS = argv.solrAllow || ['GET', 'HEAD'];
var SOLR_INVALID_PARAMS = argv.solrDeny || ['qt', 'stream'];
var PROXY_HOST = argv.proxyHost || 'localhost';
var PROXY_PORT = argv.proxyPort || 9090;
var SARAPIS_PORT = argv.sarapisPort || 3000;
//Process the valid paths
var basicPath = SOLR_BASEPATH;
var validPaths = '';
if (SOLR_CORE != undefined) {
basicPath = basicPath + '/' + SOLR_CORE;
}
if (VALID_SUBPATHS == undefined) {
//Only the default path is valid
validPaths = basicPath + '/select';
}else{
var str = JSON.stringify(VALID_SUBPATHS);
str = str.replace(/\"/g, "");
str.split(',').forEach(function(validPath) {
//Split the subpaths and create a comma seperated list of full valid paths with basepath/core/subpath
validPaths = basicPath + validPath + ',' + validPaths;
});
}
cliLog.info("The valis paths are: " + validPaths);
//setup proxy options
var solrProxy = require('solr-proxy');
var solrProxyOptions = {
listenPort: PROXY_PORT,
validHttpMethods: SOLR_VALID_METHODS,
validPaths: validPaths,
invalidParams: SOLR_INVALID_PARAMS,
backend: {
host: SOLR_HOST,
port: SOLR_PORT
}
};
//setup our solr client against the proxy
var solrClient = require('solr-client');
var proxyClient = solrClient.createClient(PROXY_HOST, PROXY_PORT, SOLR_CORE, SOLR_BASEPATH);
//setup basic options for our rest server
var restServer = new hapi.Server();
var server = restServer;
module.exports = server;
restServer.connection({port: SARAPIS_PORT, labels: ['api']});
//register rest routes
module.exports.registerRoutes = function () {
//setup endpoints
// pass on solr connection and log callback
var solrQuery = require('./route/solr/query')(proxyClient, function (level, message) {
restServer.log(level, message)
});
var base = require('./route/base');
var adminInfo = require('./route/admin/info');
var adminInspect = require('./route/admin/log/inspect');
var adminSettings = require('./route/admin/log/settings');
var web = require('./route/static/web');
//add routes to server
restServer.route(base.GET);
restServer.route(solrQuery.GET);
restServer.route(adminInfo.GET);
restServer.route(adminInspect.GET);
restServer.route(adminSettings.GET);
restServer.route(adminSettings.POST);
restServer.route(web.GET);
};
//register rest routes
module.exports.registerPlugins = function () {
//setup plugins
var bunyanPlugin = require('./plugin/bunyan')(restLog);
var swaggerPlugins = require('./plugin/swagger');
//add plugins to server
restServer.register(bunyanPlugin, bunyanPlugin.callback);
restServer.register(swaggerPlugins.core, swaggerPlugins.core.route, swaggerPlugins.core.error);
restServer.register(swaggerPlugins.ui, swaggerPlugins.ui.route, swaggerPlugins.ui.error);
};
//finally startup proxy and rest server
//skip this step if loading this module from externally, e.g. for testing
if (!module.parent) {
solrProxy.start(9090, solrProxyOptions); //the port option is mandatory even though being overwritten by the options
module.exports.registerRoutes();
module.exports.registerPlugins();
restServer.start(function () {
restServer.log('info', 'Server running at: ' + restServer.info.uri);
});
}