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

Fix a definite style guide, linting fixes, hooks #86

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

45 changes: 21 additions & 24 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
module.exports = {
'env': {
'browser': true,
'node': true,
'es6': true,
env: {
commonjs: true,
es6: true,
node: true,
},
'extends': [
'google',
'plugin:security/recommended'
],
plugins: [
'security'
],
'globals': {
'Atomics': 'readonly',
'SharedArrayBuffer': 'readonly',
extends: ['google'],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
},
'parserOptions': {
'ecmaVersion': 2018,
parserOptions: {
ecmaVersion: 2018,
},
'rules': {
"require-jsdoc" : 0,
"valid-jsdoc" : 0,
"max-len" : ["error", { "code": 125 ,"ignoreTemplateLiterals": true},],
"no-unused-vars" : 0,
"no-var" : 0,
"prefer-const":0,
"quotes": 0
rules: {
'prettier/prettier': 'error',
'object-curly-spacing': 'off',
'require-jsdoc': 0,
'space-before-function-paren': 'off',
'quote-props': 'off',
indent: 'off',
'comma-dangle': 'off',
'max-len': ['error', { code: 125, ignoreTemplateLiterals: true }],
},
plugins: ['prettier', 'security'],
extends: ['plugin:security/recommended'],
};
8 changes: 8 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// .prettierrc.js
module.exports = {
tabWidth: 2,
semi: true,
singleQuote: true,
trailingComma: 'es5',
printWidth: 120,
};
117 changes: 58 additions & 59 deletions caracal.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
require('dotenv').config();
const express = require('express');

var proxy = require('http-proxy-middleware');
const https = require('https');
var cookieParser = require('cookie-parser');
var throng = require('throng');
var routeConfig = require("./routes.json");
var helmet = require('helmet');
const cookieParser = require('cookie-parser');
const throng = require('throng');
const routeConfig = require('./routes.json');
// const helmet = require('helmet');
const fs = require('fs');

// handlers
Expand All @@ -23,11 +22,11 @@ const Model = require('./handlers/modelTrainer.js');
const DataTransformationHandler = require('./handlers/dataTransformationHandler.js');
// TODO validation of data

var WORKERS = process.env.NUM_THREADS || 4;
const WORKERS = process.env.NUM_THREADS || 4;

var PORT = process.env.PORT || 4010;
const PORT = process.env.PORT || 4010;

var MONGO_URI = process.env.MONGO_URI || 'mongodb://localhost';
const MONGO_URI = process.env.MONGO_URI || 'mongodb://localhost';

const app = express();
app.use(cookieParser());
Expand Down Expand Up @@ -65,13 +64,13 @@ app.use(cookieParser());
}));**/

