diff --git a/swarmlab-app/src/.env b/swarmlab-app/src/.env new file mode 100644 index 00000000..15c31032 --- /dev/null +++ b/swarmlab-app/src/.env @@ -0,0 +1,17 @@ +NODE_ENV=development + +#MONGO +URL=mongodb://mongo:27017/ +USER= +PASS= +DATABASE=fluentdb +COLLECTION=test + +#REDIS + +REDIS=redisserver +REDIS_PORT=6379 + +#SERVER +PORT=3000 + diff --git a/swarmlab-app/src/package.json b/swarmlab-app/src/package.json index ea9f3ee8..75a6196c 100644 --- a/swarmlab-app/src/package.json +++ b/swarmlab-app/src/package.json @@ -13,6 +13,7 @@ "chai": "^4.2.0", "cors": "^2.8.5", "dirty-chai": "^2.0.1", + "dotenv": "^10.0.0", "express": "^4.17.1", "express-validator": "^6.6.1", "helmet": "^4.1.1", diff --git a/swarmlab-app/src/run/app.js b/swarmlab-app/src/run/app.js index 6ffaea9d..609efa29 100644 --- a/swarmlab-app/src/run/app.js +++ b/swarmlab-app/src/run/app.js @@ -4,7 +4,11 @@ var pathmodule = require("path"); var app = require("express")(); var http = require("http").Server(app); var https = require("https"); -var CONFIG = require(pathmodule.resolve(__dirname, "runconfig.js")); +// var CONFIG = require(pathmodule.resolve(__dirname, "runconfig.js")); +const dotenv = require("dotenv"); +const check123 = dotenv.config(); +const { DATABASE, COLLECTION, URL, REDIS, REDIS_PORT, PORT } = process.env; + const io = require("socket.io")(http, { // pingTimeout: 30000, // allowUpgrades: false, @@ -29,9 +33,10 @@ const createAdapter = require("socket.io-redis"); const Redis = require("ioredis"); +// console.log(REDIS, REDIS_PORT, DATABASE, COLLECTION, URL); const pubClient = new Redis({ - host: "redisserver", - port: 6379, + host: REDIS, + port: REDIS_PORT, }); //const pubClient = new RedisClient({ host: 'localhost', port: 6379 }); @@ -98,15 +103,15 @@ const corsOptions = { // Lefos - get length of logs app.get("/length", cors(corsOptions), (req, res) => { console.error("getting length of logs"); - var url = "mongodb://mongo:27017/"; + var url = URL; MongoClient.connect( url, { useNewUrlParser: true, useUnifiedTopology: true }, function (err, db) { if (err) throw err; - var dbo = db.db("fluentdb"); + var dbo = db.db(DATABASE); dbo - .collection("test") + .collection(COLLECTION) .find({}) .toArray(function (err, result) { if (err) throw err; @@ -211,16 +216,16 @@ function IsJsonString(str) { app.get("/services", cors(corsOptions), (req, res) => { console.error("getting length of logs"); - var url = "mongodb://mongo:27017/"; + var url = URL; MongoClient.connect( url, { useNewUrlParser: true, useUnifiedTopology: true }, function (err, db) { if (err) throw err; - var dbo = db.db("fluentdb"); + var dbo = db.db(DATABASE); dbo - .collection("test") + .collection(COLLECTION) .find({}) .toArray(function (err, result) { if (err) throw err; @@ -278,16 +283,16 @@ app.get("/services", cors(corsOptions), (req, res) => { app.get("/test", cors(corsOptions), (req, res) => { console.log("reading from db...."); - var url = "mongodb://mongo:27017/"; + var url = URL; MongoClient.connect( url, { useNewUrlParser: true, useUnifiedTopology: true }, function (err, db) { if (err) throw err; - var dbo = db.db("fluentdb"); + var dbo = db.db(DATABASE); dbo - .collection("test") + .collection(COLLECTION) .find({}) .toArray(function (err, result) { if (err) throw err; @@ -323,16 +328,16 @@ app.get("/test", cors(corsOptions), (req, res) => { app.get("/raw", cors(corsOptions), (req, res) => { console.log("reading from db...."); - var url = "mongodb://mongo:27017/"; + var url = URL; MongoClient.connect( url, { useNewUrlParser: true, useUnifiedTopology: true }, function (err, db) { if (err) throw err; - var dbo = db.db("fluentdb"); + var dbo = db.db(DATABASE); dbo - .collection("test") + .collection(COLLECTION) .find({}) .toArray(function (err, result) { if (err) throw err; @@ -373,16 +378,16 @@ app.get("/test2", cors(corsOptions), (req, res) => { console.error("EXTRA PARAMS: " + logtext); //console.log("TEST LOG"); - var url = "mongodb://mongo:27017/"; + var url = URL; var jsonfinal = []; MongoClient.connect( url, { useNewUrlParser: true, useUnifiedTopology: true }, function (err, db) { if (err) throw err; - var dbo = db.db("fluentdb"); + var dbo = db.db(DATABASE); dbo - .collection("test") + .collection(COLLECTION) .find({}) .toArray(function (err, result) { if (err) throw err; @@ -768,194 +773,6 @@ function convertDateToUTC(date) { ); } -// *************************************************** -// get pipelines -// *************************************************** - -async function getpipelines(token, pipelinename) { - const agent = new https.Agent({ - rejectUnauthorized: false, - }); - const instance = axios.create({ - baseURL: "https://api.swarmlab.io", - withCredentials: true, - rejectUnauthorized: false, - crossdomain: true, - httpsAgent: agent, - headers: { - Accept: "application/json", - "Content-Type": "multipart/form-data", - Authorization: "Bearer " + token, - }, - }); - /* - var params = { - playbook: value - } - var options = { - params: params, - headers: { 'content-type': 'application/x-www-form-urlencoded',Authorization: `Bearer ${token}` }, - }; - - const playbook = await api.GET('playbookCode',options); - return playbook -*/ - try { - var pipelines = { - querytokenFilter: CONFIG.api.token, - filter: pipelinename, - }; - //var params = { - // pipeline: pipelines - // } - var params = { - querytokenFilter: CONFIG.api.token, - filter: pipelinename, - }; - - var options = { - params: params, - headers: { - "content-type": "application/x-www-form-urlencoded", - Authorization: `Bearer ${token}`, - }, - }; - - //https://api.swarmlab.io/gettutorlabrooms?sort=pipelinename%7Casc&page=1&per_page=5&filter=&type=scripts&tutor=yes - instance.defaults.timeout = 30000; - //const res = await instance.get('/getplaygrounds',params,options); - const res = await instance.get("/getplaygrounds", options); - if (res.status == 200) { - return res.data; - } else { - console.log("noerror: " + res); - return await res.status; - } - } catch (err) { - console.error("error: " + err); - var error = new Object(); - error.action = "401"; - return await error; - } -} - -// *************************************************** -// get user pipelines -// *************************************************** - -async function getuserpipelines(token, user, swarmlabname) { - var pipelinename = user; - const agent = new https.Agent({ - rejectUnauthorized: false, - }); - const instance = axios.create({ - baseURL: "https://api.swarmlab.io", - withCredentials: true, - rejectUnauthorized: false, - crossdomain: true, - httpsAgent: agent, - headers: { - Accept: "application/json", - "Content-Type": "multipart/form-data", - Authorization: "Bearer " + token, - }, - }); - try { - var pipelines = { - querytokenFilter: CONFIG.api.token, - filter: pipelinename, - swarmlabname: swarmlabname, - }; - //var params = { - // pipeline: pipelines - // } - var params = { - querytokenFilter: CONFIG.api.token, - filter: pipelinename, - swarmlabname: swarmlabname, - }; - - var options = { - params: params, - headers: { - "content-type": "application/x-www-form-urlencoded", - Authorization: `Bearer ${token}`, - }, - }; - - instance.defaults.timeout = 30000; - const res = await instance.get("/getuserplaygrounds", options); - if (res.status == 200) { - return res.data; - } else { - console.log("noerror: " + res); - return await res.status; - } - } catch (err) { - console.error("error: " + err); - var error = new Object(); - error.action = "401"; - error.error = err; - return await error; - } -} - -global.online = "ob"; -global.pipelines = []; - -function sendlog(reslog, pathfileval) { - var usertmp = global.pipelines.find((x) => x.pathlogfile == pathfileval); - //for (var key in usertmp.data){ - var user = usertmp.data[0].user25user; - // if(usertmp.data){ - console.log("-----------------------" + JSON.stringify(usertmp)); - io.in(user).emit("logdata", reslog); - // } - //} -} - -// function onlogfile(path) { -// console.log("File", path, "has been added"); -// var pathfileval = pathmodule.basename(path); -// var arrfile = pathfileval.toString().split("-"); -// var pathfile = arrfile[0]; -// var indexfind1 = global.pipelines.findIndex( -// (x) => x.pathlogfile == pathfileval -// ); -// console.log( -// "file11111111111111111111111111111111 " + JSON.stringify(pathfileval) -// ); -// if (indexfind1 === -1) { -// (async () => { -// console.log( -// "file2222222222222222222222222222222222222 " + -// JSON.stringify(pathfileval) -// ); -// var token = "d2539e5a7ae1f9f1b0eb2b8f22ca467a86d28407"; // desto -// var resdata = await getpipelines(token, pathfile); -// //resdata.data.pathlogfile = 'test' -// var resob = {}; -// resob.pathlogfile = pathfileval; -// var resobarray = []; -// for (let i in resdata.data) { -// var resob1 = {}; -// resob1.data = resdata.data[i].res25swarmlabname; -// resob1.user25user = resdata.data[i].res25user; -// resob1.res25creator = resdata.data[i].res25creator; -// resob1.res25fileforce = resdata.data[i].res25fileforce; -// resobarray.push(resob1); -// } -// resob.data = resobarray; -// var indexfind = global.pipelines.findIndex( -// (x) => x.pathlogfile == pathfileval -// ); -// indexfind === -1 -// ? global.pipelines.push(resob) -// : console.log("object already exists " + pathfileval); -// })(); -// } -// } - // Έλεγχος αν υπάρχει ήδη ενεργό stream με τη βάση δεδομένων για κάποιον async function checkstream(data) { var res = await getkey(data.id); @@ -964,55 +781,19 @@ async function checkstream(data) { } else { console.log("Creating Stream...."); - var url = "mongodb://mongo:27017/"; + var url = URL; MongoClient.connect( url, { useNewUrlParser: true, useUnifiedTopology: true }, function (err, db) { if (err) throw err; - var dbo = db.db("fluentdb"); - dbo.collection("test", onCollectionNew.bind(data)); + var dbo = db.db(DATABASE); + dbo.collection(COLLECTION, onCollectionNew.bind(data)); } ); } } -function sleep(ms) { - return new Promise((resolve) => setTimeout(resolve, ms)); -} - -// δεν την χρησιμοποιώ κάπου -function getSHA256ofJSON(data, inputEncoding, encoding) { - if (!data) { - return ""; - } - inputEncoding = inputEncoding || "utf-8"; - encoding = encoding || "hex"; - const hash = require("crypto").createHash("md5"); - return hash.update(JSON.stringify(data), inputEncoding).digest(encoding); -} - -// async function getUser(token) { -// return new Promise((resolve) => { -// pubClient.get(token, function (err, reply) { -// if (err) { -// console.log("----------error------------"); - -// resolve(null); -// } else { -// if (reply) { -// console.log("---------found----------"); -// resolve(1); -// } else { -// console.log("----------not found------------"); -// resolve(2); -// //return 2 -// } -// } -// }); -// }); -// } - // Διαδικασία ελέγχου στοιχείων στο redis για την εξακρίβωση ύπαρξης stream με τη βάση async function getkey(id) { return new Promise((resolve) => {