Skip to content

Commit

Permalink
Merge pull request #3 from aaferna/1.1.2
Browse files Browse the repository at this point in the history
1.1.2
  • Loading branch information
aaferna authored Dec 2, 2022
2 parents c3f07fa + eba10c3 commit 9d3a085
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 22 deletions.
4 changes: 1 addition & 3 deletions components/status.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
7 changes: 7 additions & 0 deletions core/modules/express.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
exports.jsonErrorHandler = async (err, req, res, next) => {
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})
}

68 changes: 51 additions & 17 deletions core/server.js
Original file line number Diff line number Diff line change
@@ -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/"),
Expand All @@ -16,29 +15,64 @@ const express = require("express"),
functions[r.replace(".js", "")] = require(dirFunc + "/" + r);
})


let dirApis = require("path").join(__dirname, "../components/"),
apiFiles = fs.readdirSync(dirApis);
apiFiles.forEach(r =>{
exsrv.use(require(dirApis + "/" + 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.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);

methodstoShow.push({
method: method,
uri: route.path

})

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 `)
})
}
});
}
}
}

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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9d3a085

Please sign in to comment.