Last active
April 10, 2017 14:08
-
-
Save nhusher/658dce62e8ddad36ce6f08f54f20df1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: '3' | |
services: | |
db: | |
image: mongo | |
# ports: | |
# - "27017:27017" | |
my-project: | |
image: my-project | |
build: | |
context: . | |
links: | |
- db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM node:6.9.4-alpine | |
WORKDIR /app | |
ADD package.json ./ | |
RUN npm install --production | |
ADD server.js ./ | |
CMD npm start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "test-dc", | |
"version": "1.0.0", | |
"description": "", | |
"main": "server.js", | |
"scripts": { | |
"start": "node server.js" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"mongodb": "^2.2.25" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MongoClient = require('mongodb').MongoClient | |
const DATABASE_URL = process.env.DATABASE_URL || 'mongodb://db:27017/myproject' | |
function connect (url) { | |
return new Promise((resolve, reject) => { | |
MongoClient.connect(url, (err, db) => { | |
if (err) reject(err) | |
else resolve(db) | |
}) | |
}) | |
} | |
function wait (ms, val) { | |
return new Promise(resolve => { | |
setTimeout(() => { | |
resolve(val) | |
}) | |
}, ms) | |
} | |
function retry (coroutine, tries, interval = 500) { | |
return Promise.resolve(coroutine()) | |
.catch(err => { | |
if (tries) return wait(interval, () => retry(coroutine, tries - 1)) | |
else throw err | |
}) | |
} | |
retry(() => connect(DATABASE_URL), 5).then(db => { | |
console.log(`Connected to database [${DATABASE_URL}]`) | |
db.close() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment