diff --git a/.env b/.env index 8c2600d2b2134c131948b78ec86bf9182cf4873d..7706e1cc80342cfe24f77d9113797ae933b682f3 100644 --- a/.env +++ b/.env @@ -1,2 +1,4 @@ -BACKEND_PORT=3000 +BACKEND_3000=3000 +BACKEND_3001=3001 +BACKEND_3002=3002 FRONTEND_PORT=8080 \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 5d6f58f8965709415fc7821d0b2b5683569171bc..ba0d03c05a6d487705a133f9f817b0aa76c77752 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,25 +17,11 @@ services: networks: - letsDo-network - backend1: - build: - context: server - dockerfile: Dockerfile - env_file: - - .env - restart: always - depends_on: - - database - environment: - - DB_USER - - DB_PASSWORD - networks: - - letsDo-network - - backend2: + backend: build: context: server dockerfile: Dockerfile + tty: true env_file: - .env restart: always @@ -49,17 +35,20 @@ services: database: image: mongo + tty: true container_name: mongo-db loadbalancer: build: context: loadbalancer dockerfile: Dockerfile + tty: true links: - - backend1 - - backend2 + - backend ports: - - ${BACKEND_PORT}:${BACKEND_PORT} + - '${BACKEND_3000}:${BACKEND_3000}' + - '${BACKEND_3001}:${BACKEND_3001}' + - '${BACKEND_3002}:${BACKEND_3002}' networks: - letsDo-network diff --git a/loadbalancer/.env b/loadbalancer/.env new file mode 100644 index 0000000000000000000000000000000000000000..4c78ea5b127c688d10e4d7adfc09a2355ace6142 --- /dev/null +++ b/loadbalancer/.env @@ -0,0 +1,3 @@ +BACKEND_3000=3000 +BACKEND_3001=3001 +BACKEND_3002=3002 \ No newline at end of file diff --git a/loadbalancer/Dockerfile b/loadbalancer/Dockerfile index c54f47624007d52c678267dc48737ae28b9e5664..71a5bc3a1c9438d93a899a8a7773d72e35948026 100644 --- a/loadbalancer/Dockerfile +++ b/loadbalancer/Dockerfile @@ -2,6 +2,8 @@ FROM nginx COPY nginx.conf /etc/nginx/nginx.conf -EXPOSE 3000 +EXPOSE ${BACKEND_3000} +EXPOSE ${BACKEND_3001} +EXPOSE ${BACKEND_3002} CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/loadbalancer/nginx.conf b/loadbalancer/nginx.conf index f18eea7a0acf4c733fb444651141f92a46c27881..0c484e4b249b616194b4c541e4a9958b099924c5 100644 --- a/loadbalancer/nginx.conf +++ b/loadbalancer/nginx.conf @@ -1,11 +1,14 @@ http { upstream backend { - server backend1:3000; - server backend2:3001; + server backend:3000; + server backend:3001; + server backend:3002; } server { listen 3000; + listen 3001; + listen 3002; location / { proxy_pass http://backend/; diff --git a/server/Dockerfile b/server/Dockerfile index c24a7e106a00a93b789a3426b54bb633a316d968..144429fe31ba7789aa42fdee1b47b8829a42f1d0 100644 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -8,6 +8,6 @@ RUN npm install COPY . . -EXPOSE ${BACKEND_PORT} +EXPOSE ${BACKEND_3000} CMD [ "npm", "start" ] \ No newline at end of file diff --git a/server/index.js b/server/index.js index 46acaeed78d1ce2c129c658604ecc2311ffc064b..34a0751c1f2b2fccca7f598ff252f5cc7efaac89 100644 --- a/server/index.js +++ b/server/index.js @@ -3,6 +3,7 @@ import express from 'express'; import mongoose from 'mongoose'; import dotenv from 'dotenv'; import cors from 'cors'; +import os from 'os'; import authRoute from './routes/auth.js'; import todoRoute from './routes/todos.js'; @@ -21,6 +22,10 @@ app.use(express.json()); app.use('/api/auth', authRoute); app.use('/api/todos', todoRoute); +app.use('/', async (req, res) => { + res.json({ message: 'Everything works fine!', hostname: req.hostname }); +}); + async function start() { try { await mongoose.connect( @@ -28,7 +33,7 @@ async function start() { ); app.listen(BACKEND_PORT, () => { - console.log(`Server started on port: ${BACKEND_PORT}`); + console.log(`Running container: ${os.hostname()}`); }); return null;