Skip to content

Commit

Permalink
Commit inicial
Browse files Browse the repository at this point in the history
  • Loading branch information
dnievas04 committed Mar 20, 2020
0 parents commit 92d5bd8
Show file tree
Hide file tree
Showing 10 changed files with 1,577 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Archivos de configuración
.env

# Logs
logs
**/*.log
**/*.log*


# Dependency directories
node_modules
typings
typings/*
.vscode
33 changes: 33 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function env(key, _default, type = 's') {
if (!!process.env[key] === false) {
return _default;
}
const value = process.env[key];
switch (type) {
case 'b':
return value.toLowerCase() === 'true';
case 'n':
return parseInt(value, 10);
default:
return value;
}
}


module.exports = {
api: {
url: env('API_URL', 'http://localhost:3004/api/'),
},
db: {
sqlserver: {
server: env('SQLSERVER_SERVER', '172.16.1.79'),
database: env('SQLSERVER_DB', 'Hospital'),
user: env('SQLSERVER_USER', 'danievas'),
password: env('SQLSERVER_PASS', 'PeThiSha'),
},
mongo: {
url: env('MONGO_HOST', 'mongodb://localhost:27017'),
database: env('MONGO_DB', 'rrhh_testing')
}
}
}
71 changes: 71 additions & 0 deletions db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const config = require('./config');
const logger = require('./logger').logger;
const sqlClient = require('mssql');
const mongoose = require('mongoose');


// SQLServer Config
const sqlConfig = {
user: config.db.sqlserver.user,
password: config.db.sqlserver.password,
server: config.db.sqlserver.server,
database: config.db.sqlserver.database,
parseJSON: true,
requestTimeout:60000,
connectionTimeout:60000,
pool: {
max: 10,
min: 0,
idleTimeoutMillis: 30000
}
}

const sqlServerPool = new sqlClient.ConnectionPool(sqlConfig);

sqlServerPool.on('error', err => {
logger.error('SQLServer Error' + err);
})


//MongoDB Config
const mongoConfig = {
url: config.db.mongo.url,
database: config.db.mongo.database
}

mongoose.Promise = Promise

mongoose.connection.on('connected', () => {
logger.debug('MongoDB Connection Established')
})

mongoose.connection.on('reconnected', () => {
logger.debug('MongoDB Connection Reestablished')
})

mongoose.connection.on('disconnected', () => {
logger.debug('MongoDB Connection Disconnected')
})

mongoose.connection.on('close', () => {
logger.debug('MongoDB Connection Closed')
})

mongoose.connection.on('error', (error) => {
logger.error('MongoDB ERROR: ' + error)
})

async function connectMongoDB(){
await mongoose.connect(mongoConfig.url + '/' + mongoConfig.database, {
useNewUrlParser: true,
autoReconnect: true,
reconnectTries: 1000000,
reconnectInterval: 3000
})
}

module.exports = {
sqlServer : sqlServerPool,
connectMongoDB: connectMongoDB
}

45 changes: 45 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const db = require('./db');
const poll = require('./poll');

const sqlServerPool = db.sqlServerPool;

async function sync() {
// await sqlServerPool.connect();
await db.connectMongoDB();
await poll.postFichada(
{
"agente" : {
"_id" : "5e53f48ed40607323c624b8f"
},
"fecha" : "2013-09-27T14:06:46.000Z",
"esEntrada" : false,
"reloj" : 1,
"format" : "RSI_DLF_IDENTITY_VERIFIED",
"data1" : "0000001018",
"data2" : "3"
}
)

// let response = await fetch("/subscribe");
// if (response.status == 502) {
// // Status 502 is a connection timeout error,
// // may happen when the connection was pending for too long,
// // and the remote server or a proxy closed it
// // let's reconnect
// await subscribe();
// } else if (response.status != 200) {
// // An error - let's show it
// showMessage(response.statusText);
// // Reconnect in one second
// await new Promise(resolve => setTimeout(resolve, 1000));
// await subscribe();
// } else {
// // Get and show the message
// let message = await response.text();
// showMessage(message);
// // Call subscribe() again to get the next message
// await subscribe();
// }
}

sync();
74 changes: 74 additions & 0 deletions logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const { createLogger, format, transports } = require('winston');
const Transport = require('winston-transport');
const { combine, timestamp, printf } = format;


// Custom Output Format
const printFormat = printf(({ level, message, timestamp}) => {
return `${timestamp} [${level}]: ${message}`;
});

const customFormat = combine(
timestamp(),
printFormat
);


// Custom Transport. Logs only info level to console
class CustomInfoTransport extends Transport {
constructor(options) {
super(options);
this.name = 'customLogger';
this.level = options && options.level || 'info';
this.levelOnly = options && options.levelOnly;
this.levels = options && options.levels || [];
}

log(info, callback) {
try{

if (this.levelOnly && info.level == this.level ) {
console.log(`${info.timestamp} [${info.level}]: ${info.message}`);
}
this.emit('logged');
callback();
}
catch (err){
console.log(err);
}
}
}


/**
* Final Logger Configuration
* - Write all logs ONLY with level `info` to console
* - Write all logs with level `error` and below to `error.log`
* - Write all logs with level `debug` and below to `combined.log`
*/
const logger = createLogger({
format: customFormat,
exitOnError: false,
transports: [
new CustomInfoTransport({
levelOnly: true,
}),
new transports.File({ filename: 'error.log', level: 'error' }),
new transports.File({ filename: 'combined.log', level: 'debug' })
]
});

//
// If we're not in production then log to the `console` with the format:
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
//
// if (process.env.NODE_ENV !== 'production') {
// logger.add(new winston.transports.Console({
// format: winston.format.simple()
// }));
// }

module.exports = {
logger : logger,
printFormat: printFormat
}
Loading

0 comments on commit 92d5bd8

Please sign in to comment.