Skip to content

Commit

Permalink
Allow multiple env vars for DSNs
Browse files Browse the repository at this point in the history
  • Loading branch information
tiberiuichim committed May 12, 2022
1 parent e586013 commit e1b249b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 14 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# volto-searchlib

### Elasticsearch and NLPService middleware

You can configure the proxied Elasticsearch with the env var:

`RAZZLE_PROXY_ES_DSN_${appName}`, so for example `RAZZLE_PROXY_ES_DSN_datahub`.

If you use only one ES server, you can simply set `RAZZLE_PROXY_ES_DSN`.

You can configure the proxied NLPServer with the env var:

`RAZZLE_PROXY_QA_DSN_${appName}`, for example:
`RAZZLE_PROXY_QA_DSN_globalsearch`. You you use only one NLP service, you can
simply set `RAZZLE_PROXY_QA_DSN`. Notice, in principle the NLPService can use
different Elasticsearch indexes.

[![Releases](https://img.shields.io/github/v/release/eea/volto-searchlib)](https://github.com/eea/volto-searchlib/releases)

[![Pipeline](https://ci.eionet.europa.eu/buildStatus/icon?job=volto-addons%2Fvolto-searchlib%2Fmaster&subject=master)](https://ci.eionet.europa.eu/view/Github/job/volto-addons/job/volto-searchlib/job/master/display/redirect)
Expand Down
65 changes: 51 additions & 14 deletions src/middleware/elasticsearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,60 +113,97 @@ const DOC_ID_RE = /.*_doc\/(?<url>.+)/m;

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

const getUrlNLP = (appName) => {
return (
process.env[`RAZZLE_PROXY_QA_DSN_${appName}`] ||
process.env.RAZZLE_PROXY_QA_DSN ||
'http://localhost:8000/api'
);
};

const getUrlES = (appName) => {
return (
process.env[`RAZZLE_PROXY_ES_DSN_${appName}`] ||
process.env.RAZZLE_PROXY_ES_DSN ||
'http://localhost:9200/_all'
);
};

export const createHandler = ({ urlNLP, urlES }) => {
return function esProxyHandler(req, res, next) {
let urlES, urlNLP;

const appNames = Object.keys(config.settings.searchlib.searchui);

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

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

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

if (appName) {
if (searchRequestAppName) {
const body = req.body || {};
const conf =
body.params?.config || config.settings.searchlib.searchui[appName];
body.params?.config ||
config.settings.searchlib.searchui[searchRequestAppName];

log('conf', searchRequestAppName, conf.enableNLP);

log('conf', appName, conf.enableNLP);
urlNLP = getUrlNLP(searchRequestAppName);
urlES = getUrlES(searchRequestAppName);

handleSearch(req, res, next, {
appName,
appName: searchRequestAppName,
urlNLP,
urlES: conf.enableNLP ? urlNLP : urlES,
});
return;
}

appName = appNames
const settingsAppName = appNames
.map((name) => filterRequests(req, esSettingsWhitelist(name), name))
.find((b) => b);

if (appName) {
handleSettings(req, res, next, { appName, urlNLP, urlES });
if (settingsAppName) {
urlNLP = getUrlNLP(settingsAppName);
urlES = getUrlES(settingsAppName);

handleSettings(req, res, next, {
appName: settingsAppName,
urlNLP,
urlES,
});
return;
}

appName = appNames
const downloadAppName = appNames
.map((name) => filterRequests(req, esDownloadWhitelist(name), name))
.find((b) => b);

if (appName) {
handleDownload(req, res, next, { appName, urlNLP, urlES });
if (downloadAppName) {
urlNLP = getUrlNLP(downloadAppName);
urlES = getUrlES(downloadAppName);

handleDownload(req, res, next, {
appName: downloadAppName,
urlNLP,
urlES,
});
return;
}

Expand Down

0 comments on commit e1b249b

Please sign in to comment.