From fd848c2419d0746b12849164f0d2ae388ae05849 Mon Sep 17 00:00:00 2001
From: Mamadu Lamarana Bah <mamadulamarana.bah.etu@univ-lille.fr>
Date: Tue, 13 Feb 2024 23:44:52 +0100
Subject: [PATCH] mise en place socket io et multi-entry(bundle)

---
 .../server/controllers/HtmlResponseBuilder.js | 12 ------
 .../server/controllers/RequestController.js   |  4 +-
 .../server/controllers/ResponseBuilder.js     | 37 +++++++++++++------
 encheres/server/controllers/about.js          | 24 ------------
 encheres/server/controllers/auctioneer.js     | 22 -----------
 encheres/server/controllers/bidder.js         | 22 -----------
 encheres/server/controllers/default.js        | 29 ---------------
 encheres/server/controllers/ioController.js   |  8 ++--
 encheres/server/main.js                       |  5 +++
 encheres/server/public/html/about.html        | 12 ++++++
 encheres/server/public/html/auctioneer.html   | 15 ++++++++
 encheres/server/public/html/bidder.html       | 15 ++++++++
 encheres/server/public/index.html             | 23 +++++++++++-
 .../public/scripts/auctioneer-bundle.js       |  2 +
 .../scripts/auctioneer-bundle.js.LICENSE.txt  |  3 ++
 .../server/public/scripts/bidder-bundle.js    |  2 +
 .../scripts/bidder-bundle.js.LICENSE.txt      |  3 ++
 encheres/server/public/scripts/bundle.js      |  2 -
 encheres/server/public/scripts/main-bundle.js |  2 +
 ...LICENSE.txt => main-bundle.js.LICENSE.txt} |  0
 20 files changed, 111 insertions(+), 131 deletions(-)
 delete mode 100644 encheres/server/controllers/HtmlResponseBuilder.js
 delete mode 100644 encheres/server/controllers/about.js
 delete mode 100644 encheres/server/controllers/auctioneer.js
 delete mode 100644 encheres/server/controllers/bidder.js
 delete mode 100644 encheres/server/controllers/default.js
 create mode 100644 encheres/server/public/html/about.html
 create mode 100644 encheres/server/public/html/auctioneer.html
 create mode 100644 encheres/server/public/html/bidder.html
 create mode 100644 encheres/server/public/scripts/auctioneer-bundle.js
 create mode 100644 encheres/server/public/scripts/auctioneer-bundle.js.LICENSE.txt
 create mode 100644 encheres/server/public/scripts/bidder-bundle.js
 create mode 100644 encheres/server/public/scripts/bidder-bundle.js.LICENSE.txt
 delete mode 100644 encheres/server/public/scripts/bundle.js
 create mode 100644 encheres/server/public/scripts/main-bundle.js
 rename encheres/server/public/scripts/{bundle.js.LICENSE.txt => main-bundle.js.LICENSE.txt} (100%)

diff --git a/encheres/server/controllers/HtmlResponseBuilder.js b/encheres/server/controllers/HtmlResponseBuilder.js
deleted file mode 100644
index a51f8aa..0000000
--- a/encheres/server/controllers/HtmlResponseBuilder.js
+++ /dev/null
@@ -1,12 +0,0 @@
-
-export default class HtmlResponseBuilder {
-
-    response;
-    url;
-    
-    constructor(url, response) {
-        this.url = url;
-        this.response =response;
-    }
-     
-} 
\ No newline at end of file
diff --git a/encheres/server/controllers/RequestController.js b/encheres/server/controllers/RequestController.js
index 23f0f85..a8fbfae 100644
--- a/encheres/server/controllers/RequestController.js
+++ b/encheres/server/controllers/RequestController.js
@@ -32,8 +32,8 @@ export default class RequestController {
     this.response.end();
   }
 
