From 9697eb5a903187aff7c65957f047bd3a13dec6d1 Mon Sep 17 00:00:00 2001 From: Agustin Fernandez Date: Wed, 30 Nov 2022 22:21:59 -0300 Subject: [PATCH 1/5] 1.1.1 Optimizacion de Codigo --- components/status.js | 4 +--- core/modules/express.js | 5 +++++ core/server.js | 20 +++++++------------- 3 files changed, 13 insertions(+), 16 deletions(-) create mode 100644 core/modules/express.js diff --git a/components/status.js b/components/status.js index 78a860c..b1c79ca 100644 --- a/components/status.js +++ b/components/status.js @@ -1,10 +1,8 @@ -const express = require('express'), - router = express.Router() - router.get('/v1/manager/status', (req, res) => { // * Ejemplo de Conx. a MySQL + // let conn = db.sql("test", "SELECT * FROM users") // conn.then(data =>{ // functions["alert"].conlog(data) diff --git a/core/modules/express.js b/core/modules/express.js new file mode 100644 index 0000000..47cd0bc --- /dev/null +++ b/core/modules/express.js @@ -0,0 +1,5 @@ +exports.jsonErrorHandler = async (err, req, res, next) => { + log4j.log("warn", `IP Origen: ${req.headers['x-forwarded-for'] || req.socket.remoteAddress }, Endpoint: ${req.originalUrl} | Se enviaron datos que no estan formateados en JSON`) + res.json({ msg : "Se enviaron datos que no estan formateados en JSON" }); +} + diff --git a/core/server.js b/core/server.js index 6f596d3..79d604c 100644 --- a/core/server.js +++ b/core/server.js @@ -1,13 +1,12 @@ -const express = require("express"), - exsrv = express(), +global.express = require("express") +global.router = express.Router() + +const exsrv = express(), helmet = require('helmet'), fs = require('fs'), cors = require('cors'), - jsonErrorHandler = async (err, req, res, next) => { - log4j.log("warn", `IP Origen: ${req.headers['x-forwarded-for'] || req.socket.remoteAddress }, Endpoint: ${req.originalUrl} | Se enviaron datos que no estan formateados en JSON`) - res.json({ msg : "Se enviaron datos que no estan formateados en JSON" }); - } - + { jsonErrorHandler } = require("../core/modules/express") + global.functions = [] let dirFunc = require("path").join(__dirname, "../functions/"), @@ -16,22 +15,17 @@ const express = require("express"), functions[r.replace(".js", "")] = require(dirFunc + "/" + r); }) - - exsrv.use(express.json()) - exsrv.use(helmet()) exsrv.use(jsonErrorHandler) + exsrv.use(helmet()) exsrv.use(cors({ origin: '*' })); - let dirApis = require("path").join(__dirname, "../components/"), apiFiles = fs.readdirSync(dirApis); apiFiles.forEach(r =>{ exsrv.use(require(dirApis + "/" + r)); }) - - exsrv.all('*', (req, res, next) => { log4j.log("warn", `IP Origen: ${req.headers['x-forwarded-for'] || req.socket.remoteAddress }, Endpoint: ${req.originalUrl} | No se puede resolver el metodo `) From ac608721b2bb791fbd6696b2c444461ed3ae4735 Mon Sep 17 00:00:00 2001 From: Agustin Fernandez Date: Thu, 1 Dec 2022 19:20:28 -0300 Subject: [PATCH 2/5] 1.1.2 Listado de Metodos --- core/server.js | 56 ++++++++++++++++++++++++++++++++++++++++------- package-lock.json | 4 ++-- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/core/server.js b/core/server.js index 79d604c..9bdfe4e 100644 --- a/core/server.js +++ b/core/server.js @@ -15,24 +15,64 @@ const exsrv = express(), functions[r.replace(".js", "")] = require(dirFunc + "/" + r); }) - exsrv.use(express.json()) - exsrv.use(jsonErrorHandler) - exsrv.use(helmet()) - exsrv.use(cors({ origin: '*' })); - let dirApis = require("path").join(__dirname, "../components/"), apiFiles = fs.readdirSync(dirApis); apiFiles.forEach(r =>{ exsrv.use(require(dirApis + "/" + r)); + }) - exsrv.all('*', (req, res, next) => { + exsrv.use(express.json()) + exsrv.use(jsonErrorHandler) + exsrv.use(helmet()) + exsrv.use(cors({ origin: '*' })); + + + exsrv.get('/methods', (req, res) => { - log4j.log("warn", `IP Origen: ${req.headers['x-forwarded-for'] || req.socket.remoteAddress }, Endpoint: ${req.originalUrl} | No se puede resolver el metodo `) + let methodstoShow = [] + + function space(x) { + var res = ''; + while(x--) res += ' '; + return res; + } + + function listRoutes(){ + for (var i = 0; i < arguments.length; i++) { + if(arguments[i].stack instanceof Array){ + arguments[i].stack.forEach(function(a){ + var route = a.route; + if(route){ + route.stack.forEach(function(r){ + var method = r.method.toUpperCase(); + // console.log(method,space(8 - method.length),route.path); + + methodstoShow.push({ + method: method, + uri: route.path + + }) + + + }) + } + }); + } + } + } + + listRoutes(router) + + res.status(400).json({ routes: methodstoShow }) + + }) + + exsrv.all('*', (req, res, next) => { + log4j.log("warn", `IP ${req.headers['x-forwarded-for'] || req.socket.remoteAddress } :: Method ${ req.method } :: Endpoint ${req.originalUrl} :: No se puede resolver el metodo `) res.status(404).json({ msg: `No se puede resolver el metodo ${req.originalUrl}` }); - }); module.exports = exsrv \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index eb34b85..38d0613 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "monoIntegrator", - "version": "1.0.0", + "version": "1.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "monoIntegrator", - "version": "1.0.0", + "version": "1.1.0", "license": "Apache-2.0", "dependencies": { "cors": "^2.8.5", From 372f160975108bd2148312a401978c5d1d4a9e27 Mon Sep 17 00:00:00 2001 From: Agustin Fernandez Date: Thu, 1 Dec 2022 19:33:07 -0300 Subject: [PATCH 3/5] 1.1.2 Se mejora el mensaje de fail Req --- core/modules/express.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/modules/express.js b/core/modules/express.js index 47cd0bc..81ddbdc 100644 --- a/core/modules/express.js +++ b/core/modules/express.js @@ -1,5 +1,7 @@ exports.jsonErrorHandler = async (err, req, res, next) => { - log4j.log("warn", `IP Origen: ${req.headers['x-forwarded-for'] || req.socket.remoteAddress }, Endpoint: ${req.originalUrl} | Se enviaron datos que no estan formateados en JSON`) - res.json({ msg : "Se enviaron datos que no estan formateados en JSON" }); + const { randomUUID } = require('crypto'), + idreq = randomUUID() + log4j.log("warn", `IP ${req.headers['x-forwarded-for'] || req.socket.remoteAddress } :: Method ${ req.method } :: Endpoint ${req.originalUrl} :: IDREQ ${idreq} :: Se enviaron datos que no estan formateados en JSON`) + res.status(400).json({ msg: "Existe un inconveniente en la solicitud", idreq: idreq}) } From ca630960514788f735e2dd4bd4b1064b82d2ccc6 Mon Sep 17 00:00:00 2001 From: Agustin Fernandez Date: Fri, 2 Dec 2022 00:13:42 -0300 Subject: [PATCH 4/5] 1.1.2 Se habilita o no ver todos los metodos --- config.json | 5 ++-- core/server.js | 71 +++++++++++++++++++++++++------------------------- 2 files changed, 39 insertions(+), 37 deletions(-) diff --git a/config.json b/config.json index 4bfdd2d..80e5dfc 100644 --- a/config.json +++ b/config.json @@ -1,15 +1,16 @@ { "port": 1320, + "methods": false, "datastore":[ { - "name": "test", + "name": "manager", "conn":{ "server": "localhost", "user": "root", "password": "", "port": 3306, "connectionreintent": 5, - "database": "TEST" + "database": "metronica_manager" } } ] diff --git a/core/server.js b/core/server.js index 9bdfe4e..a8a20e9 100644 --- a/core/server.js +++ b/core/server.js @@ -7,6 +7,11 @@ const exsrv = express(), cors = require('cors'), { jsonErrorHandler } = require("../core/modules/express") + exsrv.use(express.json()) + exsrv.use(jsonErrorHandler) + exsrv.use(helmet()) + exsrv.use(cors({ origin: '*' })); + global.functions = [] let dirFunc = require("path").join(__dirname, "../functions/"), @@ -19,54 +24,50 @@ const exsrv = express(), apiFiles = fs.readdirSync(dirApis); apiFiles.forEach(r =>{ exsrv.use(require(dirApis + "/" + r)); - }) - exsrv.use(express.json()) - exsrv.use(jsonErrorHandler) - exsrv.use(helmet()) - exsrv.use(cors({ origin: '*' })); + if(config){ + exsrv.get('/methods', (req, res) => { - exsrv.get('/methods', (req, res) => { + let methodstoShow = [] - let methodstoShow = [] - - function space(x) { - var res = ''; - while(x--) res += ' '; - return res; - } - - function listRoutes(){ - for (var i = 0; i < arguments.length; i++) { - if(arguments[i].stack instanceof Array){ - arguments[i].stack.forEach(function(a){ - var route = a.route; - if(route){ - route.stack.forEach(function(r){ - var method = r.method.toUpperCase(); - // console.log(method,space(8 - method.length),route.path); + function space(x) { + var res = ''; + while(x--) res += ' '; + return res; + } + + function listRoutes(){ + for (var i = 0; i < arguments.length; i++) { + if(arguments[i].stack instanceof Array){ + arguments[i].stack.forEach(function(a){ + var route = a.route; + if(route){ + route.stack.forEach(function(r){ + var method = r.method.toUpperCase(); + // console.log(method,space(8 - method.length),route.path); - methodstoShow.push({ - method: method, - uri: route.path + methodstoShow.push({ + method: method, + uri: route.path - }) + }) - }) - } - }); + }) + } + }); + } } } - } - - listRoutes(router) + + listRoutes(router) - res.status(400).json({ routes: methodstoShow }) + res.status(400).json({ routes: methodstoShow }) - }) + }) + } exsrv.all('*', (req, res, next) => { log4j.log("warn", `IP ${req.headers['x-forwarded-for'] || req.socket.remoteAddress } :: Method ${ req.method } :: Endpoint ${req.originalUrl} :: No se puede resolver el metodo `) From eba10c3d905888e272b2218a1d05d86b12225c78 Mon Sep 17 00:00:00 2001 From: Agustin Fernandez Date: Fri, 2 Dec 2022 00:20:39 -0300 Subject: [PATCH 5/5] Revert "1.1.2" This reverts commit ca630960514788f735e2dd4bd4b1064b82d2ccc6. --- config.json | 5 ++-- core/server.js | 71 +++++++++++++++++++++++++------------------------- 2 files changed, 37 insertions(+), 39 deletions(-) diff --git a/config.json b/config.json index 80e5dfc..4bfdd2d 100644 --- a/config.json +++ b/config.json @@ -1,16 +1,15 @@ { "port": 1320, - "methods": false, "datastore":[ { - "name": "manager", + "name": "test", "conn":{ "server": "localhost", "user": "root", "password": "", "port": 3306, "connectionreintent": 5, - "database": "metronica_manager" + "database": "TEST" } } ] diff --git a/core/server.js b/core/server.js index a8a20e9..9bdfe4e 100644 --- a/core/server.js +++ b/core/server.js @@ -7,11 +7,6 @@ const exsrv = express(), cors = require('cors'), { jsonErrorHandler } = require("../core/modules/express") - exsrv.use(express.json()) - exsrv.use(jsonErrorHandler) - exsrv.use(helmet()) - exsrv.use(cors({ origin: '*' })); - global.functions = [] let dirFunc = require("path").join(__dirname, "../functions/"), @@ -24,50 +19,54 @@ const exsrv = express(), apiFiles = fs.readdirSync(dirApis); apiFiles.forEach(r =>{ exsrv.use(require(dirApis + "/" + r)); + }) + exsrv.use(express.json()) + exsrv.use(jsonErrorHandler) + exsrv.use(helmet()) + exsrv.use(cors({ origin: '*' })); - if(config){ - exsrv.get('/methods', (req, res) => { - - let methodstoShow = [] - function space(x) { - var res = ''; - while(x--) res += ' '; - return res; - } - - function listRoutes(){ - for (var i = 0; i < arguments.length; i++) { - if(arguments[i].stack instanceof Array){ - arguments[i].stack.forEach(function(a){ - var route = a.route; - if(route){ - route.stack.forEach(function(r){ - var method = r.method.toUpperCase(); - // console.log(method,space(8 - method.length),route.path); + exsrv.get('/methods', (req, res) => { - methodstoShow.push({ - method: method, - uri: route.path + let methodstoShow = [] - }) + function space(x) { + var res = ''; + while(x--) res += ' '; + return res; + } + + function listRoutes(){ + for (var i = 0; i < arguments.length; i++) { + if(arguments[i].stack instanceof Array){ + arguments[i].stack.forEach(function(a){ + var route = a.route; + if(route){ + route.stack.forEach(function(r){ + var method = r.method.toUpperCase(); + // console.log(method,space(8 - method.length),route.path); + methodstoShow.push({ + method: method, + uri: route.path }) - } - }); - } + + + }) + } + }); } } - - listRoutes(router) + } + + listRoutes(router) - res.status(400).json({ routes: methodstoShow }) + res.status(400).json({ routes: methodstoShow }) - }) - } + }) exsrv.all('*', (req, res, next) => { log4j.log("warn", `IP ${req.headers['x-forwarded-for'] || req.socket.remoteAddress } :: Method ${ req.method } :: Endpoint ${req.originalUrl} :: No se puede resolver el metodo `)