Skip to content

Commit

Permalink
Mejoras importantes
Browse files Browse the repository at this point in the history
  • Loading branch information
aaferna committed Jan 2, 2024
1 parent b034fe9 commit c37a3df
Show file tree
Hide file tree
Showing 16 changed files with 214 additions and 490 deletions.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PORT=1900
TIMEZONE=America/Argentina/Buenos_Aires
ENABLE_CORS=false
ENABLE_HELMET=false
GCHAT_ENDPOINT=https://chat.googleapis.com/v1/spaces/XXXXX
TRACE=true
PROCESS_HEADER=true
4 changes: 2 additions & 2 deletions core/expressHandle.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ exports.trx = req => {
};

exports.jsonErrorHandler = async (err, req, res, next) => {
const { id, ip, uri, method } = this.reqInfo(req);
const { id, ip, uri, method } = this.trx(req);

log(
'error',
Expand All @@ -25,7 +25,7 @@ exports.jsonErrorHandler = async (err, req, res, next) => {
};

exports.notFoundHandler = (req, res, next) => {
const { id, ip, uri, method } = this.reqInfo(req);
const { id, ip, uri, method } = this.trx(req);

log(
'warn',
Expand Down
8 changes: 6 additions & 2 deletions core/log4j.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const log4js = require('log4js');

exports.log = (tipo, data, appenderName = 'Integrador') => {
const isDevMode = process.argv.slice(2)[0] === 'dev'; // Lee la variable de entorno DEVMODE
const isDevMode = process.env.NODE_ENV === 'dev'; // Lee la variable de entorno DEVMODE

const appenders = {
[appenderName]: {
Expand All @@ -18,6 +18,8 @@ exports.log = (tipo, data, appenderName = 'Integrador') => {
if (isDevMode) appenders.consoleAppender = { type: 'console' };

log4js.configure({
pm2: true,
pm2InstanceVar: 'INSTANCE_ID',
appenders,
categories: {
default: {
Expand All @@ -33,6 +35,8 @@ exports.log = (tipo, data, appenderName = 'Integrador') => {

if (tipo == 'debug') {
logger.debug(data);
} else if (tipo == 'trace') {
logger.trace(data);
} else if (tipo == 'error') {
logger.error(data);
} else if (tipo == 'warn') {
Expand All @@ -42,6 +46,6 @@ exports.log = (tipo, data, appenderName = 'Integrador') => {
}
};

exports.debug = data => {
exports.ldebug = data => {
this.log('debug', data);
};
47 changes: 0 additions & 47 deletions core/sql.js

This file was deleted.

3 changes: 3 additions & 0 deletions cors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"origin": "*"
}
Binary file modified db/.DS_Store
Binary file not shown.
Empty file added db/.gitkeep
Empty file.
19 changes: 0 additions & 19 deletions db/datastores.json

This file was deleted.

20 changes: 20 additions & 0 deletions ecosystem.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = {
apps: [
{
name: 'monoIntegrator',
namespace: 'Integrator',
script: 'index.js',
instances: '2',
instance_var: 'INSTANCE_ID',
exec_mode: 'cluster',
autorestart: true,
watch: false,
max_memory_restart: '1G',
restart_delay: 3000,
env: {
NODE_ENV: 'prd',
PORT: 1900
},
},
],
};
Empty file added functions/.gitkeep
Empty file.
4 changes: 4 additions & 0 deletions helmet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"contentSecurityPolicy": false,
"xDownloadOptions": false
}
100 changes: 36 additions & 64 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
require('dotenv').config();
const dirFunc = './functions/', port = parseInt(process.env.PORT) || 1900;

log = require('./core/log4j').log;
global.debug = require('./core/log4j').debug;
global.port = parseInt(process.env.PORT) || 3000;
global.log = require('./core/log4j').log;
global.logd = require('./core/log4j').ldebug;
global.fun = {};
global.fs = require('fs');
global.path = require('path');
global.dirRout = './routes/';
global.dirFunc = './functions/';
global.codePath = __dirname;
global.fun =[];


const numCPUs = require('os').cpus().length
const cluster = require('cluster');
if (process.argv[2] === 'dev') {
process.env.NODE_ENV = process.argv[2]
log('warn', `monoIntegrator iniciado en modo Desarrollador`);
} else {
log('info', `monoIntegrator iniciado en modo Productivo`);
}

global.walkSync = (dir, filelist = []) => {
fs.readdirSync(dir).forEach(file => {
Expand All @@ -24,68 +26,38 @@ global.walkSync = (dir, filelist = []) => {
};

try {
const functionFiles = walkSync('./functions/');
functionFiles.forEach(file => {
const files = walkSync(dirFunc);
files.forEach(file => {
if (file.endsWith('.js')) {
try {
const functionName = file.split('/').pop().replace('.js', '');
global.fun[functionName] = require(`./${file}`);

const functionName = file.split('/');
const nombreFuncion = functionName[functionName.length - 1].replace('.js', '');
fun[nombreFuncion] = require(`./${file}`);

if (process.env.TRACE === 'true' || process.env.NODE_ENV == 'prd') {
log("info", `Función ${nombreFuncion} detectada`);
}

} catch (error) {
log('error', `Existe un inconveniente al importar la función ubicada en ${file}: ${error}`);
log(
'error',
`Existe un inconveniente al importar la funcion ubicada en ${file}: ${error}`,
);
}
}
});
} catch (error) {
log('error', `Existe un inconveniente al recorrer los archivos en Funciones: ${error}`);
log(
'error',
`Existe un inconveniente al recorrer los archivos en Funciones: ${error}`,
);
}

if (process.argv.slice(2)[0] === 'dev') {
log('info', `monoIntegrator iniciado en modo Desarrollador`);

try {
const functionFiles = fs.readdirSync(dirFunc);
functionFiles.forEach(filename => {
log("info", `Funcion ${filename.replace(".js", "")} detectada `)
});
} catch (error) {
log('warn', `Existió un inconveniente al validar las funciones :: ${error}`);
process.exit();
}

try {
const routeFiles = global.walkSync(global.dirRout);
if (routeFiles.length === 0) {
log('info', `No hay rutas para inicializar`);
}
routeFiles.forEach(file => {
if (file.endsWith('.js')) {
// log("info", `Ruta ${file} detectada `)
}
});
} catch (error) {
log('error', `Existe un inconveniente al validar las rutas :: ${error}`);
}

const appServer = require('./server');
appServer.listen(global.port, () => {
log('info', `Servidor iniciado en el puerto ${global.port} bajo PID ${process.pid}`);
});
} else {
if (cluster.isMaster) {
log('info', `Master se inició bajo PID ${process.pid}`);

for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}

cluster.on('exit', (worker, code, signal) => {
log('info', `El Proceso ${worker.process.pid} se cerró bajo el código ${code} y con señal ${signal}`);
});
} else {
global.port = global.port + cluster.worker.id;
const appServer = require('./server');
appServer.listen(global.port, () => {
log('info', `Servidor iniciado en el puerto ${global.port} bajo PID ${process.pid}`);
});
}
}
const appserver = require('./server');
appserver.listen(port, () => {
log(
'info',
`Servidor iniciado en el puerto ${port} bajo PID ${process.pid}`,
);
});
Loading

0 comments on commit c37a3df

Please sign in to comment.