From dd9e6274810a0998cb73831100d98484b2df3551 Mon Sep 17 00:00:00 2001
From: Yehor Potebenko <hitechnic.uu68@gmail.com>
Date: Tue, 16 May 2023 23:12:26 +0200
Subject: [PATCH] refactor: replace magic numbers with the environment
 variables

---
 .env                    |  4 +++-
 docker-compose.yml      | 27 ++++++++-------------------
 loadbalancer/.env       |  3 +++
 loadbalancer/Dockerfile |  4 +++-
 loadbalancer/nginx.conf |  7 +++++--
 server/Dockerfile       |  2 +-
 server/index.js         |  7 ++++++-
 7 files changed, 29 insertions(+), 25 deletions(-)
 create mode 100644 loadbalancer/.env

diff --git a/.env b/.env
index 8c2600d..7706e1c 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 5d6f58f..ba0d03c 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 0000000..4c78ea5
--- /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 c54f476..71a5bc3 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 f18eea7..0c484e4 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 c24a7e1..144429f 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 46acaee..34a0751 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;
-- 
GitLab