// handle non-json raw body for post
app.use(function(req, res, next) {
var data = '';
app.use(function (req, res, next) {
let data = '';
req.setEncoding(null);
req.on('data', function(chunk) {
req.on('data', function (chunk) {
data += chunk;
});
req.on('end', function() {
req.on('end', function () {
req.body = data;
next();
});
Expand All @@ -83,88 +82,88 @@ app.get('/auth/Token/renew', auth.tokenTrade(auth.PUBKEY, auth.PRIKEY, userFunct
app.get('/auth/Token/proto', auth.firstSetupUserSignupExists());

// TODO way to populate this semi-automatically?
var HANDLERS = {
"loginHandler": function() {
const HANDLERS = {
loginHandler: function () {
return auth.loginHandler(auth.PUBKEY);
},
"sanitizeBody": function() {
sanitizeBody: function () {
return sanitizeBody;
},
"monitorCheck": monitor.check,
"mongoFind": dataHandlers.General.find,
"mongoAdd": dataHandlers.General.add,
"mongoUpdate": dataHandlers.General.update,
"mongoDelete": dataHandlers.General.delete,
"mongoDistinct": dataHandlers.General.distinct,
"filterHandler": auth.filterHandler,
"permissionHandler": permissionHandler,
"editHandler": auth.editHandler,
"proxyHandler": proxyHandler,
"getDataset": DataSet.getDataset,
"trainModel": Model.trainModel,
"deleteDataset": DataSet.deleteData,
"sendTrainedModel": Model.sendTrainedModel,
"iipHandler": function() {
monitorCheck: monitor.check,
mongoFind: dataHandlers.General.find,
mongoAdd: dataHandlers.General.add,
mongoUpdate: dataHandlers.General.update,
mongoDelete: dataHandlers.General.delete,
mongoDistinct: dataHandlers.General.distinct,
filterHandler: auth.filterHandler,
permissionHandler: permissionHandler,
editHandler: auth.editHandler,
proxyHandler: proxyHandler,
getDataset: DataSet.getDataset,
trainModel: Model.trainModel,
deleteDataset: DataSet.deleteData,
sendTrainedModel: Model.sendTrainedModel,
iipHandler: function () {
return iipHandler;
},
"markMulti": function() {
markMulti: function () {
return dataHandlers.Mark.multi;
},
"markSpatial": function() {
markSpatial: function () {
return dataHandlers.Mark.spatial;
},
"findMarkTypes": function() {
findMarkTypes: function () {
return dataHandlers.Mark.findMarkTypes;
},
"heatmapTypes": function() {
heatmapTypes: function () {
return dataHandlers.Heatmap.types;
},
"wcido": function() {
wcido: function () {
return dataHandlers.User.wcido;
},
"addPresetlabels": function() {
addPresetlabels: function () {
return dataHandlers.Presetlabels.add;
},
"updatePresetlabels": function() {
updatePresetlabels: function () {
return dataHandlers.Presetlabels.update;
},
"removePresetlabels": function() {
removePresetlabels: function () {
return dataHandlers.Presetlabels.remove;
},
};

// register configurable services
// TODO verify all
for (let i in routeConfig) {
for (const i in routeConfig) {
if (Object.prototype.hasOwnProperty.call(routeConfig, i)) {
let rule = routeConfig[i];
const rule = routeConfig[i];
if (!rule.method) {
console.error('rule number '+ i +' has no "method"');
console.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"');
console.error('rule number ' + i + ' is static and has no "use"');
process.exit(1);
}
app.use(express.static(rule.use));
} else {
for (let j in rule.handlers) {
for (const j in rule.handlers) {
if (Object.prototype.hasOwnProperty.call(rule.handlers, j)) {
let handler = rule.handlers[j];
const handler = rule.handlers[j];
if (!rule.route) {
console.error('rule number '+ i +' has no "route"');
console.error('rule number ' + i + ' has no "route"');
process.exit(1);
}
if (!handler.function) {
console.error('rule number '+ i +' handler ' + j + ' has no "function"');
console.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 + ')');
if (!HANDLERS.hasOwnProperty(handler.function)) {
console.error('handler named "' + handler.function + '" not found (rule ' + i + ' handler ' + j + ')');
process.exit(1);
}
let args = handler.args || [];
const args = handler.args || [];
// handler.function needs to be in handlers
app[rule.method](rule.route, HANDLERS[handler.function](...args));
}
Expand All @@ -174,35 +173,35 @@ for (let i in routeConfig) {
}

// render mongo returns/data
app.use('/data', function(req, res, next) {
app.use('/data', function (req, res, next) {
if (!req.data) {
res.status(404).json({});
}
res.json(req.data);
});

// error handler
app.use(function(err, req, res, next) {
let statusCode = err.statusCode || 500;
app.use(function (err, req, res, next) {
const statusCode = err.statusCode || 500;
// wrap strings in a json
if (typeof err === 'string' || err instanceof String) {
err = {'error': err};
err = { error: err };
console.error(err);
} else {
console.error(err.error || err.message || err.toString());
}
res.status(statusCode).json(err);
});

var startApp = function(app) {
return function() {
const startApp = function (app) {
return function () {
// Prepare for SSL/HTTPS
var httpsOptions = {};
const httpsOptions = {};
try {
var sslPkPath = "./ssl/privatekey.pem";
var sslCertPath = "./ssl/certificate.pem";
const sslPkPath = './ssl/privatekey.pem';
const sslCertPath = './ssl/certificate.pem';
if (fs.existsSync(sslPkPath) && fs.existsSync(sslCertPath)) {
console.info("Starting in HTTPS Mode mode");
console.info('Starting in HTTPS Mode mode');
httpsOptions.key = fs.readFileSync(sslPkPath, 'utf8');
httpsOptions.cert = fs.readFileSync(sslCertPath, 'utf8');
}
Expand Down
Loading