From d42c3d45b9e2c3d70e6863797110e1a81b04a899 Mon Sep 17 00:00:00 2001
From: IsolatedSushi <simen.vanherpt@gmail.com>
Date: Sun, 27 Sep 2020 21:39:27 +0200
Subject: [PATCH] Small changes

---
 .../app/index.js                              | 25 ++++++++++++++-----
 .../app/package-lock.json                     |  5 ++++
 .../app/package.json                          |  3 ++-
 .../docker-compose.yml                        |  8 +++---
 .../haproxy/haproxy.cfg                       |  8 +++---
 backend/server.js                             | 20 +++------------
 daprEnvoy.yaml => envoy(dapr).yaml            |  4 +--
 envoy.yaml                                    |  4 +--
 8 files changed, 42 insertions(+), 35 deletions(-)
 rename daprEnvoy.yaml => envoy(dapr).yaml (89%)

diff --git a/backend/microservices and load balancing/app/index.js b/backend/microservices and load balancing/app/index.js
index ae63215..a650e17 100644
--- a/backend/microservices and load balancing/app/index.js	
+++ b/backend/microservices and load balancing/app/index.js	
@@ -1,17 +1,30 @@
 const app = require("express")();
-const appid = process.env.APPID;
+const http = require('http');
 
-app.get("/", (req,res) => 
-res.send({"appid": appid, "point": generatePoint()}))
+const appid = process.env.APPID || -1;
+const port = 8090;
+
+const requestListener =  app.get("/generatePoint", (req,res) => {
+  res.send({"appid": appid, "point": generatePoint()})
+})
 
-var count = 0;
 //Generate randomPoint
 function generatePoint(){
+    console.log
     var xVal = Math.floor(Math.random()*20);
     var yVal = Math.floor(Math.random()*20);
     //console.log("Generated point. ID: " + count + "  x: " + xVal + " y: " + yVal);
     
     return {id:appid, x:xVal,y:yVal};
   }
- 
-app.listen(appid, ()=>console.log(`${appid} is listening on ${appid}`))
+
+
+  if(appid<0){
+    const server = http.createServer(requestListener);
+    server.listen(port);
+    console.log('Listening at port: ' + port);
+  }
+  else{
+    app.listen(appid, ()=>console.log(`${appid} is listening on ${appid}`))
+  }
+  
\ No newline at end of file
diff --git a/backend/microservices and load balancing/app/package-lock.json b/backend/microservices and load balancing/app/package-lock.json
index a3a0c97..b9a8213 100644
--- a/backend/microservices and load balancing/app/package-lock.json	
+++ b/backend/microservices and load balancing/app/package-lock.json	
@@ -162,6 +162,11 @@
       "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz",
       "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac="
     },
+    "http": {
+      "version": "0.0.1-security",
+      "resolved": "https://registry.npmjs.org/http/-/http-0.0.1-security.tgz",
+      "integrity": "sha512-RnDvP10Ty9FxqOtPZuxtebw1j4L/WiqNMDtuc1YMH1XQm5TgDRaR1G9u8upL6KD1bXHSp9eSXo/ED+8Q7FAr+g=="
+    },
     "http-errors": {
       "version": "1.7.2",
       "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.2.tgz",
diff --git a/backend/microservices and load balancing/app/package.json b/backend/microservices and load balancing/app/package.json
index ad8fd85..b0be334 100644
--- a/backend/microservices and load balancing/app/package.json	
+++ b/backend/microservices and load balancing/app/package.json	
@@ -10,6 +10,7 @@
   "author": "",
   "license": "ISC",
   "dependencies": {
-    "express": "^4.17.1"
+    "express": "^4.17.1",
+    "http": "0.0.1-security"
   }
 }
diff --git a/backend/microservices and load balancing/docker-compose.yml b/backend/microservices and load balancing/docker-compose.yml
index 16bbbb7..4d80f55 100644
--- a/backend/microservices and load balancing/docker-compose.yml	
+++ b/backend/microservices and load balancing/docker-compose.yml	
@@ -10,17 +10,17 @@ services:
     nodeapp1:
         image: nodeapp
         environment:
