Skip to content

Commit

Permalink
[refactor] add docker and docker compose setup and changed database e…
Browse files Browse the repository at this point in the history
…nviroment variable
  • Loading branch information
JorgeCeja committed May 20, 2018
1 parent f63aa57 commit f46fcdb
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
MONGODB_URI=mongodb://localhost:27017/graphqlTodo
DATABASE_URI=mongodb://localhost:27017/graphql

SESSION_SECRET=Your-Session-Secret-Goes-Here

Expand Down
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM node:8.11.1-alpine

WORKDIR /usr/app

COPY package.json .
RUN npm install -g -s --no-progress yarn && yarn

COPY . .
18 changes: 18 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: '2'
services:
web:
build: .
command: sh -c './wait-for.sh mongodb:27017 && yarn start'
volumes:
- .:/usr/app/
- /usr/app/node_modules
ports:
- '8000:8000'
depends_on:
- mongodb
environment:
DATABASE_URI: mongodb://mongodb:27017/graphql
mongodb:
image: mongo:3.6.4-jessie
ports:
- '27017:27017'
4 changes: 2 additions & 2 deletions src/config/config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const RateLimit = require('express-rate-limit');

const {
NODE_ENV, MONGODB_URI, SESSION_SECRET, PORT
NODE_ENV, DATABASE_URI, SESSION_SECRET, PORT
} = process.env;

const CONFIG = {
SESSION_SECRET,
MONGODB_URI,
DATABASE_URI,
limiter: new RateLimit({
windowMs: 15 * 60 * 1000, // 1 minute
max: 100, // limit each IP to 100 requests per windowMs
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const config = require('./config/config');
mongoose.Promise = global.Promise;

// Set up default mongoose connection
mongoose.connect(config.MONGODB_URI, {
mongoose.connect(config.DATABASE_URI, {
useMongoClient: true
});

Expand Down
60 changes: 60 additions & 0 deletions wait-for.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/sh

# Credit: https://github.com/vnegrisolo/wait-for


log() {
echo "[wait-for] [`date +'%Y%m%d%H%M%S'`] $@"
}

usage() {
echo "Usage: `basename "$0"` [--timeout=15] <HOST>:<PORT> [<HOST_2>:<PORT_2>]"
}

unknown_arg() {
log "[ERROR] unknown argument: '$@'"
usage
exit 2
}

wait_for() {
timeout=$1 && host=$2 && port=$3
log "wait '$host':'$port' up to '$timeout'"
for i in `seq $timeout` ; do
if nc -z "$host" "$port" > /dev/null 2>&1 ; then
log "wait finish '$host:$port' [`expr $(date +%s) - $START`] seconds"
exit 0
fi
log "wait attempt '${host}:${port}' [$i]"
sleep 1
done
log "[ERROR] wait timeout of '$timeout' on '$host:$port'"
exit 1
}

trap 'kill $(jobs -p) &>/dev/null' EXIT

START=$(date +%s)
timeout=15
pids=""
for i in $@ ; do
case $i in
--timeout=*) timeout="${i#*=}" ;;
-t=*) timeout="${i#*=}" ;;
*:* )
wait_for "$timeout" "${i%%:*}" "${i##*:}" &
pids="$pids $!"
;;
*) unknown_arg "$i" ;;
esac
done

status=0
for pid in $pids; do
if ! wait $pid ; then
status=1
fi
done

log "wait done with status=$status"
exit $status

0 comments on commit f46fcdb

Please sign in to comment.