-  buildResponse()  {
-    new ResponseBuilder(this.url, this.#response).handleResponse();
+  async buildResponse()  {
+    await new ResponseBuilder(this.url, this.#response).handleResponse();
   }
 
 }
\ No newline at end of file
diff --git a/encheres/server/controllers/ResponseBuilder.js b/encheres/server/controllers/ResponseBuilder.js
index c0b1097..67df9f5 100644
--- a/encheres/server/controllers/ResponseBuilder.js
+++ b/encheres/server/controllers/ResponseBuilder.js
@@ -1,8 +1,5 @@
-import defaultBuilder from './default.js';
-import aboutBuilder from './about.js';
-import auctioneerBuilder from './auctioneer.js'
+import * as fs from 'fs/promises';
 import ErrorResponseBuilder from './ErrorResponseBuilder.js';
-import bidderBuilder from './bidder.js';
 import { URL } from 'url';
 
 export default class ResponseBuilder {
@@ -27,28 +24,44 @@ export default class ResponseBuilder {
       return this.#request;
     }
 
-      handleResponse() {
-        this.buildResponse();
+    async handleResponse() {
+        await this.buildResponse();
       }
       
-      buildResponse()  {    
+    async buildResponse()  {    
         
         // routage "à la main"
         if (this.url === '/' ){
-          new defaultBuilder(this.url, this.response).buildResponse();
+          await this.readResponse('./public/index.html');
         }
         else if (this.url.startsWith('/about')){
-          new aboutBuilder(this.url, this.response).buildResponse();
+          await this.readResponse('./public/html/about.html');
         }
         else if(this.url.startsWith('/auctioneer')) {
-          new auctioneerBuilder(this.url, this.response).buildResponse();
+          await this.readResponse('./public/html/auctioneer.html');
         }
         else if(this.url.startsWith('/bidder')) {
-          new bidderBuilder(this.url, this.response).buildResponse();
+          await this.readResponse('./public/html/bidder.html');
         }
         else {
-          new ErrorResponseBuilder(this.url, this.response, 404).handleError(); 
+          await this.readResponse(`./public${this.#url}`);
         }
       }      
     
+      async readResponse(file)  {
+        try {
+          // check if resource is available
+          await fs.access(file);
+          // read the requested resource content
+          const data = await fs.readFile(file);
+          // send resource content
+          this.response.statusCode = 200;
+          this.response.write(data);
+        }
+        catch(err) { // resource is not available
+          new ErrorResponseBuilder(this.url, this.response, 404).handleError(); 
+        }
+      }
+    
+
 }
\ No newline at end of file
diff --git a/encheres/server/controllers/about.js b/encheres/server/controllers/about.js
deleted file mode 100644
index fd890ab..0000000
--- a/encheres/server/controllers/about.js
+++ /dev/null
@@ -1,24 +0,0 @@
-import HtmlResponseBuilder from "./HtmlResponseBuilder.js";
-import * as fs from 'fs';
-
-
-export default class about extends HtmlResponseBuilder{
-    
-    constructor(url, response){
-        super(url, response);
-    } 
-
-    buildResponse()  {
-        let path = `..${this.url}.html`;
-        try {
-            fs.accessSync(path, fs.constants.R_OK);
-            const content = fs.readFileSync(path);
-            this.response.write(content);
-            this.response.end();
-        }catch(err) {
-            new ErrorResponseBuilder(this.url, this.response, 404).handleError();
-        }
-    
-      }
-    
-} 
\ No newline at end of file
diff --git a/encheres/server/controllers/auctioneer.js b/encheres/server/controllers/auctioneer.js
deleted file mode 100644
index 2f52a85..0000000
--- a/encheres/server/controllers/auctioneer.js
+++ /dev/null
@@ -1,22 +0,0 @@
-import HtmlResponseBuilder from "./HtmlResponseBuilder.js";
-import * as fs from 'fs';
-
-export default class auctioneer extends HtmlResponseBuilder{
-    
-    constructor(url, response){
-        super(url, response);
-    } 
-
-    buildResponse()  {
-        let path = `..${this.url}.html`;
-        try {
-            fs.accessSync(path, fs.constants.R_OK);
-            const content = fs.readFileSync(path);
-            this.response.write(content);
-        }catch(err) {
-            new ErrorResponseBuilder(this.url, this.response, 404).handleError();
-        }
-    
-      }
-
-} 
\ No newline at end of file
diff --git a/encheres/server/controllers/bidder.js b/encheres/server/controllers/bidder.js
deleted file mode 100644
index e0f1dbb..0000000
--- a/encheres/server/controllers/bidder.js
+++ /dev/null
@@ -1,22 +0,0 @@
-import HtmlResponseBuilder from "./HtmlResponseBuilder.js";
-import * as fs from 'fs';
-
-export default class bidder extends HtmlResponseBuilder{
-    
-    constructor(url, response){
-        super(url, response);
-    } 
-
-    buildResponse()  {
-        let path = `..${this.url}.html`;
-        try {
-            fs.accessSync(path, fs.constants.R_OK);
-            const content = fs.readFileSync(path);
-            this.response.write(content);
-        }catch(err) {
-            new ErrorResponseBuilder(this.url, this.response, 404).handleError();
-        }
-    
-      }
-
-} 
\ No newline at end of file
diff --git a/encheres/server/controllers/default.js b/encheres/server/controllers/default.js
deleted file mode 100644
index 5912633..0000000
--- a/encheres/server/controllers/default.js
+++ /dev/null
@@ -1,29 +0,0 @@
-import HtmlResponseBuilder from "./HtmlResponseBuilder.js";
-import * as fs from 'fs';
-
-import ErrorResponseBuilder from "./ErrorResponseBuilder.js";
-
-
-export default class defaultBuilder extends HtmlResponseBuilder{
-    
-    constructor(url, response){
-        super(url, response);
-    }
-
-  /**
-  * send the requested resource as it is, if it exists, else responds with a 404
-  */
-  buildResponse()  {
-    const path = `..${this.url}index.html`;
-    try {
-        console.log(path);
-        fs.accessSync(path, fs.constants.R_OK);
-        const content = fs.readFileSync(path);
-        this.response.write(content);
-    }catch(err) {
-        new ErrorResponseBuilder(this.url, this.response, 404).handleError();
-    }
-
-  }
-
-} 
\ No newline at end of file
diff --git a/encheres/server/controllers/ioController.js b/encheres/server/controllers/ioController.js
index 9ec5381..75de8ee 100644
--- a/encheres/server/controllers/ioController.js
+++ b/encheres/server/controllers/ioController.js
@@ -6,7 +6,7 @@ export default class IOController {
   
     constructor(io) {
       this.#io = io;
-      this.#clients = new Map();
+      //this.#clients = new Map();
     }
   
     registerSocket(socket) {
@@ -17,14 +17,12 @@ export default class IOController {
   
     setupListeners(socket) {
       socket.on( 'pong', () => this.greatings(socket) );
-      socket.on( 'disconnect' , () => this.leave(socket) );
+      //socket.on( 'disconnect' , () => this.leave(socket) );
     }
   
     greatings(socket) {
       console.log(`pong received from (id : ${socket.id}) at ${new Date().toLocaleTimeString()}`);
-      //const timerId = setInterval(this.randomNumber.bind(this), this.#interval);
-      //const timerId = setInterval(this.randomNumber.bind(this), this.#interval, socket); /**si on veut que le nombre random soit différent */
-      this.#clients.set(socket.id, timerId);
+      //this.#clients.set(socket.id);
     }
   
     leave(socket) {
diff --git a/encheres/server/main.js b/encheres/server/main.js
index 9a1144e..538ffe7 100644
--- a/encheres/server/main.js
+++ b/encheres/server/main.js
@@ -8,6 +8,11 @@ const server = http.createServer(
 	(request, response) => new RequestController(request, response).handleRequest()
 );
 
+const io = new ServerIO(server);
+
+// mise en place du serveur de socket.io
+const ioController = new IOController(io);
+io.on('connection', ioController.registerSocket.bind(ioController) );
 
 
 server.listen(8080);
\ No newline at end of file
diff --git a/encheres/server/public/html/about.html b/encheres/server/public/html/about.html
new file mode 100644
index 0000000..1199e9d
--- /dev/null
+++ b/encheres/server/public/html/about.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+
+<html>
+    <head>
+        <meta charset="UTF-8">
+        <title>Server</title>
+        <link href="./style/style.css" rel="stylesheet" type="text/css"/>
+    </head>
+                                
+    <body>
+        <h1>Infos</h1>
+    </body>
\ No newline at end of file
diff --git a/encheres/server/public/html/auctioneer.html b/encheres/server/public/html/auctioneer.html
new file mode 100644
index 0000000..74b7c47
--- /dev/null
+++ b/encheres/server/public/html/auctioneer.html
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+
+<html>
+    <head>
+        <meta charset="UTF-8">
+        <title>Server</title>
+        <link href="./style/style.css" rel="stylesheet" type="text/css"/>
+        <script src="/socket.io/socket.io.js"></script>
+    <script defer src="../scripts/auctioneer-bundle.js?8646d609246098631a96"></script></head>
+                                
+    <body>
+        <h1>Comissaire-priseur</h1>
+    </body>
+
+</html>
\ No newline at end of file
diff --git a/encheres/server/public/html/bidder.html b/encheres/server/public/html/bidder.html
new file mode 100644
index 0000000..f053e67
--- /dev/null
+++ b/encheres/server/public/html/bidder.html
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+
+<html>
+    <head>
+        <meta charset="UTF-8">
+        <title>Server</title>
+        <link href="./style/style.css" rel="stylesheet" type="text/css"/>
+        <script src="/socket.io/socket.io.js"></script>
+    <script defer src="../scripts/bidder-bundle.js?8646d609246098631a96"></script></head>
+                                
+    <body>
+        <h1>Enchérisseur</h1>
+    </body>
+
+</html>
\ No newline at end of file
diff --git a/encheres/server/public/index.html b/encheres/server/public/index.html
index 121c593..f1f72bb 100644
--- a/encheres/server/public/index.html
+++ b/encheres/server/public/index.html
@@ -1 +1,22 @@
-<head><script defer src="scripts/bundle.js?50d73e68ded995014797"></script></head>
\ No newline at end of file
+<!DOCTYPE html>
+
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
+
+    <head>
+	    <meta charset="UTF-8"/>
+	    <title>server</title>
+        <link href="./style/style.css" rel="stylesheet" type="text/css"/>
+
+    <script defer src="scripts/main-bundle.js?8646d609246098631a96"></script></head>
+
+    <body>
+        <h1>Enchères</h1>
+        <p class="ok"></p>
+            <ul id="liens">
+                <li> <a href="./auctioneer">Etre Comissaire-priseur</a> </li>
+                <li> <a href="./bidder">Participer aux Enchères</a> </li>
+                <li> <a href="./about">Infos sur l'application</a> </li>
+            </ul>
+    </body>
+
+</html>
\ No newline at end of file
diff --git a/encheres/server/public/scripts/auctioneer-bundle.js b/encheres/server/public/scripts/auctioneer-bundle.js
new file mode 100644
index 0000000..f857f6d
--- /dev/null
+++ b/encheres/server/public/scripts/auctioneer-bundle.js
@@ -0,0 +1,2 @@
+/*! For license information please see auctioneer-bundle.js.LICENSE.txt */
+(()=>{var __webpack_modules__={"./src/scripts/auctioneer.js":()=>{eval("console.log('le bundle-auctioneer a été généré');\nconst socket = io();//# sourceURL=[module]\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiLi9zcmMvc2NyaXB0cy9hdWN0aW9uZWVyLmpzIiwibmFtZXMiOlsiY29uc29sZSIsImxvZyIsInNvY2tldCIsImlvIl0sInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly9hcHAvLi9zcmMvc2NyaXB0cy9hdWN0aW9uZWVyLmpzP2U2NjIiXSwic291cmNlc0NvbnRlbnQiOlsiY29uc29sZS5sb2coJ2xlIGJ1bmRsZS1hdWN0aW9uZWVyIGEgw6l0w6kgZ8OpbsOpcsOpJyk7XHJcblxyXG5jb25zdCBzb2NrZXQgPSBpbygpO1xyXG4iXSwibWFwcGluZ3MiOiJBQUFBQSxPQUFPLENBQUNDLEdBQUcsQ0FBQyxtQ0FBbUMsQ0FBQztBQUVoRCxNQUFNQyxNQUFNLEdBQUdDLEVBQUUsQ0FBQyxDQUFDIn0=\n//# sourceURL=webpack-internal:///./src/scripts/auctioneer.js\n")}},__webpack_exports__={};__webpack_modules__["./src/scripts/auctioneer.js"]()})();
\ No newline at end of file
diff --git a/encheres/server/public/scripts/auctioneer-bundle.js.LICENSE.txt b/encheres/server/public/scripts/auctioneer-bundle.js.LICENSE.txt
new file mode 100644
index 0000000..fb879f6
--- /dev/null
+++ b/encheres/server/public/scripts/auctioneer-bundle.js.LICENSE.txt
@@ -0,0 +1,3 @@
+/*!***********************************!*\
+  !*** ./src/scripts/auctioneer.js ***!
+  \***********************************/
diff --git a/encheres/server/public/scripts/bidder-bundle.js b/encheres/server/public/scripts/bidder-bundle.js
new file mode 100644
index 0000000..36081fb
--- /dev/null
+++ b/encheres/server/public/scripts/bidder-bundle.js
@@ -0,0 +1,2 @@
+/*! For license information please see bidder-bundle.js.LICENSE.txt */
+(()=>{var __webpack_modules__={"./src/scripts/bidder.js":()=>{eval("console.log('le bundle-bidder a été généré');\nconst socket = io();//# sourceURL=[module]\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiLi9zcmMvc2NyaXB0cy9iaWRkZXIuanMiLCJuYW1lcyI6WyJjb25zb2xlIiwibG9nIiwic29ja2V0IiwiaW8iXSwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsid2VicGFjazovL2FwcC8uL3NyYy9zY3JpcHRzL2JpZGRlci5qcz9hNjJhIl0sInNvdXJjZXNDb250ZW50IjpbImNvbnNvbGUubG9nKCdsZSBidW5kbGUtYmlkZGVyIGEgw6l0w6kgZ8OpbsOpcsOpJyk7XHJcblxyXG5jb25zdCBzb2NrZXQgPSBpbygpOyJdLCJtYXBwaW5ncyI6IkFBQUFBLE9BQU8sQ0FBQ0MsR0FBRyxDQUFDLCtCQUErQixDQUFDO0FBRTVDLE1BQU1DLE1BQU0sR0FBR0MsRUFBRSxDQUFDLENBQUMifQ==\n//# sourceURL=webpack-internal:///./src/scripts/bidder.js\n")}},__webpack_exports__={};__webpack_modules__["./src/scripts/bidder.js"]()})();
\ No newline at end of file
diff --git a/encheres/server/public/scripts/bidder-bundle.js.LICENSE.txt b/encheres/server/public/scripts/bidder-bundle.js.LICENSE.txt
new file mode 100644
index 0000000..84cd2c5
--- /dev/null
+++ b/encheres/server/public/scripts/bidder-bundle.js.LICENSE.txt
@@ -0,0 +1,3 @@
+/*!*******************************!*\
+  !*** ./src/scripts/bidder.js ***!
+  \*******************************/
diff --git a/encheres/server/public/scripts/bundle.js b/encheres/server/public/scripts/bundle.js
deleted file mode 100644
index b11f4fd..0000000
--- a/encheres/server/public/scripts/bundle.js
+++ /dev/null
@@ -1,2 +0,0 @@
-/*! For license information please see bundle.js.LICENSE.txt */
-(()=>{var __webpack_modules__={"./src/scripts/main.js":()=>{eval("// write your code here\nconsole.log('le bundle a été généré');\nconst socket = io();//# sourceURL=[module]\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiLi9zcmMvc2NyaXB0cy9tYWluLmpzIiwibmFtZXMiOlsiY29uc29sZSIsImxvZyIsInNvY2tldCIsImlvIl0sInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly9hcHAvLi9zcmMvc2NyaXB0cy9tYWluLmpzPzI5NjMiXSwic291cmNlc0NvbnRlbnQiOlsiLy8gd3JpdGUgeW91ciBjb2RlIGhlcmVcbmNvbnNvbGUubG9nKCdsZSBidW5kbGUgYSDDqXTDqSBnw6luw6lyw6knKTtcblxuY29uc3Qgc29ja2V0ID0gaW8oKTtcblxuXG4iXSwibWFwcGluZ3MiOiJBQUFBO0FBQ0FBLE9BQU8sQ0FBQ0MsR0FBRyxDQUFDLHdCQUF3QixDQUFDO0FBRXJDLE1BQU1DLE1BQU0sR0FBR0MsRUFBRSxDQUFDLENBQUMifQ==\n//# sourceURL=webpack-internal:///./src/scripts/main.js\n")}},__webpack_exports__={};__webpack_modules__["./src/scripts/main.js"]()})();
\ No newline at end of file
diff --git a/encheres/server/public/scripts/main-bundle.js b/encheres/server/public/scripts/main-bundle.js
new file mode 100644
index 0000000..983ac6d
--- /dev/null
+++ b/encheres/server/public/scripts/main-bundle.js
@@ -0,0 +1,2 @@
+/*! For license information please see main-bundle.js.LICENSE.txt */
+(()=>{var __webpack_modules__={"./src/scripts/main.js":()=>{eval("// write your code here\nconsole.log('le bundle a été généré');//# sourceURL=[module]\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiLi9zcmMvc2NyaXB0cy9tYWluLmpzIiwibmFtZXMiOlsiY29uc29sZSIsImxvZyJdLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJ3ZWJwYWNrOi8vYXBwLy4vc3JjL3NjcmlwdHMvbWFpbi5qcz8yOTYzIl0sInNvdXJjZXNDb250ZW50IjpbIi8vIHdyaXRlIHlvdXIgY29kZSBoZXJlXG5jb25zb2xlLmxvZygnbGUgYnVuZGxlIGEgw6l0w6kgZ8OpbsOpcsOpJyk7XG5cblxuICAiXSwibWFwcGluZ3MiOiJBQUFBO0FBQ0FBLE9BQU8sQ0FBQ0MsR0FBRyxDQUFDLHdCQUF3QixDQUFDIn0=\n//# sourceURL=webpack-internal:///./src/scripts/main.js\n")}},__webpack_exports__={};__webpack_modules__["./src/scripts/main.js"]()})();
\ No newline at end of file
diff --git a/encheres/server/public/scripts/bundle.js.LICENSE.txt b/encheres/server/public/scripts/main-bundle.js.LICENSE.txt
similarity index 100%
rename from encheres/server/public/scripts/bundle.js.LICENSE.txt
rename to encheres/server/public/scripts/main-bundle.js.LICENSE.txt
-- 
GitLab