Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add logger, console->logger migration #79

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions caracal.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const sanitizeBody = require('./handlers/sanitizeHandler.js');
const DataSet = require('./handlers/datasetHandler.js');
const Model = require('./handlers/modelTrainer.js');
const DataTransformationHandler = require('./handlers/dataTransformationHandler.js');

// services
const {logger} = require('./service/logger/winston');

// TODO validation of data

var WORKERS = process.env.NUM_THREADS || 4;
Expand Down Expand Up @@ -139,12 +143,12 @@ for (let i in routeConfig) {
if (Object.prototype.hasOwnProperty.call(routeConfig, i)) {
let rule = routeConfig[i];
if (!rule.method) {
console.error('rule number '+ i +' has no "method"');
logger.error('rule number '+ i +' has no "method"');
process.exit(1);
}
if (rule.method == 'static') {
if (!rule.use) {
console.error('rule number '+ i +' is static and has no "use"');
logger.error('rule number '+ i +' is static and has no "use"');
process.exit(1);
}
app.use(express.static(rule.use));
Expand All @@ -153,15 +157,15 @@ for (let i in routeConfig) {
if (Object.prototype.hasOwnProperty.call(rule.handlers, j)) {
let handler = rule.handlers[j];
if (!rule.route) {
console.error('rule number '+ i +' has no "route"');
logger.error('rule number '+ i +' has no "route"');
process.exit(1);
}
if (!handler.function) {
console.error('rule number '+ i +' handler ' + j + ' has no "function"');
logger.error('rule number '+ i +' handler ' + j + ' has no "function"');
process.exit(1);
}
if (! HANDLERS.hasOwnProperty(handler.function)) {
console.error('handler named "'+ handler.function + '" not found (rule '+ i +' handler ' + j + ')');
logger.error('handler named "'+ handler.function + '" not found (rule '+ i +' handler ' + j + ')');
process.exit(1);
}
let args = handler.args || [];
Expand All @@ -187,9 +191,9 @@ app.use(function(err, req, res, next) {
// wrap strings in a json
if (typeof err === 'string' || err instanceof String) {
err = {'error': err};
console.error(err);
logger.error(err);
} else {
console.error(err.error || err.message || err.toString());
logger.error(err.error || err.message || err.toString());
}
res.status(statusCode).json(err);
});
Expand All @@ -202,17 +206,17 @@ var startApp = function(app) {
var sslPkPath = "./ssl/privatekey.pem";
var sslCertPath = "./ssl/certificate.pem";
if (fs.existsSync(sslPkPath) && fs.existsSync(sslCertPath)) {
console.info("Starting in HTTPS Mode mode");
logger.info("Starting in HTTPS Mode mode");
httpsOptions.key = fs.readFileSync(sslPkPath, 'utf8');
httpsOptions.cert = fs.readFileSync(sslCertPath, 'utf8');
}
} catch (err) {
console.error(err);
logger.error(err);
}
if (httpsOptions.key && httpsOptions.cert) {
https.createServer(httpsOptions, app).listen(PORT, () => console.log('listening HTTPS on ' + PORT));
https.createServer(httpsOptions, app).listen(PORT, () => logger.info('listening HTTPS on ' + PORT));
} else {
app.listen(PORT, () => console.log('listening on ' + PORT));
app.listen(PORT, () => logger.info('listening on ' + PORT));
}
};
};
Expand Down
31 changes: 16 additions & 15 deletions handlers/authHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const jwksClient = require('jwks-rsa');
var atob = require('atob');
var fs = require('fs');
var filterFunction = require('./filterFunction.js');
const {logger} = require('../service/logger/winston');

const {execSync} = require('child_process');
const preCommand = "openssl req -subj ";
Expand All @@ -24,7 +25,7 @@ if (!fs.existsSync('./keys/key') && !fs.existsSync('./keys/key.pub') && GENERATE
try {
execSync(`${preCommand}'/CN=www.camicroscope.com/O=caMicroscope Local Instance Key./C=US'${postCommand}`);
} catch (err) {
console.log({err: err});
logger.info({err: err});
}
}

Expand All @@ -35,13 +36,13 @@ try {
} else {
if (DISABLE_SEC || ENABLE_SECURITY_AT && Date.parse(ENABLE_SECURITY_AT) > Date.now()) {
PRIKEY = '';
console.warn('prikey null since DISABLE_SEC and no prikey provided');
logger.warn('prikey null since DISABLE_SEC and no prikey provided');
} else {
console.error('prikey does not exist');
logger.error('prikey does not exist');
}
}
} catch (err) {
console.error(err);
logger.error(err);
}

try {
Expand All @@ -51,13 +52,13 @@ try {
} else {
if (DISABLE_SEC || ENABLE_SECURITY_AT && Date.parse(ENABLE_SECURITY_AT) > Date.now()) {
PUBKEY = '';
console.warn('pubkey null since DISABLE_SEC and no prikey provided');
logger.warn('pubkey null since DISABLE_SEC and no prikey provided');
} else {
console.error('pubkey does not exist');
logger.error('pubkey does not exist');
}
}
} catch (err) {
console.error(err);
logger.error(err);
}

if (DISABLE_SEC && !JWK_URL) {
Expand All @@ -69,7 +70,7 @@ if (DISABLE_SEC && !JWK_URL) {
jwksUri: JWK_URL,
});
} else {
console.error('need JWKS URL (JWK_URL)');
logger.error('need JWKS URL (JWK_URL)');
process.exit(1);
}

Expand Down Expand Up @@ -104,9 +105,9 @@ function jwkTokenTrade(jwksClient, signKey, userFunction) {
res.status(401).send('{"err":"no token found"}');
}
jwksClient.getSigningKey(getJwtKid(THISTOKEN), (err, key) => {
// console.log(key);
// logger.info(key);
if (err) {
console.error(err);
logger.error(err);
res.status(401).send({
'err': err,
});
Expand All @@ -131,7 +132,7 @@ function tokenTrade(checkKey, signKey, userFunction) {
}
jwt.verify(THISTOKEN, checkKey, jwtOptions, function(err, token) {
if (err) {
console.error(err);
logger.error(err);
res.status(401).send({
'err': err,
});
Expand All @@ -143,7 +144,7 @@ function tokenTrade(checkKey, signKey, userFunction) {
});
} else {
userFunction(token).then((x) => {
// console.log(x);
// logger.info(x);
if (x === false) {
res.status(401).send({
'err': 'User Unauthorized',
Expand All @@ -161,7 +162,7 @@ function tokenTrade(checkKey, signKey, userFunction) {
});
}
}).catch((e) => {
console.log(e);
logger.info(e);
res.status(401).send(e);
});
}
Expand All @@ -177,7 +178,7 @@ function loginHandler(checkKey) {
try {
token = jwt.decode(getToken(req)) || {};
} catch (e) {
console.warn(e);
logger.warn(e);
}
req.tokenInfo = token;
req.userType = token.userType || DEFAULT_USER_TYPE || 'Null';
Expand All @@ -193,7 +194,7 @@ function loginHandler(checkKey) {
}
jwt.verify(getToken(req), checkKey, jwtOptions, function(err, token) {
if (err) {
console.error(err);
logger.error(err);
res.status(401).send({
'err': err,
});
Expand Down
11 changes: 6 additions & 5 deletions handlers/dataHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var mongo = require('mongodb');

var MONGO_URI = process.env.MONGO_URI || 'mongodb://localhost';
var DISABLE_SEC = (process.env.DISABLE_SEC === 'true') || false;
const {logger} = require('../service/logger/winston');


function mongoFind(database, collection, query) {
Expand Down Expand Up @@ -54,7 +55,7 @@ function mongoDistinct(database, collection, upon, query) {
}
});
} catch (error) {
console.error(error);
logger.error(error);
rej(error);
}
});
Expand Down Expand Up @@ -82,7 +83,7 @@ function mongoAdd(database, collection, data) {
}
});
} catch (error) {
console.error(error);
logger.error(error);
rej(error);
}
});
Expand All @@ -109,7 +110,7 @@ function mongoDelete(database, collection, query) {
});
}
} catch (error) {
console.error(error);
logger.error(error);
rej(error);
}
});
Expand Down Expand Up @@ -139,7 +140,7 @@ function mongoAggregate(database, collection, pipeline) {
});
}
} catch (error) {
console.error(error);
logger.error(error);
rej(error);
}
});
Expand All @@ -159,7 +160,7 @@ function mongoUpdate(database, collection, query, newVals) {
}
dbo.collection(collection).updateOne(query, newVals, function(err, result) {
if (err) {
console.log(err);
logger.info(err);
rej(err);
}
delete result.connection;
Expand Down
30 changes: 16 additions & 14 deletions handlers/dataTransformationHandler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const client = require("mongodb").MongoClient;
const fs = require("fs");

const {logger} = require('../service/logger/winston');

class DataTransformationHandler {
constructor(url, path) {
this.url = url;
Expand All @@ -14,7 +16,7 @@ class DataTransformationHandler {
}

startHandler() {
console.log("||-- Start --||");
logger.info("||-- Start --||");
this.counter = 0;
this.max = 300;
this.id = 1;
Expand All @@ -29,7 +31,7 @@ class DataTransformationHandler {
config = JSON.parse(rawdata);
} catch (err) {
if (err.code == "ENOENT") {
console.log(`'${this.filePath}' File Fot Found!`);
logger.info(`'${this.filePath}' File Fot Found!`);
} else {
throw err;
}
Expand All @@ -54,8 +56,8 @@ class DataTransformationHandler {
collection.find(query).toArray((err, rs) => {
if (err) {
this.isProcessing = false;
console.log(`||-- The 'Preset Labels' Document Upgrade Is Failed --||`);
console.log(err);
logger.info(`||-- The 'Preset Labels' Document Upgrade Is Failed --||`);
logger.info(err);
dbc.close();
this.cleanHandler();
return;
Expand All @@ -68,8 +70,8 @@ class DataTransformationHandler {
collection.insertOne(defaultData, (err, result) => {
if (err) {
this.isProcessing = false;
console.log(`||-- The 'Preset Labels' Document Upgrade Is Failed --||`);
console.log(err);
logger.info(`||-- The 'Preset Labels' Document Upgrade Is Failed --||`);
logger.info(err);
dbc.close();
this.cleanHandler();
return;
Expand All @@ -95,32 +97,32 @@ class DataTransformationHandler {
collection.deleteOne(query, (err, result) => {
if (err) {
this.isProcessing = false;
console.log(`||-- The 'Preset Labels' Document Upgrade Is Failed --||`);
console.log(err);
logger.info(`||-- The 'Preset Labels' Document Upgrade Is Failed --||`);
logger.info(err);
dbc.close();
this.cleanHandler();
return;
}
collection.insertOne(config, (err, result) => {
if (err) {
this.isProcessing = false;
console.log(`||-- The 'Preset Labels' Document Upgrade Is Failed --||`);
console.log(err);
logger.info(`||-- The 'Preset Labels' Document Upgrade Is Failed --||`);
logger.info(err);
dbc.close();
this.cleanHandler();
return;
}
this.isProcessing = false;
console.log(`||-- The Document At The 'configuration' Collection Has Been Upgraded To Version 1.0.0 --||`);
logger.info(`||-- The Document At The 'configuration' Collection Has Been Upgraded To Version 1.0.0 --||`);
dbc.close();
this.cleanHandler();
});
});
}
});
}).catch((err) => {
console.log(`||-- The 'Preset Labels' Document Upgrade Is Failed --||`);
console.log(err.message);
logger.info(`||-- The 'Preset Labels' Document Upgrade Is Failed --||`);
logger.info(err.message);
this.isProcessing = false;
if (this.max == this.counter) {
this.cleanHandler();
Expand All @@ -144,7 +146,7 @@ class DataTransformationHandler {
}

cleanHandler() {
console.log("||-- Done --||");
logger.info("||-- Done --||");
this.counter = 0;
this.max = 300;
this.id = 1;
Expand Down
8 changes: 5 additions & 3 deletions handlers/datasetHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const crypto = require("crypto");
const AdmZip = require('adm-zip');
const path = require('path');

const {logger} = require('../service/logger/winston');

let LABELS_PATH = null;
let IMAGES_SPRITE_PATH = null;

Expand All @@ -32,7 +34,7 @@ class Data {
async load() {
let This = this;
const imgRequest = new Promise((resolve) => {
// console.log(this.IMAGES_SPRITE_PATH);
// logger.info(this.IMAGES_SPRITE_PATH);
inkjet.decode(fs.readFileSync(this.IMAGES_SPRITE_PATH), function(err, decoded) {
const pixels = Float32Array.from(decoded.data).map((pixel) => {
return pixel / 255;
Expand Down Expand Up @@ -126,7 +128,7 @@ class Data {
function getDataset() {
return function(req, res) {
let data = JSON.parse(req.body);
// console.log(req.body.ar);
// logger.info(req.body.ar);
let userFolder = crypto.randomBytes(20).toString('hex');
if (!fs.existsSync('dataset')) {
fs.mkdirSync('dataset/');
Expand Down Expand Up @@ -155,7 +157,7 @@ function deleteData() {
if (err) {
throw err;
}
console.log(`Temp folder deleted!`);
logger.info(`Temp folder deleted!`);
});
res.json({status: 'Temp folder deleted!'});
};
Expand Down
3 changes: 2 additions & 1 deletion handlers/iipHandler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var proxy = require('http-proxy-middleware');

var IIP_PATH = process.env.IIP_PATH || 'http://ca-iip/';
const {logger} = require('../service/logger/winston');

iipHandler = function(req, res, next) {
if (req.query) {
Expand All @@ -22,7 +23,7 @@ iipHandler = function(req, res, next) {
proxy({
secure: false,
onError(err, req, res) {
console.log(err);
logger.error(err);
err.statusCode = 500;
next(err);
},
Expand Down
Loading