Skip to content

Commit

Permalink
Allow downloading results; needs to start server with RAZZLE_PROXY_ES…
Browse files Browse the repository at this point in the history
…_DSN=http://localhost:9200/data_searchui
  • Loading branch information
tiberiuichim committed Nov 11, 2021
1 parent d3300ab commit fea43d0
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/middleware/elasticsearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const esSettingsWhitelist = (name) => ({
GET: [`/_es/${name}/_settings`],
});

const esGetDocWhitelist = (name) => ({
GET: [`/_es/${name}/_doc/.*`],
});

function filterRequests(req, whitelist, retVal) {
const tomatch = whitelist[req.method] || [];
const matches = tomatch.filter((m) => req.url.match(m)).length;
Expand All @@ -32,8 +36,6 @@ function handleSearchRequest(req, res, params) {
delete body.params.config;
}

console.log('handle search', url, urlES);

superagent
.post(url)
.send(body)
Expand Down Expand Up @@ -73,8 +75,6 @@ const handleSearch = (req, res, next, params) => {
if (typeof body === 'string') body = JSON.parse(body);
const { requestType } = body;

// console.log('requestType', requestType, body);

if (requestType) delete body.requestType; // TODO: is this safe?

switch (requestType) {
Expand All @@ -100,11 +100,30 @@ const handleDownload = (req, res, next, { appName, urlNLP, urlES }) => {
download(urlES, appConfig, req, res);
};

const DOC_ID = /.*_doc\/(?<url>.+)/m;

const handleDocRequest = (req, res, next, { urlES, docId }) => {
const url = `${urlES}/_doc/${docId}`;
superagent.get(url).end((err, resp) => {
if (resp && resp.body) res.send(resp.body);
});
};

export const createHandler = ({ urlNLP, urlES }) => {
return function esProxyHandler(req, res, next) {
const appNames = Object.keys(config.settings.searchlib.searchui);

let appName;
appName = appNames
.map((name) => filterRequests(req, esGetDocWhitelist(name), name))
.find((b) => b);

if (appName) {
const docId = req.path.match(DOC_ID).groups['url'];
handleDocRequest(req, res, next, { urlES, docId });
return;
}

appName = appNames
.map((name) => filterRequests(req, esProxyWhitelist(name), name))
.find((b) => b);
Expand Down

0 comments on commit fea43d0

Please sign in to comment.