Skip to content

Commit

Permalink
Add elasticsearch service, environment and loader
Browse files Browse the repository at this point in the history
Some observations: There are two elasticsearch packages in npm worth
looking at, 'elasticsearch' and '@elastic/elasticsearch'. The second
is meant to replace the first, but mongoose-elasticsearch-xp which will
handle the replication of models apparently uses clients from the first.
Further down the line we can test if '@elastic/elasticsearch' and be
used instead.

The elasticsearch client is uncaptured.

Use makeConfig() to prevent future issues:
See elastic/elasticsearch-js#33
  • Loading branch information
brunodccarvalho committed Mar 20, 2020
1 parent b98eba4 commit 5d8c38f
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 3 deletions.
11 changes: 11 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ DB_PASS=
# Specifies the name of the database to connect to
DB_NAME=nijobs

# Specifies the host for the elasticsearch instance
ES_HOST=localhost
# Specifies the port for the elasticsearch instance
ES_PORT=9200
# For use with external instances if 'http://$HOST:$PORT' is insufficient
ES_URI=
# Specifies the username with which to connect to the instance
ES_USER=
# Specified the password with which to connect to the instance
ES_PASS=

# Session secret - OVERRIDE IN PRODUCTION
SESSION_SECRET=O Rui foi membro do IEEE.

Expand Down
10 changes: 9 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3"
services:
web-prod:
build:
build:
context: .
dockerfile: Dockerfile-prod
environment:
Expand All @@ -10,6 +10,7 @@ services:
- "${PORT}:${PORT}"
depends_on:
- mongo
- elasticsearch
web-dev:
build:
context: .
Expand All @@ -22,6 +23,7 @@ services:
- ./src:/usr/src/nijobs-be/src
depends_on:
- mongo
- elasticsearch
test:
build:
context: .
Expand All @@ -37,3 +39,9 @@ services:
image: mongo
ports:
- "27017:27017"
elasticsearch:
image: elasticsearch:7.6.1
environment:
- discovery.type=single-node
ports:
- "9200:9200"
89 changes: 87 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
"bcrypt": "^3.0.6",
"body-parser": "^1.18.3",
"dotenv-flow": "^3.0.0",
"elasticsearch": "^16.6.0",
"express": "^4.16.4",
"express-session": "^1.15.6",
"express-validator": "^6.2.0",
"http-status-codes": "^1.4.0",
"mongoose": "^5.7.5",
"mongoose-elasticsearch-xp": "^5.8.0",
"mongoose-unique-array": "^0.3.1",
"morgan": "^1.9.1",
"passport": "^0.4.0",
Expand Down
6 changes: 6 additions & 0 deletions src/config/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ module.exports = Object.freeze({
db_pass: process.env.DB_PASS,
db_name: process.env.DB_NAME,

es_host: process.env.ES_HOST,
es_port: process.env.ES_PORT,
es_uri: process.env.ES_URI,
es_user: process.env.ES_USER,
es_pass: process.env.ES_PASS,

session_secret: process.env.SESSION_SECRET,

port: process.env.PORT,
Expand Down
26 changes: 26 additions & 0 deletions src/loaders/elasticsearch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const elasticsearch = require("elasticsearch");
const config = require("../config/env");

const makeConfig = () => {
if (!(config.db_uri || (config.db_host && config.db_port))) {
console.error("Either 'ES_URI' or 'ES_HOST' and 'ES_PORT' must be specified in the env file! See README.md for details.");
process.exit(125);
}

const pass = config.es_pass ? `:${config.es_pass}` : "";
const auth = `${config.es_user}${pass}`;

const uri = config.es_uri || `http://${config.es_host}:${config.es_port}`;

return {
host: uri,
httpAuth: auth,
log: "trace", // development only
apiVersion: "7.6",
name: "nijobs-es",
};
};

const setupClient = () => elasticsearch.Client(makeConfig());

module.exports = setupClient;
3 changes: 3 additions & 0 deletions src/loaders/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const mongooseLoader = require("./mongoose");
const elasticsearchLoader = require("./elasticsearch");
const expressLoader = require("./express");

const setupLoaders = async ({ expressApp }) => {
await mongooseLoader();
console.info("Mongoose DB connection initialized");
await elasticsearchLoader();
console.info("Elasticsearch connection initialized");
await expressLoader(expressApp);
console.info("Express initialized");
require("../config/passport");
Expand Down

0 comments on commit 5d8c38f

Please sign in to comment.