-            - APPID=1111
+            - APPID=1
     nodeapp2:
         image: nodeapp
         environment:
-            - APPID=2222
+            - APPID=2
     nodeapp3:
         image: nodeapp
         environment:
-            - APPID=3333
+            - APPID=3
     nodeapptest:
         image: nodeapp
         environment:
-            - APPID=4444
+            - APPID=4
         
\ No newline at end of file
diff --git a/backend/microservices and load balancing/haproxy/haproxy.cfg b/backend/microservices and load balancing/haproxy/haproxy.cfg
index a13ae5c..c87d797 100644
--- a/backend/microservices and load balancing/haproxy/haproxy.cfg	
+++ b/backend/microservices and load balancing/haproxy/haproxy.cfg	
@@ -6,7 +6,7 @@ frontend http
 
 backend all
     mode http
-    server s1 nodeapp1:1111
-    server s2 nodeapp2:2222
-    server s3 nodeapp3:3333
-    server s4 nodeapptest:4444
+    server s1 nodeapp1:1
+    server s2 nodeapp2:2
+    server s3 nodeapp3:3
+    server s4 nodeapptest:4
diff --git a/backend/server.js b/backend/server.js
index 771e0fd..81a527f 100644
--- a/backend/server.js
+++ b/backend/server.js
@@ -28,23 +28,14 @@ var pointPackage = protoDescriptor.pointPackage;
 
 //SERVICE
 function sendPoint(call, callback) {
-  axios.get('http://localhost:8090').then(resp=>{
+  axios.get('http://localhost:8090/generatePoint').then(resp=>{
     callback(null, resp["data"]["point"]);
   })
 }
 
 
 
-
-
-
-
-
-
-
-/**
- * @param {!Object} call
- */
+//Points stream
 function sendPointStream(call) {
   
   function sender(point) {
@@ -76,11 +67,7 @@ function generatePoint(){
   return {id:count, x:xVal,y:yVal};
 }
 
-
-
-/**
- * @return {!Object} gRPC server
- */
+//Create server and route the grpc functions to js functions
 function getServer() {
   var server = new grpc.Server();
   server.addService(pointPackage.Point.service, {
@@ -90,6 +77,7 @@ function getServer() {
   return server;
 }
 
+//Setup
 if (require.main === module) {
   var server = getServer();
   server.bindAsync(
diff --git a/daprEnvoy.yaml b/envoy(dapr).yaml
similarity index 89%
rename from daprEnvoy.yaml
rename to envoy(dapr).yaml
index b531033..473dd13 100644
--- a/daprEnvoy.yaml
+++ b/envoy(dapr).yaml
@@ -2,7 +2,7 @@ static_resources:
   listeners:
   - name: listener_0
     address:
-      socket_address: { address: 0.0.0.0, port_value: 8080 }
+      socket_address: { address: 0.0.0.0, port_value: 7050 }
     filter_chains:
     - filters:
       - name: envoy.http_connection_manager
@@ -36,4 +36,4 @@ static_resources:
     type: logical_dns
     http2_protocol_options: {}
     lb_policy: round_robin
-    hosts: [{ socket_address: { address: host.docker.internal, port_value: 9090 }}]
\ No newline at end of file
+    hosts: [{ socket_address: { address:  host.docker.internal, port_value: 50460 }}]
\ No newline at end of file
diff --git a/envoy.yaml b/envoy.yaml
index 473dd13..b531033 100644
--- a/envoy.yaml
+++ b/envoy.yaml
@@ -2,7 +2,7 @@ static_resources:
   listeners:
   - name: listener_0
     address:
-      socket_address: { address: 0.0.0.0, port_value: 7050 }
+      socket_address: { address: 0.0.0.0, port_value: 8080 }
     filter_chains:
     - filters:
       - name: envoy.http_connection_manager
@@ -36,4 +36,4 @@ static_resources:
     type: logical_dns
     http2_protocol_options: {}
     lb_policy: round_robin
-    hosts: [{ socket_address: { address:  host.docker.internal, port_value: 50460 }}]
\ No newline at end of file
+    hosts: [{ socket_address: { address: host.docker.internal, port_value: 9090 }}]
\ No newline at end of file
-- 
GitLab