Skip to content

Commit 58c6326

Browse files
author
marcos
committed
update(): dockerfile, docker-compose, mongoose
1 parent 36c0582 commit 58c6326

File tree

7 files changed

+88
-3
lines changed

7 files changed

+88
-3
lines changed

.dockerignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
e2e
2+
node_modules
3+
src

Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM node:11-alpine
2+
3+
RUN mkdir -p /usr/src/app
4+
5+
WORKDIR /usr/src/app
6+
7+
COPY . .
8+
9+
RUN npm install
10+
11+
EXPOSE 3600
12+
13+
CMD ["npm", "run", "start"]

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,16 @@ Visit www.toptal.com/blog and subscribe to our newsletter to read great posts
1818
To run the project, please use a command line the following:
1919
- npm start
2020
- It will run the server at port 3600.
21+
22+
23+
### 2019-09-13 update
24+
25+
- Refactored mongoose to a proper common service.
26+
- Added a Dockerfile and docker-compose configuration.
27+
28+
If you are familiar to docker and you have docker installed on your machine and just want to run the project without issues please do:
29+
30+
- docker-compose build
31+
- docker-compose up
32+
- It will run the mongodb at port 27017 (for testing purposes only).
33+
- It will run the server at port 3600.

common/services/mongoose.service.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const mongoose = require('mongoose');
2+
let count = 0;
3+
4+
const options = {
5+
autoIndex: false, // Don't build indexes
6+
reconnectTries: 30, // Retry up to 30 times
7+
reconnectInterval: 500, // Reconnect every 500ms
8+
poolSize: 10, // Maintain up to 10 socket connections
9+
// If not connected, return errors immediately rather than waiting for reconnect
10+
bufferMaxEntries: 0
11+
};
12+
13+
const connectWithRetry = () => {
14+
console.log('MongoDB connection with retry')
15+
mongoose.connect("mongodb://mongo:27017/rest-tutorial", options).then(()=>{
16+
console.log('MongoDB is connected')
17+
}).catch(err=>{
18+
console.log('MongoDB connection unsuccessful, retry after 5 seconds. ', ++count);
19+
setTimeout(connectWithRetry, 5000)
20+
})
21+
};
22+
23+
connectWithRetry();
24+
25+
exports.mongoose = mongoose;

docker-compose.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
version: '3'
2+
services:
3+
api:
4+
image: makinhs/rest-api-tutorial
5+
build: .
6+
networks:
7+
- backend
8+
ports:
9+
- "3600:3600"
10+
depends_on:
11+
- mongo
12+
13+
mongo:
14+
image: mongo
15+
volumes:
16+
- ./data:/data/db
17+
networks:
18+
- backend
19+
ports:
20+
- "27017:27017"
21+
22+
web-cli:
23+
image: makinhs/rest-api-tutorial
24+
links:
25+
- mongo
26+
networks:
27+
- backend
28+
command: sh
29+
30+
networks:
31+
backend:
32+
driver: bridge

users/models/users.model.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
const mongoose = require('mongoose');
2-
mongoose.connect('mongodb://localhost/rest-tutorial');
1+
const mongoose = require('../../common/services/mongoose.service').mongoose;
32
const Schema = mongoose.Schema;
43

54
const userSchema = new Schema({

users/routes.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ exports.routesConfig = function (app) {
3333
PermissionMiddleware.minimumPermissionLevelRequired(ADMIN),
3434
UsersController.removeById
3535
]);
36-
};
36+
};

0 commit comments

Comments
 (0)