From ab1b96d9d104a92e802937b9d206c7d8821d621e Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Sat, 4 Sep 2021 17:03:43 +0200 Subject: [PATCH] Foundations Project: Client Code Mantainance @ HTTP Interface --- Client/client.js | 2 - Client/httpInterface.js | 71 ++++--------------- Client/webSocketsInterface.js | 11 +-- .../Foundations/SA/Utilities/HttpResponses.js | 42 ++++++++++- 4 files changed, 57 insertions(+), 69 deletions(-) diff --git a/Client/client.js b/Client/client.js index 5adf10a82..c5ab5b73a 100644 --- a/Client/client.js +++ b/Client/client.js @@ -165,12 +165,10 @@ try { WEB_SOCKETS_INTERFACE = WEB_SOCKETS_INTERFACE.newWebSocketsInterface() WEB_SOCKETS_INTERFACE.initialize() - WEB_SOCKETS_INTERFACE.run() console.log('Web Sockets Interface ....................................... Listening at port ' + global.env.CLIENT_WEB_SOCKETS_INTERFACE_PORT) HTTP_INTERFACE = HTTP_INTERFACE.newHttpInterface() HTTP_INTERFACE.initialize() - HTTP_INTERFACE.run() console.log('Http Interface .............................................. Listening at port ' + global.env.CLIENT_HTTP_INTERFACE_PORT) console.log('') diff --git a/Client/httpInterface.js b/Client/httpInterface.js index 89b98454e..4cb4b65f3 100644 --- a/Client/httpInterface.js +++ b/Client/httpInterface.js @@ -9,8 +9,7 @@ exports.newHttpInterface = function newHttpInterface() { */ let thisObject = { initialize: initialize, - finalize: finalize, - run: run + finalize: finalize } let webhook = new Map() @@ -18,32 +17,29 @@ exports.newHttpInterface = function newHttpInterface() { return thisObject function finalize() { - + webhook = undefined } function initialize() { - - } - - function run() { /* We will create an HTTP Server and leave it running forever. */ - SA.nodeModules.http.createServer(onHttpRequest).listen(global.env.CLIENT_HTTP_INTERFACE_PORT) - /* Starting the browser now is optional */ - if (process.argv.includes("noBrowser")) { - //Running Client only with no UI. - } else { - SA.nodeModules.open('http://localhost:' + global.env.CLIENT_HTTP_INTERFACE_PORT) - } + SA.nodeModules.http.createServer(onHttpRequest).listen(global.env.CLIENT_HTTP_INTERFACE_PORT) + /* Starting the browser now is optional */ + if (process.argv.includes("noBrowser")) { + //Running Client only with no UI. + } else { + SA.nodeModules.open('http://localhost:' + global.env.CLIENT_HTTP_INTERFACE_PORT) + } } function onHttpRequest(httpRequest, httpResponse) { try { let requestPathAndParameters = httpRequest.url.split('?') // Remove version information let requestPath = requestPathAndParameters[0].split('/') + let endpointOrFile = requestPath[1] - switch (requestPath[1]) { + switch (endpointOrFile) { case 'WEB3': { @@ -1194,7 +1190,7 @@ exports.newHttpInterface = function newHttpInterface() { break case 'ChartLayers': { - SA.projects.foundations.utilities.httpResponses.respondWithFile(global.env.PATH_TO_UI + '/' + requestPath[1] + '/' + requestPath[2], httpResponse) + SA.projects.foundations.utilities.httpResponses.respondWithFile(global.env.PATH_TO_UI + '/' + endpointOrFile + '/' + requestPath[2], httpResponse) } break case 'Files': @@ -1700,50 +1696,9 @@ exports.newHttpInterface = function newHttpInterface() { break default: { - noEndpointReceived() + SA.projects.foundations.utilities.httpResponses.respondWithWebFile(httpResponse, endpointOrFile, global.env.PATH_TO_CLIENT) } } - - function noEndpointReceived() { - - if (requestPath[1] === '') { - /* - When there is no endpoint specidied we will respond with this app's Home Page. - */ - let fs = SA.nodeModules.fs - - let fileName = global.env.PATH_TO_CLIENT + 'WebServer/index.html' - fs.readFile(fileName, onFileRead) - - function onFileRead(err, file) { - - let fileContent = file.toString() - - SA.projects.foundations.utilities.httpResponses.respondWithContent(fileContent, httpResponse) - } - } else { - /* - When there is a parameter but it does not match any of the available endpoints, we - will serve the file with the same name at at a location that depends on it's file extention. - */ - let completeFileName = requestPath[1] - let fileExtension = completeFileName.split('.')[1] - let path - - switch (fileExtension) { - case 'js': { - path = global.env.PATH_TO_UI + '/' + completeFileName - SA.projects.foundations.utilities.httpResponses.respondWithFile(path, httpResponse) - break - } - case 'css': { - SA.projects.foundations.utilities.httpResponses.respondWithStyleSheet(httpResponse, global.env.PATH_TO_CLIENT, completeFileName) - break - } - } - } - } - } catch (err) { console.log(err.stack) } diff --git a/Client/webSocketsInterface.js b/Client/webSocketsInterface.js index 36dfd2aef..79bb3a216 100644 --- a/Client/webSocketsInterface.js +++ b/Client/webSocketsInterface.js @@ -4,8 +4,7 @@ let thisObject = { initialize: initialize, - finalize: finalize, - run: run + finalize: finalize } const WEB_SOCKET = require('ws') @@ -15,15 +14,11 @@ return thisObject function finalize() { - + socketServer = undefined } function initialize() { - - } - - function run() { - setUpWebSocketServer() + setUpWebSocketServer() } function setUpWebSocketServer() { diff --git a/Projects/Foundations/SA/Utilities/HttpResponses.js b/Projects/Foundations/SA/Utilities/HttpResponses.js index 96cb2a44d..1b0485964 100644 --- a/Projects/Foundations/SA/Utilities/HttpResponses.js +++ b/Projects/Foundations/SA/Utilities/HttpResponses.js @@ -7,7 +7,8 @@ exports.newFoundationsUtilitiesHttpResponses = function () { respondWithFont: respondWithFont, respondWithEmptyArray: respondWithEmptyArray, respondWithStyleSheet: respondWithStyleSheet, - respondWithProjectFolderFileList: respondWithProjectFolderFileList + respondWithProjectFolderFileList: respondWithProjectFolderFileList, + respondWithWebFile: respondWithWebFile } return thisObject @@ -171,4 +172,43 @@ exports.newFoundationsUtilitiesHttpResponses = function () { } } } + + function respondWithWebFile(httpResponse, fileNameReceived, path) { + + if (fileNameReceived === '') { + /* + When there is no endpoint specidied we will respond with this app's Home Page. + */ + let fs = SA.nodeModules.fs + + let fileName = path + 'WebServer/index.html' + fs.readFile(fileName, onFileRead) + + function onFileRead(err, file) { + + let fileContent = file.toString() + + SA.projects.foundations.utilities.httpResponses.respondWithContent(fileContent, httpResponse) + } + } else { + /* + When there is a parameter but it does not match any of the available endpoints, we + will serve the file with the same name at at a location that depends on it's file extention. + */ + let completeFileName = fileNameReceived + let fileExtension = completeFileName.split('.')[1] + + switch (fileExtension) { + case 'js': { + let path = global.env.PATH_TO_UI + '/' + completeFileName + SA.projects.foundations.utilities.httpResponses.respondWithFile(path, httpResponse) + break + } + case 'css': { + SA.projects.foundations.utilities.httpResponses.respondWithStyleSheet(httpResponse, path, completeFileName) + break + } + } + } + } } \ No newline at end of file