From e4fc210f11b686e4f473000b39fbee575fb4f4df Mon Sep 17 00:00:00 2001 From: Matias Date: Fri, 24 May 2019 20:08:44 -0300 Subject: [PATCH] Partial commit for saweb-api migration --- .dockerignore | 2 - .gitignore | 2 - AABrowserAPI/TeamSetup.js | 9 +- Project.njsproj | 1 - Server/AzureStorage.js | 239 ++++++------------- Server/AzureStorageAccessManager.js | 79 ------ Server/BotsScripts.js | 9 +- Server/ConfigReader.js | 176 ++------------ Server/MinioStorage.js | 4 +- Server/SessionManager.js | 82 ++++--- index.html | 3 +- server.js | 358 +++++----------------------- 12 files changed, 193 insertions(+), 771 deletions(-) delete mode 100644 Server/AzureStorageAccessManager.js diff --git a/.dockerignore b/.dockerignore index 3ca193c28..04dcc9fab 100644 --- a/.dockerignore +++ b/.dockerignore @@ -19,5 +19,3 @@ obj/* bin/* /.vscode Project.njsproj - -this.server.config.dev.json diff --git a/.gitignore b/.gitignore index 73453af37..e475fff35 100644 --- a/.gitignore +++ b/.gitignore @@ -6,8 +6,6 @@ bin/* .env .env.dev -this.server.config.json -this.server.config.dev.json /node_modules /dependency diff --git a/AABrowserAPI/TeamSetup.js b/AABrowserAPI/TeamSetup.js index 07113f136..fababcc51 100644 --- a/AABrowserAPI/TeamSetup.js +++ b/AABrowserAPI/TeamSetup.js @@ -6,12 +6,11 @@ initialize: initialize } - let masterAppServerURL, storage + let storage return thisObject; - function initialize(pServerConfig, pStorage) { - masterAppServerURL = pServerConfig.masterAppServerURL + function initialize(pStorage) { storage = pStorage } @@ -579,7 +578,7 @@ const graphqlClient = require('graphql-client') const usersModuleAPI = graphqlClient({ - url: masterAppServerURL + url: process.env.GATEWAY_ENDPOINT }); @@ -1081,7 +1080,7 @@ const graphqlClient = require('graphql-client') const usersModuleAPI = graphqlClient({ - url: masterAppServerURL + url: process.env.GATEWAY_ENDPOINT }); diff --git a/Project.njsproj b/Project.njsproj index b1341553a..0a6af2c4c 100644 --- a/Project.njsproj +++ b/Project.njsproj @@ -77,7 +77,6 @@ - diff --git a/Server/AzureStorage.js b/Server/AzureStorage.js index 780b20f03..62bbf1710 100644 --- a/Server/AzureStorage.js +++ b/Server/AzureStorage.js @@ -10,26 +10,14 @@ } let storageData; - let serverConfig; - - let Minio = require('minio'); - let minioClient; + let ecosystem; return thisObject; - function initialize(pStorageData, pServerConfig) { + function initialize(pStorageData, pEcosystem) { storageData = pStorageData; - serverConfig = pServerConfig; - - minioClient = new Minio.Client({ - endPoint: process.env.MINIO_END_POINT, - port: JSON.parse(process.env.MINIO_PORT), - useSSL: JSON.parse(process.env.MINIO_USE_SSL), - accessKey: process.env.MINIO_ACCESS_KEY, - secretKey: process.env.MINIO_SECRET_KEY - }) - + ecosystem = pEcosystem; } function readData(pOrg, pPath, pFile, saveAtCache, callBackFunction) { @@ -58,56 +46,8 @@ if (CONSOLE_LOG === true) { console.log("[INFO] Azure Storage -> readData -> " + pOrg + '.' + pPath + '.' + pFile + " NOT found at cache."); } - let storage = require('azure-storage'); - let connectionString; - - switch (serverConfig.environment) { - - case "Develop": { - - connectionString = serverConfig.configAndPlugins.Develop.connectionString; - break; - } - - case "Production": { - - connectionString = serverConfig.configAndPlugins.Production.connectionString; - break; - } - } - - let blobService = storage.createBlobService(connectionString); - - let pParam1 = pOrg; - let pParam2 = pPath; - let pParam3 = pFile; - let container = 'aaplatform'; - let azurePath = pOrg + "/" + pPath + "/" + pFile; - - let bucketName = 'aaplatform'; - let textFilename; - - if (pParam1 === undefined) { - - /* This is used when the requests come from the browser, for background compatibility. */ - - container = pParam3; - azurePath = pParam2; - - bucketName = pParam3; - textFilename = pParam2.substring(pParam2.indexOf("/") + 1, pParam2.length); - - } else { - - /* This is used for internal requests within AAWeb, for background compatibility. */ - - bucketName = 'aaplatform'; - textFilename = pParam1 + "/" + pParam2 + "/" + pParam3; - } - - - blobService.getBlobToText(container, azurePath, onFileReceived); - + let connectionDetails = getConnectionDetails(pOrg, pPath, pFile); + connectionDetails.blobService.getBlobToText(connectionDetails.container, connectionDetails.filePath, onFileReceived); function onFileReceived(err, text, response) { @@ -143,23 +83,6 @@ } - /* TEMPORARY CODE TO UPDATE THE MINIO SERVER DURING TRANSITION. */ - - // try { - - // minioClient.putObject(bucketName, textFilename, text, 'text/plain', function (err) { - // if (err) { - // console.log("ERROR AT MINIO SERVER PUTTING OBJECT : " + err); - // return; - // } - // console.log(textFilename + ' Successfully uploaded ' + textFilename + ' the MINIO SERVER'); - // }) - - // } catch (err) { - // console.log("ERROR UPDATING MINIO SERVER : " + err); - // } - - callBackFunction(global.DEFAULT_OK_RESPONSE, text); } catch (err) { @@ -175,6 +98,7 @@ } } + // TODO remove this functions and resolve them inside the Teams module. function writeData(pOrg, pPath, pFile, pFileContent, callBackFunction) { try { @@ -184,29 +108,9 @@ if (CONSOLE_LOG === true) { console.log("[INFO] Azure Storage -> writeData -> pPath = " + pPath); } if (CONSOLE_LOG === true) { console.log("[INFO] Azure Storage -> writeData -> pFile = " + pFile); } - let storage = require('azure-storage'); - let connectionString; - - switch (serverConfig.environment) { - - case "Develop": { - - connectionString = serverConfig.configAndPlugins.Develop.connectionString; - break; - } - - case "Production": { - - connectionString = serverConfig.configAndPlugins.Production.connectionString; - break; - } - } - - let blobService = storage.createBlobService(connectionString); - let blobPath = pOrg + "/" + pPath + "/" + pFile; let blobText = pFileContent.toString(); - - blobService.createBlockBlobFromText('aaplatform', blobPath, blobText, onFileCreated); + let connectionDetails = getConnectionDetails(pOrg, pPath, pFile); + connectionDetails.blobService.createBlockBlobFromText(connectionDetails.container, connectionDetails.filePath, blobText, onFileCreated); function onFileCreated(err, text, response) { @@ -252,28 +156,8 @@ if (CONSOLE_LOG === true) { console.log("[INFO] Azure Storage -> deleteBlob -> pPath = " + pPath); } if (CONSOLE_LOG === true) { console.log("[INFO] Azure Storage -> deleteBlob -> pFile = " + pFile); } - let storage = require('azure-storage'); - let connectionString; - - switch (serverConfig.environment) { - - case "Develop": { - - connectionString = serverConfig.configAndPlugins.Develop.connectionString; - break; - } - - case "Production": { - - connectionString = serverConfig.configAndPlugins.Production.connectionString; - break; - } - } - - let blobService = storage.createBlobService(connectionString); - let blobPath = pOrg + "/" + pPath + "/" + pFile; - - blobService.deleteBlob('aaplatform', blobPath, onBlobDeleted); + let connectionDetails = getConnectionDetails(pOrg, pPath, pFile); + connectionDetails.blobService.deleteBlob(connectionDetails.container, connectionDetails.filePath, onBlobDeleted); function onBlobDeleted(err, text, response) { @@ -317,28 +201,11 @@ if (CONSOLE_LOG === true) { console.log("[INFO] Azure Storage -> createContainer -> Entering function."); } if (CONSOLE_LOG === true) { console.log("[INFO] Azure Storage -> createContainer -> pContainerName = " + pContainerName); } - let storage = require('azure-storage'); - let connectionString; - - switch (serverConfig.environment) { - - case "Develop": { - - connectionString = serverConfig.configAndPlugins.Develop.connectionString; - break; - } - - case "Production": { - - connectionString = serverConfig.configAndPlugins.Production.connectionString; - break; - } - } - - let blobService = storage.createBlobService(connectionString); - let containerName = pContainerName.toLowerCase(); + let storage = require('azure-storage'); + let connectionString = process.env.STORAGE_CONNECTION_STRING; + let blobService = storage.createBlobService(connectionString); blobService.createContainer(containerName, onContainerCreated); function onContainerCreated(err) { @@ -366,9 +233,6 @@ callBackFunction(global.DEFAULT_FAIL_RESPONSE); return; } - - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; } callBackFunction(global.DEFAULT_OK_RESPONSE); @@ -388,28 +252,11 @@ if (CONSOLE_LOG === true) { console.log("[INFO] Azure Storage -> deleteContainer -> Entering function."); } if (CONSOLE_LOG === true) { console.log("[INFO] Azure Storage -> deleteContainer -> pContainerName = " + pContainerName); } - let storage = require('azure-storage'); - let connectionString; - - switch (serverConfig.environment) { - - case "Develop": { - - connectionString = serverConfig.configAndPlugins.Develop.connectionString; - break; - } - - case "Production": { - - connectionString = serverConfig.configAndPlugins.Production.connectionString; - break; - } - } - - let blobService = storage.createBlobService(connectionString); - let containerName = pContainerName.toLowerCase(); + let storage = require('azure-storage'); + let connectionString = process.env.STORAGE_CONNECTION_STRING; + let blobService = storage.createBlobService(connectionString); blobService.deleteContainer(containerName, onContainerDeleted); function onContainerDeleted(err) { @@ -437,9 +284,6 @@ callBackFunction(global.DEFAULT_FAIL_RESPONSE); return; } - - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; } callBackFunction(global.DEFAULT_OK_RESPONSE); @@ -451,4 +295,55 @@ callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } + + function getConnectionDetails(pOrg, pPath, pFile){ + let filePath, container, host, accessKey, teamCodeName; + + if (pOrg === undefined) { + /* This is used when the requests come from the browser, for background compatibility. */ + filePath = pPath; + teamCodeName = pFile; + } else { + /* This is used for internal requests within AAWeb, for background compatibility. */ + filePath = pOrg + "/" + pPath + "/" + pFile; + teamCodeName = pOrg.split("/")[0]; + } + + // TODO Temporary looking on on the server side for the team connection, so the client calls are not modified. + for (let i = 0; i < ecosystem.devTeams.length; i++) { + const devTeam = ecosystem.devTeams[i]; + if(devTeam.codeName.toLowerCase() === teamCodeName.toLowerCase()){ + host = devTeam.host.storage; + accessKey = devTeam.host.accessKey; + container = devTeam.host.container; + break; + } + } + + for (let i = 0; i < ecosystem.hosts.length; i++) { + const devTeam = ecosystem.hosts[i]; + if(devTeam.codeName.toLowerCase() === teamCodeName.toLowerCase()){ + host = devTeam.host.storage; + accessKey = devTeam.host.accessKey; + container = devTeam.host.container; + break; + } + } + + if(host === undefined){ + console.log("[ERROR] Azure Storage -> readData -> Host has not been found for team: " + teamCodeName ); + } + + if(accessKey === undefined){ + console.log("[ERROR] Azure Storage -> readData -> Access key has not been found for team: " + teamCodeName); + } + + let storage = require('azure-storage'); + let blobService = storage.createBlobServiceWithSas(host, accessKey); + return { + blobService, + container, + filePath + } + } } diff --git a/Server/AzureStorageAccessManager.js b/Server/AzureStorageAccessManager.js deleted file mode 100644 index d3c7da237..000000000 --- a/Server/AzureStorageAccessManager.js +++ /dev/null @@ -1,79 +0,0 @@ -exports.newAzureStorageAccessManager = function newAzureStorageAccessManager() { - - let thisObject = { - getPermission: getPermission, - initialize: initialize - } - - let connectionString; - let blobEndpoint; - - return thisObject; - - function initialize(serverConfig, callBackFunction) { - - switch (serverConfig.environment) { - - case "Develop": { - - blobEndpoint = serverConfig.productsStorage.Develop.fileUri; - connectionString = serverConfig.configAndPlugins.Develop.connectionString; - break; - } - - case "Production": { - - blobEndpoint = serverConfig.productsStorage.Production.fileUri; - connectionString = serverConfig.configAndPlugins.Production.connectionString; - break; - } - } - - callBackFunction(); - } - - function getPermission(pContainer, pType, pDays) { - - let azure = require('azure-storage'); - let blobService = azure.createBlobService(connectionString); - - let startDate = new Date(); - startDate.setDate(startDate.getDate() - 1); - - let expiryDate = new Date(startDate); - expiryDate.setDate(startDate.getDate() + pDays); - - let azurePermissions; - - switch (pType) { - case "READ": { - azurePermissions = azure.BlobUtilities.SharedAccessPermissions.READ; - break; - } - case "WRITE": { - azurePermissions = azure.BlobUtilities.SharedAccessPermissions.WRITE; - break; - } - case "DELETE": { - azurePermissions = azure.BlobUtilities.SharedAccessPermissions.DELETE; - break; - } - } - - let blobName = undefined; - - let sharedAccessPolicy = { - AccessPolicy: { - Permissions: azurePermissions, - Start: startDate, - Expiry: expiryDate - } - }; - - let sasToken = blobService.generateSharedAccessSignature(pContainer, blobName, sharedAccessPolicy); - - return "BlobEndpoint=" + blobEndpoint + ";SharedAccessSignature=" + sasToken + ""; - - } -} - diff --git a/Server/BotsScripts.js b/Server/BotsScripts.js index c0fb31b91..a8f4d3d87 100644 --- a/Server/BotsScripts.js +++ b/Server/BotsScripts.js @@ -7,18 +7,15 @@ let storage; - let serverConfig; - let storageData; - return thisObject; - function initialize(pServerConfig, pStorage, callBackFunction) { + function initialize(ecosystem, pStorage, callBackFunction) { storage = pStorage; let storageData = new Map(); // For backwards compatibility only. Not used. - storage.initialize(storageData, pServerConfig); + storage.initialize(storageData, ecosystem); callBackFunction(); } @@ -76,4 +73,4 @@ return text.replace(new RegExp(search, 'g'), replacement); } } -} \ No newline at end of file +} diff --git a/Server/ConfigReader.js b/Server/ConfigReader.js index 954aae42b..97b5afa20 100644 --- a/Server/ConfigReader.js +++ b/Server/ConfigReader.js @@ -5,128 +5,30 @@ initialize: initialize } - const GITHUB = require('../Server/Github'); - let github = GITHUB.newGithub(); - let storage; - - let serverConfig; let storageData; - let ecosystem; let ecosystemObject; return thisObject; - function initialize(pEcosystem, pEcosystemObject, pStorageData, pStorage, callBackFunction) { - + function initialize(pEcosystemObject, pStorageData, pStorage, callBackFunction) { storage = pStorage; storageData = pStorageData; - ecosystem = pEcosystem; ecosystemObject = pEcosystemObject; - readAAWebConfig(); - - function readAAWebConfig() { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] ConfigReader -> readAAWebConfig -> Entering function."); } - - let fs = require('fs'); - - let fileName = './this.server.config.json'; - fs.readFile(fileName, onFileRead); - - function onFileRead(err, file) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] ConfigReader -> readAAWebConfig -> onFileRead -> Entering function."); } - - let fileText; - - fileText = file.toString(); - fileText = fileText.trim(); // remove first byte with some encoding. - - serverConfig = JSON.parse(fileText); - - if (LOG_FILE_CONTENT === true) { console.log("[INFO] ConfigReader -> readAAWebConfig -> onFileRead -> fileText = " + fileText); } - - CONSOLE_LOG = serverConfig.webServerLog.console; - LOG_FILE_CONTENT = serverConfig.webServerLog.fileContent; - - /* Finalize initializations. */ - - storage.initialize(storageData, serverConfig); - - callBackFunction(global.DEFAULT_OK_RESPONSE, serverConfig); - } - catch (err) { - console.log("[ERROR] ConfigReader -> readAAWebConfig -> onFileRead -> File = " + fileName + " Error = " + err); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - - } - } - catch (err) { - console.log("[ERROR] ConfigReader -> readAAWebConfig -> Error = " + err); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } + try { + if (CONSOLE_LOG === true) { console.log("[INFO] ConfigReader -> initialize -> Initializing storage."); } + storage.initialize(storageData, ecosystemObject); + callBackFunction(global.DEFAULT_OK_RESPONSE); + } catch (err) { + console.log("[ERROR] ConfigReader -> initialize -> Error initilizing the storage. Stack:", err.stack); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } function loadConfigs(callBackFunction) { - readEcosystemConfig(); - - function readEcosystemConfig() { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] ConfigReader -> readEcosystemConfig -> Entering function."); } - - /* - - This configuration file is the backbone of the system. The first file we are going to get is a template where other configurations are - injected and the files ends up inflated with all these configs in one single JSON object that in turn is later injected into a - javascript module with an object that is going to instantiate it at run-time. - - */ - - storage.readData('AdvancedAlgos', 'AAPlatform', 'ecosystem.json', true, onDataArrived); - - function onDataArrived(err, pData) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] ConfigReader -> readEcosystemConfig -> Cloud -> onDataArrived -> Entering function."); } - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - - console.log("[ERROR] ConfigReader -> readEcosystemConfig -> Cloud -> onDataArrived -> Could not read a file. "); - console.log("[ERROR] ConfigReader -> readEcosystemConfig -> Cloud -> onDataArrived -> err.message = " + err.message); - - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - - ecosystem = pData.toString(); - ecosystem = ecosystem.trim(); // remove first byte with some encoding. - - ecosystemObject = JSON.parse(ecosystem); - readHostsConfigs(); - } - catch (err) { - console.log("[ERROR] ConfigReader -> loadConfigs -> readEcosystemConfig -> onDataArrived -> Error = " + err); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } - catch (err) { - console.log("[ERROR] ConfigReader -> loadConfigs -> readEcosystemConfig -> Error = " + err); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } + readHostsConfigs(); function readHostsConfigs() { @@ -294,13 +196,13 @@ if (CONSOLE_LOG === true) { console.log("[INFO] ConfigReader -> readDevTeamsConfigs -> Entering function."); } /* - + Each bot has its configuration at its own repo since each team must be able to change it at will. So what we do here is to use the master config at the AAPlatform repo that we already have on memory and inject into it the config of each bot. - + Inmediatelly after that, we also load the Plotters configs using the same technique. - + */ let requestsSent = 0; @@ -361,8 +263,6 @@ configObj.repo = bot.repo; configObj.configFile = bot.configFile; - addStoragePermissions(configObj); - /* Here we will add all the possible Clones of this bot */ if (configObj.genes !== undefined) { @@ -387,9 +287,9 @@ } - /* + /* Now we have all the possible genes values in a multi-dimensional matrix. - We will go thorugh each of the elements of each dimension and get each combination possible with the other dimmensions. + We will go thorugh each of the elements of each dimension and get each combination possible with the other dimmensions. */ let combinations = []; @@ -478,7 +378,7 @@ if (requestsSent === responsesReceived) { - callBackFunction(global.DEFAULT_OK_RESPONSE, ecosystem, ecosystemObject); + callBackFunction(global.DEFAULT_OK_RESPONSE, ecosystemObject); } @@ -550,7 +450,7 @@ if (requestsSent === responsesReceived) { - callBackFunction(global.DEFAULT_OK_RESPONSE, ecosystem, ecosystemObject); + callBackFunction(global.DEFAULT_OK_RESPONSE, ecosystemObject); } } @@ -569,48 +469,6 @@ } } - function addStoragePermissions(pConfigObj) { - - try { - - if (CONSOLE_LOG === true) { console.log("[INFO] ConfigReader -> readDevTeamsConfigs -> addStoragePermissions -> Entering function."); } - - if (pConfigObj.storage !== undefined) { return; } // If this information is at the config file, then we take it, otherwise, we define it here. - - let fileUri; - let sas; - - switch (serverConfig.environment) { - - case "Develop": { - - fileUri = serverConfig.productsStorage.Develop.fileUri; - sas = serverConfig.productsStorage.Develop.sas; - - break; - } - - case "Production": { - - fileUri = serverConfig.productsStorage.Production.fileUri; - sas = serverConfig.productsStorage.Production.sas; - - break; - } - } - - pConfigObj.storage = { - sas: sas, - fileUri: fileUri - }; - - } - catch (err) { - console.log("[ERROR] ConfigReader -> readDevTeamsConfigs -> addStoragePermissions -> Error = " + err); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } - } - } catch (err) { console.log("[ERROR] ConfigReader -> readDevTeamsConfigs -> Error = " + err); @@ -618,4 +476,4 @@ } } } -} \ No newline at end of file +} diff --git a/Server/MinioStorage.js b/Server/MinioStorage.js index dea6b3d06..7338e6062 100644 --- a/Server/MinioStorage.js +++ b/Server/MinioStorage.js @@ -10,17 +10,15 @@ } let storageData; - let serverConfig; let Minio = require('minio'); let minioClient; return thisObject; - function initialize(pStorageData, pServerConfig) { + function initialize(pStorageData) { storageData = pStorageData; - serverConfig = pServerConfig; minioClient = new Minio.Client({ endPoint: process.env.MINIO_END_POINT, diff --git a/Server/SessionManager.js b/Server/SessionManager.js index 0f4183b25..cdd124328 100644 --- a/Server/SessionManager.js +++ b/Server/SessionManager.js @@ -9,49 +9,55 @@ return thisObject; - function initialize(pServerConfig, storage, callBackFunction) { + function initialize(ecosystem, storage, callBackFunction) { - readSessions(); + try { + // TODO Sessions need to be handled by the master app - function readSessions() { + const sessions = require('./config/sessions.json'); - try { - - storage.initialize(undefined, pServerConfig); - storage.readData("AdvancedAlgos", "AAPlatform", "open.sessions.json", false, onData); - - function onData(err, pText) { - - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - - console.log("[ERROR] SessionManager -> initialize -> readSessions -> onData -> Could not read a file. "); - console.log("[ERROR] SessionManager -> initialize -> readSessions -> onData -> err.message = " + err.message); - - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - return; - } - - let sessions = JSON.parse(pText); - - for (let i = 0; i < sessions.length; i++) { - - let session = sessions[i]; - - activeSessions.set(session.sessionToken, session); - - } - - callBackFunction(global.DEFAULT_OK_RESPONSE); - - } + for (let i = 0; i < sessions.length; i++) { + let session = sessions[i]; + activeSessions.set(session.sessionToken, session); } - catch (err) { - console.log("[ERROR] readSessions -> err = " + err.message); - console.log("[HINT] You need to have a file at this path -> " + filePath); - callBackFunction(global.DEFAULT_FAIL_RESPONSE); - } + callBackFunction(global.DEFAULT_OK_RESPONSE); + + // storage.initialize(undefined, ecosystem); + // storage.readData("AdvancedAlgos", "AAPlatform", "open.sessions.json", false, onData); + + // function onData(err, pText) { + + // if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + // console.log("[ERROR] SessionManager -> initialize -> readSessions -> onData -> Could not read a file. "); + // console.log("[ERROR] SessionManager -> initialize -> readSessions -> onData -> err.message = " + err.message); + + // callBackFunction(global.DEFAULT_FAIL_RESPONSE); + // return; + // } + + // let sessions = JSON.parse(pText); + + // for (let i = 0; i < sessions.length; i++) { + + // let session = sessions[i]; + + // activeSessions.set(session.sessionToken, session); + + // } + + // callBackFunction(global.DEFAULT_OK_RESPONSE); + + // } } + catch (err) { + console.log("[ERROR] readSessions -> err = " + err.message); + console.log("[HINT] You need to have a file at this path -> " + filePath); + + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + } + } function getSession(pSessionToken) { @@ -59,4 +65,4 @@ return activeSessions.get(pSessionToken); } -} \ No newline at end of file +} diff --git a/index.html b/index.html index 342215de3..4146a22e7 100644 --- a/index.html +++ b/index.html @@ -17,7 +17,6 @@ - \ No newline at end of file + diff --git a/server.js b/server.js index f8c4d9294..43041f6f8 100644 --- a/server.js +++ b/server.js @@ -1,16 +1,12 @@ require('dotenv').config() -/* These 2 are global variables, that is why they do not have a let or var */ - -CONSOLE_LOG = true -CONSOLE_ERROR_LOG = true -LOG_FILE_CONTENT = false +CONSOLE_LOG = process.env.CONSOLE_LOG === "true" +CONSOLE_ERROR_LOG = process.env.CONSOLE_ERROR_LOG === "true" +LOG_FILE_CONTENT = process.env.LOG_FILE_CONTENT === "true" if (CONSOLE_LOG === true) { console.log('[INFO] server -> Node Server Starting.') } -let serverConfig - global.DEFAULT_OK_RESPONSE = { result: 'Ok', message: 'Operation Succeeded' @@ -36,21 +32,13 @@ global.CUSTOM_FAIL_RESPONSE = { message: 'Custom Message' } -let storageData = new Map() - -let botScripts // This module is the one which grabs user bots scrips from the storage, and browserifys them. - -let ecosystem -let ecosystemObject - -// 'use strict'; +let storageData let http = require('http') let port = process.env.PORT || 1337 - let isHttpServerStarted = false - const AZURE_STORAGE = require('./Server/AzureStorage') const MINIO_STORAGE = require('./Server/MinioStorage') +let ecosystemObject = require("./Server/config/ecosystem.json"); switch (process.env.STORAGE_PROVIDER) { case 'Azure': { @@ -67,9 +55,6 @@ switch (process.env.STORAGE_PROVIDER) { } } -let sessionManager // This module have all authenticated active sessions. -let storageAccessManager // This module manages the SAS for user to access the cloud storage from the browser. - initialize() function initialize () { @@ -82,9 +67,9 @@ function initialize () { const CONFIG_READER = require('./Server/ConfigReader') let configReader = CONFIG_READER.newConfigReader() - configReader.initialize(ecosystem, ecosystemObject, storageData, storage, onInitialized) + configReader.initialize(ecosystemObject, storageData, storage, onInitialized) - function onInitialized (err, pServerConfig) { + 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. ') @@ -92,11 +77,9 @@ function initialize () { return } - serverConfig = pServerConfig - configReader.loadConfigs(onConfigsLoaded) - function onConfigsLoaded (err, pEcosystem, pEcosystemObject) { + 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. ') @@ -104,34 +87,11 @@ function initialize () { return } - ecosystem = pEcosystem ecosystemObject = pEcosystemObject - storage.initialize(storageData, serverConfig) - - const SESSION_MANAGER = require('./Server/SessionManager') - sessionManager = SESSION_MANAGER.newSessionManager() - - sessionManager.initialize(serverConfig, storage, onInitialized) - - function onInitialized () { - const STORAGE_ACCESS_MANAGER = require('./Server/AzureStorageAccessManager') - storageAccessManager = STORAGE_ACCESS_MANAGER.newAzureStorageAccessManager() - - storageAccessManager.initialize(serverConfig, onInitialized) - - function onInitialized () { - const BOT_SCRIPTS = require('./Server/BotsScripts') - botScripts = BOT_SCRIPTS.newBotScripts() - - botScripts.initialize(serverConfig, storage, onInitialized) - - function onInitialized () { - startHtttpServer() - } - } - } + storage.initialize(storageData, ecosystemObject) // TODO do not initialize as a general option + startHtttpServer () } } } @@ -153,7 +113,6 @@ function onBrowserRequest (request, response) { if (CONSOLE_LOG === true) { console.log('[INFO] server -> onBrowserRequest -> Entering function.') } if (CONSOLE_LOG === true && request.url.indexOf('NO-LOG') === -1) { console.log('[INFO] server -> onBrowserRequest -> request.url = ' + request.url) } - let htmlResponse let requestParameters = request.url.split('/') if (requestParameters[1].indexOf('index.html') >= 0) { @@ -177,7 +136,7 @@ function onBrowserRequest (request, response) { switch (requestParameters[1]) { - case serverConfig.reinitializeCommand: { + case process.env.REINITIALIZE_COMMAND: { initialize() respondWithContent('Node JS Server Reinitilized.', response) @@ -192,39 +151,39 @@ function onBrowserRequest (request, response) { break; } - case 'FileService': - { + // case 'FileService': + // { - processPost(request, response, onPostReceived) + // processPost(request, response, onPostReceived) - function onPostReceived(pData) { + // function onPostReceived(pData) { - let request = JSON.parse(pData); + // let request = JSON.parse(pData); - storage.readData(undefined, request.path, request.conatinerName, false, onReady) + // storage.readData(undefined, request.path, request.conatinerName, false, onReady) - function onReady(err, pFileContent) { + // function onReady(err, pFileContent) { - let responseToClient = { - err: err, - text: pFileContent - }; + // let responseToClient = { + // err: err, + // text: pFileContent + // }; - respondWithContent(JSON.stringify(responseToClient), response); - } - } + // respondWithContent(JSON.stringify(responseToClient), response); + // } + // } - break; - } + // break; + // } case 'Plotter.js': { - /* + /* - This file is build 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. + 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 { @@ -237,7 +196,7 @@ function onBrowserRequest (request, response) { try { let fileContent = file.toString() - /* This is the string we will use to insert into the Plotters.js script. */ + /* This is the string we will use to insert into the Plotters.js script. */ let caseString = '' + ' case "@newFunctionName@":' + '\n' + @@ -296,13 +255,13 @@ function onBrowserRequest (request, response) { 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. + 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 { @@ -379,12 +338,12 @@ function onBrowserRequest (request, response) { 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. + 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 { @@ -423,7 +382,7 @@ function onBrowserRequest (request, response) { filePath = filePath + '/' + requestParameters[4] } - respondWithFile(serverConfig.pathToCloudAppWrapperComponent + '/' + filePath, response) + respondWithFile(process.env.PATH_TO_CLOUD_APP_WRAPPER_COMPONENT + '/' + filePath, response) } break @@ -457,147 +416,34 @@ function onBrowserRequest (request, response) { case 'CockpitSpace': // This means the CockpitSpace folder. { - respondWithFile(serverConfig.pathToCanvasApp + '/CockpitSpace/' + requestParameters[2], response) + respondWithFile(process.env.PATH_TO_CANVAS_APP + '/CockpitSpace/' + requestParameters[2], response) } break case 'TopSpace': // This means the TopSpace folder. { - respondWithFile(serverConfig.pathToCanvasApp + '/TopSpace/' + requestParameters[2], response) + respondWithFile(process.env.PATH_TO_CANVAS_APP + '/TopSpace/' + requestParameters[2], response) } break case 'StrategySpace': // This means the StrategySpace folder. { - respondWithFile(serverConfig.pathToCanvasApp + '/StrategySpace/' + requestParameters[2], response) + respondWithFile(process.env.PATH_TO_CANVAS_APP + '/StrategySpace/' + requestParameters[2], response) } break case 'ControlsToolBox': // This means the StrategySpace folder. { - respondWithFile(serverConfig.pathToCanvasApp + '/ControlsToolBox/' + requestParameters[2], response) + respondWithFile(process.env.PATH_TO_CANVAS_APP + '/ControlsToolBox/' + requestParameters[2], response) } break case 'Utilities': // This means the StrategySpace folder. { - respondWithFile(serverConfig.pathToCanvasApp + '/Utilities/' + requestParameters[2], response) + respondWithFile(process.env.PATH_TO_CANVAS_APP + '/Utilities/' + requestParameters[2], response) } break - case 'AABrowserAPI': // This means the Scripts folder. - { - switch (requestParameters[2]) { - - case "authenticateUser": { - - const AABROWSER_API_USER_AUTHENTICATION = require('./AABrowserAPI/' + 'UserAuthentication'); - let userAuthentication = AABROWSER_API_USER_AUTHENTICATION.newUserAuthentication(); - - userAuthentication.initialize(sessionManager, storageAccessManager); - userAuthentication.authenticateUser(requestParameters[3], onFinish); - - function onFinish(err, pUserProfile) { - - let responseToBrowser = { - err: err, - userProfile: pUserProfile - }; - - respondWithContent(JSON.stringify(responseToBrowser), response); - } - - break; - } - - case "newTeam": { - - const AABROWSER_API_TEAM_SETUP = require('./AABrowserAPI/' + 'TeamSetup'); - let teamSetup = AABROWSER_API_TEAM_SETUP.newTeamSetup(); - - teamSetup.initialize(serverConfig, storage); - - let devTeamCodeName = decodeURI(requestParameters[3]); - let devTeamDisplayName = decodeURI(requestParameters[4]); - let userName = decodeURI(requestParameters[5]); - let botCodeName = decodeURI(requestParameters[6]); - let botDisplayName = decodeURI(requestParameters[7]); - let userId = decodeURI(requestParameters[8]); - let sharedSecret = decodeURI(requestParameters[9]); - - if (sharedSecret !== serverConfig.newTeamSharedSecret) { - - let customFailResponse = { - result: global.DEFAULT_FAIL_RESPONSE.result, - message: "The shared secret provided is incorrect" - } - - respondWithContent(JSON.stringify(customFailResponse), response); - - if (CONSOLE_LOG === true) { console.log("[WARN] server -> AABrowserAPI -> newTeam -> Shared Secret Received Incorrect."); } - - return; - - } - - teamSetup.newTeam(devTeamCodeName, devTeamDisplayName, userName, botCodeName, botDisplayName, userId, onSetupFinished); - - function onSetupFinished(err) { - - if (err.result === global.DEFAULT_OK_RESPONSE.result) { - - /* We must re-load all the webserver caches.*/ - - initialize(); - } - - respondWithContent(JSON.stringify(err), response) - } - break - } - - case 'deleteTeam': { - const AABROWSER_API_TEAM_SETUP = require('./AABrowserAPI/' + 'TeamSetup') - let teamSetup = AABROWSER_API_TEAM_SETUP.newTeamSetup() - - teamSetup.initialize(serverConfig, storage) - - let devTeamCodeName = decodeURI(requestParameters[3]) - let userName = decodeURI(requestParameters[4]) - let botCodeName = decodeURI(requestParameters[5]) - let userId = decodeURI(requestParameters[6]) - let sharedSecret = decodeURI(requestParameters[7]) - - if (sharedSecret !== serverConfig.deleteTeamSharedSecret) { - let customFailResponse = { - result: global.DEFAULT_FAIL_RESPONSE.result, - message: 'The shared secret provided is incorrect' - } - - respondWithContent(JSON.stringify(customFailResponse), response) - - if (CONSOLE_LOG === true) { console.log('[WARN] server -> AABrowserAPI -> deleteTeam -> Shared Secret Received Incorrect.') } - - return - } - - teamSetup.deleteTeam(devTeamCodeName, userName, botCodeName, userId, onSetupFinished) - - function onSetupFinished (err) { - if (err.result === global.DEFAULT_OK_RESPONSE.result) { - /* We must re-load all the webserver caches. */ - - initialize() - } - - respondWithContent(JSON.stringify(err), response) - } - break - } - } - } - break - case 'Scripts': // This means the Scripts folder. { if (requestParameters[2] === 'AppLoader.js') { @@ -698,89 +544,6 @@ function onBrowserRequest (request, response) { } break - case 'AACloud': // This means the cloud folder. - { - let filePath = requestParameters[2] - - if (requestParameters[3] !== undefined) { - filePath = filePath + '/' + requestParameters[3] - } - - if (requestParameters[4] !== undefined) { - filePath = filePath + '/' + requestParameters[4] - } - - let map = storageData - - let script = map.get(filePath) - - if (script !== undefined) { - respondWithContent(script, response) - } else { - if (CONSOLE_LOG === true) { console.log('[WARN] server -> onBrowserRequest -> readEcosystemConfig -> Script Not Found.') } - if (CONSOLE_LOG === true) { console.log('[WARN] server -> onBrowserRequest -> readEcosystemConfig -> requestParameters[2] = ' + requestParameters[2]) } - - respondWithContent('', response) - } - } - break - - case 'Bots': // This means the cloud folder. - { - switch (requestParameters[3]) { - - case 'bots': { - let devTeam = requestParameters[2] - let source = requestParameters[3] - let repo = requestParameters[4] - let path - - if (requestParameters[6] !== undefined) { - path = requestParameters[5] + '/' + requestParameters[6] - } else { - path = requestParameters[5] - } - - botScripts.getScript(devTeam, source, repo, path, onScriptReady) - - break - } - - case 'members': { - let devTeam = requestParameters[2] - let source = requestParameters[3] + '/' + requestParameters[4] - let repo = requestParameters[5] - let path - - if (requestParameters[7] !== undefined) { - path = requestParameters[6] + '/' + requestParameters[7] - } else { - path = requestParameters[6] - } - - botScripts.getScript(devTeam, source, repo, path, onScriptReady) - - break - } - - default : { - if (CONSOLE_LOG === true) { console.log('[ERROR] server -> onBrowserRequest -> Bots -> Invalid Request. ') } - } - } - - function onScriptReady (err, pScript) { - if (err.result !== global.DEFAULT_OK_RESPONSE.result) { - console.log('[ERROR] server -> onBrowserRequest -> Bots -> onScriptReady -> Could not read a file. ') - console.log('[ERROR] server -> onBrowserRequest -> Bots -> onScriptReady -> err.message = ' + err.message) - - pScript = '/* Your bot source code could not be retrieved. */' - } - - respondWithContent(pScript, response) - } - } - break - case 'Plotters': // This means the plotter folder, not to be confused with the Plotters script! { storage.readData(requestParameters[2] + '/' + 'plotters', requestParameters[3], requestParameters[4], true, onDataArrived) @@ -816,13 +579,13 @@ function onBrowserRequest (request, response) { break case 'Panels': { - respondWithFile(serverConfig.pathToCanvasApp + '/' + requestParameters[1] + '/' + requestParameters[2], response) + respondWithFile(process.env.PATH_TO_CANVAS_APP + '/' + requestParameters[1] + '/' + requestParameters[2], response) } break case 'ChartLayers': { - respondWithFile(serverConfig.pathToCanvasApp + '/' + requestParameters[1] + '/' + requestParameters[2], response) + respondWithFile(process.env.PATH_TO_CANVAS_APP + '/' + requestParameters[1] + '/' + requestParameters[2], response) } break @@ -834,31 +597,31 @@ function onBrowserRequest (request, response) { case 'Spaces': { - respondWithFile(serverConfig.pathToCanvasApp + '/' + requestParameters[1] + '/' + requestParameters[2], response) + respondWithFile(process.env.PATH_TO_CANVAS_APP + '/' + requestParameters[1] + '/' + requestParameters[2], response) } break case 'Scales': { - respondWithFile(serverConfig.pathToCanvasApp + '/' + requestParameters[1] + '/' + requestParameters[2], response) + respondWithFile(process.env.PATH_TO_CANVAS_APP + '/' + requestParameters[1] + '/' + requestParameters[2], response) } break case 'Files': { - respondWithFile(serverConfig.pathToFilesComponent + '/' + requestParameters[2], response) + respondWithFile(process.env.PATH_TO_FILES_COMPONENT + '/' + requestParameters[2], response) } break case 'FloatingSpace': { - respondWithFile(serverConfig.pathToCanvasApp + '/' + requestParameters[1] + '/' + requestParameters[2], response) + respondWithFile(process.env.PATH_TO_CANVAS_APP + '/' + requestParameters[1] + '/' + requestParameters[2], response) } break case 'ChartsSpace': { - respondWithFile(serverConfig.pathToCanvasApp + '/' + requestParameters[1] + '/' + requestParameters[2], response) + respondWithFile(process.env.PATH_TO_CANVAS_APP + '/' + requestParameters[1] + '/' + requestParameters[2], response) } break @@ -896,18 +659,9 @@ function onBrowserRequest (request, response) { console.log(err) } } else { - respondWithFile(serverConfig.pathToCanvasApp + '/' + requestParameters[1], response) + respondWithFile(process.env.PATH_TO_CANVAS_APP + '/' + requestParameters[1], response) } } - - function sendResponseToBrowser (htmlResponse) { - if (CONSOLE_LOG === true) { console.log('[INFO] server -> onBrowserRequest -> sendResponseToBrowser -> Entering function.') } - - response.writeHead(200, { 'Content-Type': 'text/html' }) - response.write(htmlResponse) - - response.end('\n') - } } function respondWithContent (content, response) {