Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to run locally with Docker Compose #182

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
10 changes: 10 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM node:9.0.0-slim

COPY package.json .
COPY package-lock.json .

RUN npm install

COPY . .

CMD npm start
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/
```
19 changes: 19 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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"
7 changes: 6 additions & 1 deletion models/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions mongo-init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
db.createUser(
{
user: "user",
pwd: "pass",
roles: [
{
role: "readWrite",
db: "randomuser"
}
]
}
);
3 changes: 3 additions & 0 deletions settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"port": 3000,
"db": "randomuser",
"dbServer": "db",
"dbUsername": "user",
"dbPassword": "pass",
"maxResults": 5000,
"limit": 20000,
"resetInterval": 300000,
Expand Down
6 changes: 3 additions & 3 deletions sockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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, {
Expand All @@ -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)
Expand Down