From 4b0b015a43224046f9999dd3c68e984911086fbf Mon Sep 17 00:00:00 2001 From: Greg Berns Date: Wed, 14 Oct 2020 14:32:20 -0700 Subject: [PATCH] Add ability to run locally with Docker Compose --- .dockerignore | 1 + Dockerfile | 10 ++++++++++ README.md | 11 +++++++++++ docker-compose.yml | 19 +++++++++++++++++++ models/db.js | 7 ++++++- mongo-init.js | 12 ++++++++++++ settings.json | 3 +++ sockets.js | 6 +++--- 8 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 mongo-init.js diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b7dd565 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM node:9.0.0-slim + +COPY package.json . +COPY package-lock.json . + +RUN npm install + +COPY . . + +CMD npm start diff --git a/README.md b/README.md index 93f3844..920f3c3 100644 --- a/README.md +++ b/README.md @@ -47,3 +47,14 @@ Just place your files in a new directory in the `api/.nextRelease/data` folder w ### How to contact us If you have any questions, feel free to ask us on our Twitter page [@randomapi](https://twitter.com/randomapi). + +### To Run Locally with Docker Compose + +```bash +# runs mongo as a background container +docker-compose up --build db -d +# start the service +docker-compose up --build randomuser +# call the API +curl -i localhost:3000/api/ +``` diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..1d0c1a9 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,19 @@ +version: "3.7" + +services: + db: + image: mongo:4.2.10 + restart: always + environment: + MONGO_INITDB_ROOT_USERNAME: user + MONGO_INITDB_ROOT_PASSWORD: pass + MONGO_INITDB_DATABASE: randomuser + ports: + - "27017:27017" + volumes: + - ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro + + randomuser: + build: . + ports: + - "3000:3000" diff --git a/models/db.js b/models/db.js index e9e37e9..bdae0a0 100644 --- a/models/db.js +++ b/models/db.js @@ -3,8 +3,13 @@ const settings = require('../settings'); module.exports = testEnv => { const dbName = settings.db + (testEnv ? '-test' : ''); + const dbServer = settings.dbServer; + const credentials = + settings.dbUsername && settings.dbPassword + ? settings.dbUsername + ':' + settings.dbPassword + '@' + : ''; - mongoose.connect('mongodb://localhost/' + dbName, { + mongoose.connect('mongodb://' + credentials + dbServer + '/' + dbName, { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false, diff --git a/mongo-init.js b/mongo-init.js new file mode 100644 index 0000000..fc61085 --- /dev/null +++ b/mongo-init.js @@ -0,0 +1,12 @@ +db.createUser( + { + user: "user", + pwd: "pass", + roles: [ + { + role: "readWrite", + db: "randomuser" + } + ] + } +); \ No newline at end of file diff --git a/settings.json b/settings.json index 88e473d..33d9130 100644 --- a/settings.json +++ b/settings.json @@ -1,6 +1,9 @@ { "port": 3000, "db": "randomuser", + "dbServer": "db", + "dbUsername": "user", + "dbPassword": "pass", "maxResults": 5000, "limit": 20000, "resetInterval": 300000, diff --git a/sockets.js b/sockets.js index 2c79952..5b045d6 100644 --- a/sockets.js +++ b/sockets.js @@ -25,7 +25,7 @@ function updateStats() { Request.findOne({ date: util.getDateTime() }, (err, obj) => { - if (obj !== null) { + if (obj !== undefined && obj !== null) { cb(err, { total: format(obj.total), bandwidth: filesize(obj.bandwidth) @@ -51,7 +51,7 @@ function updateStats() { "$sort": { "total": -1 } } ], (err, result) => { - if (result.length !== 0) { + if (result !== undefined && result.length !== 0) { cb(err, { total: format(result[0].total), bandwidth: filesize(result[0].bandwidth, { @@ -78,7 +78,7 @@ function updateStats() { } } ], (err, result) => { - if (result.length !== 0) { + if (result !== undefined && result.length !== 0) { cb(err, { total: format(Math.round(result[0].total / 30)), bandwidth: filesize(result[0].bandwidth)