diff --git a/Ecosystem.js b/Ecosystem.js index d15fbc62b..961085e1f 100644 --- a/Ecosystem.js +++ b/Ecosystem.js @@ -17,13 +17,12 @@ function newEcosystem() { initialize: initialize } - let ECOSYSTEM = "@ecosystem.json@"; + let ECOSYSTEM = JSON.parse(window.localStorage.getItem('ecosystem')); return thisObject; function initialize() { - } function setEvent(input) { diff --git a/Plotter.js b/Plotter.js index b1d992d56..4d1851a81 100644 --- a/Plotter.js +++ b/Plotter.js @@ -1,23 +1,6 @@ - - -function getNewPlotter(pDevTeamOrHost, pPlotter, pModule) { - - let plotter; - let fullCode = pDevTeamOrHost + pPlotter + pModule; - fullCode = fullCode.replace(/-/g, ""); - - switch (fullCode) { - -// Cases - - default: - { - throw("getNewPlotter: " + fullCode + " not found.") - } - } - - return plotter; +function getNewPlotter(pDevTeamOrHost, pPlotter, pModule) { + let functionName = pDevTeamOrHost + pPlotter + pModule; + functionName = functionName.replace(/-/g, ""); + functionName = 'new' + functionName + return window[functionName](); } - - - \ No newline at end of file diff --git a/PlotterPanel.js b/PlotterPanel.js index 78c9fb92a..d12ab6188 100644 --- a/PlotterPanel.js +++ b/PlotterPanel.js @@ -1,19 +1,6 @@ - -function getNewPlotterPanel(pDevTeamOrHost, pPlotter, pModule, pPanel) { - - let plotterPanel; - let fullCode = pDevTeamOrHost + pPlotter + pModule + pPanel; - fullCode = fullCode.replace(/-/g, ""); - - switch (fullCode) { - -// Cases - - default: - { - throw ("getNewPlotterPanel: " + fullCode + " not found.") - } - } - - return plotterPanel; +function getNewPlotterPanel(pDevTeamOrHost, pPlotter, pModule, pPanel) { + let functionName = pDevTeamOrHost + pPlotter + pModule + pPanel; + functionName = functionName.replace(/-/g, ""); + functionName = 'new' + functionName + return window[functionName](); } diff --git a/README.md b/README.md index 064891a42..85f6d4603 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # AAWeb -The main function of AAWeb is to serve the js files needed at the browser to run the Canvas App. +The main function of AAWeb is to serve the js files needed at the browser to run the Canvas App. ## Current Status @@ -22,8 +22,7 @@ You will need to clone these repos which contains the actual scripts of the Canv At the configuration file, you must verify that this is pointing to the place where you clonned the 2 previous repositories: ``` - "pathToFilesComponent": "../Files-Component/src", - "pathToCloudAppWrapperComponent": "../CloudApp-Wrapper-Component/src", + "pathToFilesComponent": "../Files-Component/src" ``` ## Execution diff --git a/Scripts/AppLoader.js b/Scripts/AppLoader.js index d03210f60..b2b2c587f 100644 --- a/Scripts/AppLoader.js +++ b/Scripts/AppLoader.js @@ -1,10 +1,9 @@ let dashboard -function newAppLoader () { +function newAppLoader() { const MODULE_NAME = 'App Loader' const INFO_LOG = false const ERROR_LOG = true - const INTENSIVE_LOG = false const logger = newWebDebugLog() logger.fileName = MODULE_NAME @@ -14,28 +13,26 @@ function newAppLoader () { return thisObject - function loadModules () { + async function loadModules() { try { if (INFO_LOG === true) { logger.write('[INFO] loadModules -> Entering function.') } + let ecosystem = await loadEcosystem() + window.localStorage.setItem('ecosystem', JSON.stringify(ecosystem)) + let plotters = getPlotters(ecosystem) + let modulesArray = [ - /* CloudWebScripts */ - - /* Plotters */ - - /* PlotterPanels */ - 'Globals.js', 'Ecosystem.js', - 'MQService', + 'MQService', - 'ChartsSpace/ChartUtilities.js', - 'ChartsSpace/PlottersManager.js', - 'ChartsSpace/TimelineChart.js', - 'ChartsSpace/TimeMachine.js', - 'ChartsSpace/ViewPort.js', - 'ChartsSpace/TimeLineCoordinateSystem.js', + 'ChartsSpace/ChartUtilities.js', + 'ChartsSpace/PlottersManager.js', + 'ChartsSpace/TimelineChart.js', + 'ChartsSpace/TimeMachine.js', + 'ChartsSpace/ViewPort.js', + 'ChartsSpace/TimeLineCoordinateSystem.js', 'TopSpace/CurrentEvent.js', 'TopSpace/EndUser.js', @@ -88,14 +85,14 @@ function newAppLoader () { 'Scales/RateScale.js', 'Scales/TimeScale.js', - 'Scales/TimePeriodScale.js', - 'Scales/Commons.js', + 'Scales/TimePeriodScale.js', + 'Scales/Commons.js', 'CockpitSpace/AssetBalances.js', 'CockpitSpace/Speedometer.js', - 'CockpitSpace/RestartSimulation.js', - 'CockpitSpace/GraphQLRestartSimulation.js', - 'CockpitSpace/FullScreen.js', + 'CockpitSpace/RestartSimulation.js', + 'CockpitSpace/GraphQLRestartSimulation.js', + 'CockpitSpace/FullScreen.js', 'Plotter.js', 'PlotterPanel.js', @@ -113,12 +110,12 @@ function newAppLoader () { 'Container.js', 'Displace.js', - 'Azure/azure-storage.blob.js', - 'Utilities.js', 'Dashboard.js' ] + modulesArray = modulesArray.concat(plotters) + let downloadedCounter = 0 let versionParam = window.canvasApp.version if (versionParam === undefined) { versionParam = '' } else { @@ -134,7 +131,7 @@ function newAppLoader () { if (INFO_LOG === true) { logger.write('[INFO] loadModules -> path = ' + path) } if (INFO_LOG === true) { logger.write('[INFO] loadModules -> total requested = ' + (i + 1)) } - function onRequired (pModule) { + function onRequired(pModule) { try { if (INFO_LOG === true) { logger.write('[INFO] loadModules -> onRequired -> Entering function.') } if (INFO_LOG === true) { logger.write('[INFO] loadModules -> onRequired -> Module Downloaded.') } @@ -160,4 +157,200 @@ function newAppLoader () { if (ERROR_LOG === true) { logger.write('[ERROR] loadModules -> err = ' + err) } } } + + async function loadEcosystem() { + if (INFO_LOG === true) { logger.write('[INFO] loadEcosystem -> Entering function.') } + + let headers + let accessToken = window.localStorage.getItem('access_token') + if (accessToken !== null) { + headers = { + authorization: 'Bearer ' + accessToken + } + } + + let response = await axios({ + url: window.canvasApp.graphQL.masterAppApiUrl, + method: 'post', + data: { + query: ` + query { + web_GetEcosystem { + id + devTeams { + codeName + displayName + host { + url + storage + container + accessKey + } + bots { + codeName + displayName + type + repo + configFile + products{ + codeName + displayName + description + dataSets{ + codeName + type + validPeriods + filePath + fileName + dataRange{ + filePath + fileName + } + } + exchangeList{ + name + } + plotter{ + codeName + devTeam + moduleName + repo + } + } + } + plotters { + codeName + displayName + modules{ + codeName + moduleName + description + profilePicture + panels{ + codeName + moduleName + event + } + } + repo + configFile + } + } + hosts { + codeName + displayName + host { + url + storage + container + accessKey + } + competitions { + codeName + displayName + description + startDatetime + finishDatetime + formula + plotter{ + codeName + host + moduleName + repo + } + rules + prizes + participants + repo + configFile + } + plotters { + codeName + displayName + modules{ + codeName + moduleName + description + profilePicture + panels{ + codeName + moduleName + event + } + } + repo + configFile + } + } + } + } + ` + }, + headers: headers + }) + + if (response.data.errors) { + if (ERROR_LOG === true) { console.log(spacePad(MODULE_NAME, 50) + ' : ' + '[ERROR] AppPreLoader -> loadEcosystem -> response.text = ' + JSON.stringify(res.data.errors)) } + throw error + } + + return response.data.data.web_GetEcosystem + } + + function getPlotters(ecosystem) { + if (INFO_LOG === true) { console.log('[INFO] server -> onBrowserRequest -> onFileRead -> addPlotters -> Entering function.') } + + + let plotters = [] + let devTeams = ecosystem.devTeams + let hosts = ecosystem.hosts + + plotters = plotters.concat(addScript(devTeams)) + plotters = plotters.concat(addScript(hosts)) + + return plotters + } + + function addScript(pDevTeamsOrHosts) { + if (INFO_LOG === true) { console.log('[INFO] server -> onBrowserRequest -> onFileRead -> addPlotters -> addScript -> Entering function.') } + const htmlLinePlotter = 'Plotters/@devTeam@/@repo@/@module@.js' + const htmlLinePlotterPanel = 'PlotterPanels/@devTeam@/@repo@/@module@.js' + let plotters = [] + + for (let i = 0; i < pDevTeamsOrHosts.length; i++) { + let devTeam = pDevTeamsOrHosts[i] + for (let j = 0; j < devTeam.plotters.length; j++) { + let plotter = devTeam.plotters[j] + if (plotter.modules !== undefined) { + for (let k = 0; k < plotter.modules.length; k++) { + let module = plotter.modules[k] + let htmlLineCopy = htmlLinePlotter + + let stringToInsert + stringToInsert = htmlLineCopy.replace('@devTeam@', devTeam.codeName) + stringToInsert = stringToInsert.replace('@repo@', plotter.repo) + stringToInsert = stringToInsert.replace('@module@', module.moduleName) + + plotters.push(stringToInsert) + + if (module.panels !== undefined) { + for (let l = 0; l < module.panels.length; l++) { + let panel = module.panels[l] + let htmlLineCopy = htmlLinePlotterPanel + + let stringToInsert + stringToInsert = htmlLineCopy.replace('@devTeam@', devTeam.codeName) + stringToInsert = stringToInsert.replace('@repo@', plotter.repo) + stringToInsert = stringToInsert.replace('@module@', panel.moduleName) + + plotters.push(stringToInsert) + } + } + } + } + } + } + return plotters + } + } diff --git a/server.js b/server.js index 43041f6f8..3f6d030ea 100644 --- a/server.js +++ b/server.js @@ -41,62 +41,30 @@ const MINIO_STORAGE = require('./Server/MinioStorage') let ecosystemObject = require("./Server/config/ecosystem.json"); switch (process.env.STORAGE_PROVIDER) { - case 'Azure': { - storage = AZURE_STORAGE.newAzureStorage(); - break; - } - case 'Minio': { - storage = MINIO_STORAGE.newMinioStorage(); - break; - } - default: { - console.log('[ERROR] server -> Storage Provider not supported -> process.env.STORAGE_PROVIDER = ' + process.env.STORAGE_PROVIDER) - return; - } + case 'Azure': { + storage = AZURE_STORAGE.newAzureStorage(); + break; + } + case 'Minio': { + storage = MINIO_STORAGE.newMinioStorage(); + break; + } + default: { + console.log('[ERROR] server -> Storage Provider not supported -> process.env.STORAGE_PROVIDER = ' + process.env.STORAGE_PROVIDER) + return; + } } initialize() -function initialize () { +function initialize() { if (CONSOLE_LOG === true) { console.log('[INFO] server -> initialize -> Entering function.') } - - /* Clear all cached information. */ - storageData = new Map() - - const CONFIG_READER = require('./Server/ConfigReader') - let configReader = CONFIG_READER.newConfigReader() - - configReader.initialize(ecosystemObject, storageData, storage, onInitialized) - - function onInitialized (err) { - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - console.log('[ERROR] server -> initialize -> onInitialized -> err.message = ' + err.message) - console.log('[ERROR] server -> initialize -> onInitialized -> Terminating Execution. ') - - return - } - - configReader.loadConfigs(onConfigsLoaded) - - function onConfigsLoaded (err, pEcosystemObject) { - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - console.log('[ERROR] server -> initialize -> onInitialized -> onConfigsLoaded -> err.message = ' + err.message) - console.log('[ERROR] server -> initialize -> onInitialized -> onConfigsLoaded -> Terminating Execution. ') - - return - } - - ecosystemObject = pEcosystemObject - - storage.initialize(storageData, ecosystemObject) // TODO do not initialize as a general option - - startHtttpServer () - } - } + storage.initialize(storageData, ecosystemObject) + startHtttpServer() } -function startHtttpServer () { +function startHtttpServer() { if (CONSOLE_LOG === true) { console.log('[INFO] server -> startHtttpServer -> Entering function.') } try { @@ -109,18 +77,17 @@ function startHtttpServer () { } } -function onBrowserRequest (request, response) { - if (CONSOLE_LOG === true) { console.log('[INFO] server -> onBrowserRequest -> Entering function.') } +function onBrowserRequest(request, response) { if (CONSOLE_LOG === true && request.url.indexOf('NO-LOG') === -1) { console.log('[INFO] server -> onBrowserRequest -> request.url = ' + request.url) } let requestParameters = request.url.split('/') if (requestParameters[1].indexOf('index.html') >= 0) { - /* - We use this to solve the problem when someone is arriving to the site with a sessionToken in the queryString. We extract here that - token, that will be sent later embedded into the HTML code, so that it can enter into the stardard circuit where any site can put - the sessionToken into their HTML code and from there the Browser app will log the user in. - */ + /* + We use this to solve the problem when someone is arriving to the site with a sessionToken in the queryString. We extract here that + token, that will be sent later embedded into the HTML code, so that it can enter into the stardard circuit where any site can put + the sessionToken into their HTML code and from there the Browser app will log the user in. + */ let queryString = requestParameters[1].split('?') @@ -144,245 +111,30 @@ function onBrowserRequest (request, response) { break case 'MQService': - { - let filePath = "./node_modules/@superalgos/mqservice/orderLifeCicle/webDependency.js" + { + let filePath = "./node_modules/@superalgos/mqservice/orderLifeCicle/webDependency.js" - respondWithFile(filePath, response) - break; - } + respondWithFile(filePath, response) + break; + } - // case 'FileService': - // { - - // processPost(request, response, onPostReceived) - - // function onPostReceived(pData) { - - // let request = JSON.parse(pData); - - // storage.readData(undefined, request.path, request.conatinerName, false, onReady) - - // function onReady(err, pFileContent) { - - // let responseToClient = { - // err: err, - // text: pFileContent - // }; - - // respondWithContent(JSON.stringify(responseToClient), response); - // } - // } - - // break; - // } case 'Plotter.js': { - /* - - This file is built dinamically because it has the code to instantiate the different configured Plotters. The instantiation code - will be generated using a pre-defined string with replacement points. We will go through the configuration file to learn - about all the possible plotters the system can load. - - */ - - let fs = require('fs') - try { - let fileName = 'Plotter.js' - fs.readFile(fileName, onFileRead) - - function onFileRead (err, file) { - if (CONSOLE_LOG === true) { console.log('[INFO] server -> onBrowserRequest -> onFileRead -> Entering function.') } - - try { - let fileContent = file.toString() - - /* This is the string we will use to insert into the Plotters.js script. */ - - let caseString = '' + - ' case "@newFunctionName@":' + '\n' + - ' {' + '\n' + - ' plotter = newPlotterName();' + '\n' + - ' }' + '\n' + - ' break;' + '\n' + '\n' - - let devTeams = ecosystemObject.devTeams - let hosts = ecosystemObject.hosts - - addToFileContent(devTeams) - addToFileContent(hosts) - - function addToFileContent (pDevTeamsOrHosts) { - if (CONSOLE_LOG === true) { console.log('[INFO] server -> onBrowserRequest -> onFileRead -> addToFileContent -> Entering function.') } - - for (let i = 0; i < pDevTeamsOrHosts.length; i++) { - let devTeam = pDevTeamsOrHosts[i] - - for (let j = 0; j < devTeam.plotters.length; j++) { - let plotter = devTeam.plotters[j] - - for (let k = 0; k < plotter.modules.length; k++) { - - let module = plotter.modules[k] - - let caseStringCopy = caseString - - let newFunctionName = devTeam.codeName + plotter.codeName + module.codeName - newFunctionName = newFunctionName.replace(/-/g, '') - - let stringToInsert - stringToInsert = caseStringCopy.replace('@newFunctionName@', newFunctionName) - stringToInsert = stringToInsert.replace('newPlotterName', 'new' + newFunctionName) - - let firstPart = fileContent.substring(0, fileContent.indexOf('// Cases')) - let secondPart = fileContent.substring(fileContent.indexOf('// Cases')) - - fileContent = firstPart + stringToInsert + secondPart - } - } - } - } - - respondWithContent(fileContent, response) - } catch (err) { - console.log('[ERROR] server -> onBrowserRequest -> File Not Found: ' + fileName + ' or Error = ' + err) - } - } - } catch (err) { - console.log(err) - } + respondWithFile('./Plotter.js', response) } break case 'PlotterPanel.js': { - /* - This file is build dinamically because it has the code to instantiate the different configured Plotter Panels. The instantiation code - will be generated using a pre-defined string with replacement points. We will go through the configuration file to learn - about all the possible plotters panels the system can load. - - */ - - let fs = require('fs') - try { - let fileName = 'PlotterPanel.js' - fs.readFile(fileName, onFileRead) - - function onFileRead (err, file) { - if (CONSOLE_LOG === true) { console.log('[INFO] server -> onBrowserRequest -> onFileRead -> Entering function.') } - - try { - let fileContent = file.toString() - - /* This is the string we will use to insert into the Plotters.js script. */ - - let caseString = '' + - ' case "@newFunctionName@":' + '\n' + - ' {' + '\n' + - ' plotterPanel = newPlotterPanelName();' + '\n' + - ' }' + '\n' + - ' break;' + '\n' + '\n' - - let devTeams = ecosystemObject.devTeams - let hosts = ecosystemObject.hosts - - addToFileContent(devTeams) - addToFileContent(hosts) - - function addToFileContent (pDevTeamsOrHosts) { - if (CONSOLE_LOG === true) { console.log('[INFO] server -> onBrowserRequest -> onFileRead -> addToFileContent -> Entering function.') } - - for (let i = 0; i < pDevTeamsOrHosts.length; i++) { - let devTeam = pDevTeamsOrHosts[i] - - for (let j = 0; j < devTeam.plotters.length; j++) { - let plotter = devTeam.plotters[j] - - for (let k = 0; k < plotter.modules.length; k++) { - - let module = plotter.modules[k] - - for (let l = 0; l < module.panels.length; l++) { - - let panel = module.panels[l] - - let caseStringCopy = caseString - - let newFunctionName = devTeam.codeName + plotter.codeName + module.codeName + panel.codeName - newFunctionName = newFunctionName.replace(/-/g, '') - - let stringToInsert - stringToInsert = caseStringCopy.replace('@newFunctionName@', newFunctionName) - stringToInsert = stringToInsert.replace('newPlotterPanelName', 'new' + newFunctionName) - - let firstPart = fileContent.substring(0, fileContent.indexOf('// Cases')) - let secondPart = fileContent.substring(fileContent.indexOf('// Cases')) - - fileContent = firstPart + stringToInsert + secondPart - } - } - } - } - } - - respondWithContent(fileContent, response) - } catch (err) { - console.log('[ERROR] server -> onBrowserRequest -> File Not Found: ' + fileName + ' or Error = ' + err) - } - } - } catch (err) { - console.log(err) - } + respondWithFile('./PlotterPanel.js', response) } break + case 'Ecosystem.js': { - /* - - At this page we need to insert the configuration file for the whole system that we assamble before at the begining of this module - execution. So what we do is to load a template file with an insertion point where the configuration json is injected in. - - */ - - let fs = require('fs') - try { - let fileName = 'Ecosystem.js' - fs.readFile(fileName, onFileRead) - - function onFileRead (err, file) { - if (CONSOLE_LOG === true) { console.log('[INFO] server -> onBrowserRequest -> onFileRead -> Entering function.') } - - try { - let fileContent = file.toString() - let insertContent = JSON.stringify(ecosystemObject) - - fileContent = fileContent.replace('"@ecosystem.json@"', insertContent) - - respondWithContent(fileContent, response) - } catch (err) { - console.log('[ERROR] server -> onBrowserRequest -> File Not Found: ' + fileName + ' or Error = ' + err) - } - } - } catch (err) { - console.log(err) - } - } - break - - case 'CloudAppWrapper': // This means the BrowserRun folder. - { - let filePath = requestParameters[2] - - if (requestParameters[3] !== undefined) { - filePath = filePath + '/' + requestParameters[3] - } - - if (requestParameters[4] !== undefined) { - filePath = filePath + '/' + requestParameters[4] - } - - respondWithFile(process.env.PATH_TO_CLOUD_APP_WRAPPER_COMPONENT + '/' + filePath, response) + respondWithFile('./Ecosystem.js', response) } break @@ -427,120 +179,26 @@ function onBrowserRequest (request, response) { break case 'StrategySpace': // This means the StrategySpace folder. - { - respondWithFile(process.env.PATH_TO_CANVAS_APP + '/StrategySpace/' + requestParameters[2], response) - } - break + { + respondWithFile(process.env.PATH_TO_CANVAS_APP + '/StrategySpace/' + requestParameters[2], response) + } + break case 'ControlsToolBox': // This means the StrategySpace folder. - { - respondWithFile(process.env.PATH_TO_CANVAS_APP + '/ControlsToolBox/' + requestParameters[2], response) - } - break + { + respondWithFile(process.env.PATH_TO_CANVAS_APP + '/ControlsToolBox/' + requestParameters[2], response) + } + break case 'Utilities': // This means the StrategySpace folder. - { - respondWithFile(process.env.PATH_TO_CANVAS_APP + '/Utilities/' + requestParameters[2], response) - } - break + { + respondWithFile(process.env.PATH_TO_CANVAS_APP + '/Utilities/' + requestParameters[2], response) + } + break case 'Scripts': // This means the Scripts folder. { - if (requestParameters[2] === 'AppLoader.js') { - let fs = require('fs') - try { - let fileName = './Scripts/' + 'AppLoader.js' - fs.readFile(fileName, onFileRead) - - function onFileRead (err, file) { - if (CONSOLE_LOG === true) { console.log('[INFO] server -> onBrowserRequest -> onFileRead -> Entering function.') } - - try { - let fileContent = file.toString() - - addPlotters() - - function addPlotters () { - if (CONSOLE_LOG === true) { console.log('[INFO] server -> onBrowserRequest -> onFileRead -> addPlotters -> Entering function.') } - - let htmlLinePlotter = '' + '\n' + - ' "Plotters/@devTeam@/@repo@/@module@.js",' - - let htmlLinePlotterPanel = '' + '\n' + - ' "PlotterPanels/@devTeam@/@repo@/@module@.js",' - - let devTeams = ecosystemObject.devTeams - let hosts = ecosystemObject.hosts - - addScript(devTeams) - addScript(hosts) - - function addScript (pDevTeamsOrHosts) { - if (CONSOLE_LOG === true) { console.log('[INFO] server -> onBrowserRequest -> onFileRead -> addPlotters -> addScript -> Entering function.') } - - for (let i = 0; i < pDevTeamsOrHosts.length; i++) { - - let devTeam = pDevTeamsOrHosts[i] - - for (let j = 0; j < devTeam.plotters.length; j++) { - - let plotter = devTeam.plotters[j] - - if (plotter.modules !== undefined) { - - for (let k = 0; k < plotter.modules.length; k++) { - - let module = plotter.modules[k] - - let htmlLineCopy = htmlLinePlotter - - let stringToInsert - stringToInsert = htmlLineCopy.replace('@devTeam@', devTeam.codeName) - stringToInsert = stringToInsert.replace('@repo@', plotter.repo) - stringToInsert = stringToInsert.replace('@module@', module.moduleName) - - let firstPart = fileContent.substring(0, fileContent.indexOf('/* Plotters */') + 14) - let secondPart = fileContent.substring(fileContent.indexOf('/* Plotters */') + 14) - - fileContent = firstPart + stringToInsert + secondPart - - if (module.panels !== undefined) { - - for (let l = 0; l < module.panels.length; l++) { - - let panel = module.panels[l] - - let htmlLineCopy = htmlLinePlotterPanel - - let stringToInsert - stringToInsert = htmlLineCopy.replace('@devTeam@', devTeam.codeName) - stringToInsert = stringToInsert.replace('@repo@', plotter.repo) - stringToInsert = stringToInsert.replace('@module@', panel.moduleName) - - let firstPart = fileContent.substring(0, fileContent.indexOf('/* PlotterPanels */') + 19) - let secondPart = fileContent.substring(fileContent.indexOf('/* PlotterPanels */') + 19) - - fileContent = firstPart + stringToInsert + secondPart - } - } - } - } - } - } - } - } - - respondWithContent(fileContent, response) - } catch (err) { - console.log('[ERROR] server -> onBrowserRequest -> File Not Found: ' + fileName + ' or Error = ' + err) - } - } - } catch (err) { - console.log(err) - } - } else { - respondWithFile('./Scripts/' + requestParameters[2], response) - } + respondWithFile('./Scripts/' + requestParameters[2], response) } break @@ -548,7 +206,7 @@ function onBrowserRequest (request, response) { { storage.readData(requestParameters[2] + '/' + 'plotters', requestParameters[3], requestParameters[4], true, onDataArrived) - function onDataArrived (err, pData) { + function onDataArrived(err, pData) { if (err.result !== global.DEFAULT_OK_RESPONSE.result) { console.log('[ERROR] server -> onBrowserRequest -> Plotters -> onDataArrived -> Could not read a file. ') console.log('[ERROR] server -> onBrowserRequest -> Plotters -> onDataArrived -> err.message = ' + err.message) @@ -565,7 +223,7 @@ function onBrowserRequest (request, response) { { storage.readData(requestParameters[2] + '/' + 'plotters', requestParameters[3], requestParameters[4], true, onDataArrived) - function onDataArrived (err, pData) { + function onDataArrived(err, pData) { if (err.result !== global.DEFAULT_OK_RESPONSE.result) { console.log('[ERROR] server -> onBrowserRequest -> Plotters -> PlotterPanels -> Could not read a file. ') console.log('[ERROR] server -> onBrowserRequest -> Plotters -> PlotterPanels -> err.message = ' + err.message) @@ -620,10 +278,10 @@ function onBrowserRequest (request, response) { break case 'ChartsSpace': - { - respondWithFile(process.env.PATH_TO_CANVAS_APP + '/' + requestParameters[1] + '/' + requestParameters[2], response) - } - break + { + respondWithFile(process.env.PATH_TO_CANVAS_APP + '/' + requestParameters[1] + '/' + requestParameters[2], response) + } + break default: { @@ -631,20 +289,20 @@ function onBrowserRequest (request, response) { } } - function homePage () { + function homePage() { if (requestParameters[1] === '') { let fs = require('fs') try { let fileName = 'index.html' fs.readFile(fileName, onFileRead) - function onFileRead (err, file) { + function onFileRead(err, file) { if (CONSOLE_LOG === true) { console.log('[INFO] server -> onBrowserRequest -> onFileRead -> Entering function.') } try { let fileContent = file.toString() - /* The second request parameters is the sessionToken, if it exists. */ + /* The second request parameters is the sessionToken, if it exists. */ if (requestParameters[2] !== '' && requestParameters[2] !== undefined) { fileContent = fileContent.replace("window.canvasApp.sessionToken = ''", "window.canvasApp.sessionToken = '" + requestParameters[2] + "'") @@ -664,7 +322,7 @@ function onBrowserRequest (request, response) { } } -function respondWithContent (content, response) { +function respondWithContent(content, response) { if (CONSOLE_LOG === true) { console.log('[INFO] server -> respondWithContent -> Entering function.') } try { @@ -676,13 +334,13 @@ function respondWithContent (content, response) { response.writeHead(200, { 'Content-Type': 'text/html' }) response.write(content) response.end('\n') - // console.log("Content Sent: " + content); + // console.log("Content Sent: " + content); } catch (err) { returnEmptyArray(response) } } -function respondWithFile (fileName, response) { +function respondWithFile(fileName, response) { if (CONSOLE_LOG === true) { console.log('[INFO] server -> respondWithFile -> Entering function.') } let fs = require('fs') @@ -694,7 +352,7 @@ function respondWithFile (fileName, response) { fs.readFile(fileName, onFileRead) - function onFileRead (err, file) { + function onFileRead(err, file) { if (CONSOLE_LOG === true) { console.log('[INFO] server -> respondWithFile -> onFileRead -> Entering function.') } try { @@ -705,11 +363,11 @@ function respondWithFile (fileName, response) { response.setHeader('Expires', '0') // Proxies. response.setHeader('Access-Control-Allow-Origin', '*') // Allows to access data from other domains. - // response.writeHead(200, { 'Content-Type': 'text/html' }); + // response.writeHead(200, { 'Content-Type': 'text/html' }); response.write(htmlResponse) response.end('\n') - // console.log("File Sent: " + fileName); - // + // console.log("File Sent: " + fileName); + // } catch (err) { console.log('[ERROR] server -> respondWithFile -> onFileRead -> File Not Found: ' + fileName + ' or Error = ' + err) returnEmptyArray() @@ -720,14 +378,14 @@ function respondWithFile (fileName, response) { } } -function respondWithImage (fileName, response) { +function respondWithImage(fileName, response) { if (CONSOLE_LOG === true) { console.log('[INFO] server -> respondWithImage -> Entering function.') } let fs = require('fs') try { fs.readFile(fileName, onFileRead) - function onFileRead (err, file) { + function onFileRead(err, file) { if (CONSOLE_LOG === true) { console.log('[INFO] server -> respondWithImage -> onFileRead -> Entering function.') } try { @@ -747,7 +405,7 @@ function respondWithImage (fileName, response) { } } -function returnEmptyArray (response) { +function returnEmptyArray(response) { try { if (CONSOLE_LOG === true) { console.log('[INFO] server -> respondWithFile -> returnEmptyArray -> Entering function.') } @@ -762,26 +420,3 @@ function returnEmptyArray (response) { console.log('[ERROR] server -> returnEmptyArray -> err.message ' + err.message) } } - -function processPost (request, response, callback) { - let data = '' - if (typeof callback !== 'function') return null - - if (request.method == 'POST') { - request.on('data', function (pData) { - data += pData - if (data.length > 1e6) { - data = '' - response.writeHead(413, { 'Content-Type': 'text/plain' }).end() - request.connection.destroy() - } - }) - - request.on('end', function () { - callback(data) - }) - } else { - response.writeHead(405, { 'Content-Type': 'text/plain' }) - response.end() - } -}