Update modules and apply logger use

pull/26/head
HardiReady 2018-02-04 15:58:28 +01:00
parent ebfaa7afb1
commit ef65552c7b
6 changed files with 1599 additions and 317 deletions

View File

@ -6,8 +6,14 @@ const {exec} = require('child_process');
const UserModel = require('../models/user');
const signatureTool = require('../tools/signature-tool');
const debug = require('debug');
const error = debug('cc:cronjob:err');
const logger = debug('cc:cronjob');
logger.log = console.log.bind(console);
const createAllSignatures = () => {
console.log('\x1b[35m%s\x1b[0m', new Date().toLocaleString()
logger('\x1b[35m%s\x1b[0m', new Date().toLocaleString()
+ ': cron job started - UPDATE SIGNATURES');
// mock response
@ -25,40 +31,40 @@ const createAllSignatures = () => {
if (!err || (err && err.message.startsWith('Fraction not defined'))) {
callback()
} else {
console.error('\x1b[41m%s\x1b[0m', new Date().toLocaleString()
+ ': Error in execution - UPDATE SIGNATURES: ' + err)
error('\x1b[41m%s\x1b[0m', new Date().toLocaleString()
+ ': Error in execution - UPDATE SIGNATURES: ' + err.message)
}
};
signatureTool(user._id, res, next)
}, () => {
if (err) {
console.error('\x1b[41m%s\x1b[0m', new Date().toLocaleString()
+ ': Error in execution - UPDATE SIGNATURES: ' + err)
error('\x1b[41m%s\x1b[0m', new Date().toLocaleString()
+ ': Error in execution - UPDATE SIGNATURES: ' + err.message)
}
console.log('\x1b[35m%s\x1b[0m', new Date().toLocaleString()
logger('\x1b[35m%s\x1b[0m', new Date().toLocaleString()
+ ': finished successful - UPDATE SIGNATURES');
})
});
};
const createBackup = () => {
console.log('\x1b[35m%s\x1b[0m', new Date().toLocaleString()
logger('\x1b[35m%s\x1b[0m', new Date().toLocaleString()
+ ': cron job started - CREATE BACKUP');
exec(__dirname + '/../../backup/backup.sh', (error, stdout, stderr) => {
if (error) {
console.log(error);
exec(__dirname + '/../../backup/backup.sh', (err, stdout, stderr) => {
if (err) {
error(err.message);
}
console.log('\x1b[32m%s\x1b[0m',stderr);
console.log('\x1b[35m%s\x1b[0m', new Date().toLocaleString()
logger('\x1b[32m%s\x1b[0m',stderr);
logger('\x1b[35m%s\x1b[0m', new Date().toLocaleString()
+ ': cron job finished - CREATE BACKUP');
})
};
// Execute daily @ 02:30 AM
const cronJobSignature = cron.job('00 30 02 * * *', createAllSignatures);
const cronJobSignature = cron.job('00 09 * * * *', createAllSignatures);
// Execute on Mon, Thu and Sat @ 04:00 AM
const cronJobBackup = cron.job('00 00 04 * * mon,thu,sat', createBackup);
const cronJobBackup = cron.job('00 30 * * * *', createBackup);
module.exports = {
cronJobSignature: cronJobSignature,

1824
api/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -6,8 +6,8 @@
"author": "Florian Hartwich <hardi@noarch.de>",
"private": true,
"scripts": {
"start": "NODE_ENV=production node server.js",
"dev": "NODE_ENV=dev nodemon server.js",
"start": "DEBUG='cc:*' NODE_ENV=production node server.js",
"dev": "DEBUG='cc:*' NODE_ENV=dev nodemon server.js",
"test": "mocha --require ./test/config/spec_helper.js",
"e2e": "NODE_ENV=test node server.js"
},
@ -17,25 +17,26 @@
"body-parser": "~1.13.2",
"cors": "^2.8.4",
"cron": "^1.3.0",
"debug": "~2.2.0",
"express": "^4.16.1",
"debug": "^3.1.0",
"express": "^4.16.2",
"imagemin": "^5.2.2",
"imagemin-pngquant": "^5.0.0",
"jimp": "^0.2.27",
"jsonwebtoken": "^7.4.3",
"lodash": "^4.17.4",
"mkdirp": "^0.5.1",
"mongoose": "^4.12.0",
"mongoose": "^5.0.3",
"morgan": "~1.6.1",
"multer": "^1.3.0",
"node-sha1": "^1.0.1",
"q": "^1.5.0",
"serve-favicon": "~2.3.0"
"serve-favicon": "~2.3.0",
"supports-color": "^5.1.0"
},
"devDependencies": {
"chai": "^3.5.0",
"chai-http": "^3.0.0",
"mocha": "^3.5.3",
"nodemon": "^1.12.1"
"nodemon": "^1.14.12"
}
}

View File

@ -8,9 +8,6 @@ const Q = require('q');
const _ = require('lodash');
const logger = require('debug')('cc:authenticate');
const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
const checkAdmin = require('../middleware/permission-check').checkAdmin;
// HTTP status codes by name
const codes = require('./http-codes');
@ -55,7 +52,7 @@ let authCheck = (username, password, res) => {
}
if (user && user.activated && bcrypt.compareSync(password, user.password)) {
// authentication successful
var secret = process.env.JWS_SECRET;
let secret = process.env.JWS_SECRET;
deferred.resolve({
_id: user._id,
username: user.username,

View File

@ -5,7 +5,12 @@ const path = require('path');
const favicon = require('serve-favicon');
const bodyParser = require('body-parser');
const requestLogger = require('morgan');
const debug = require('debug')('cc:server');
const debug = require('debug');
const error = debug('cc:server:err');
const logger = debug('cc:server');
logger.log = console.log.bind(console);
const cors = require('cors')
const mongoose = require('mongoose');
@ -93,44 +98,29 @@ app.use(function (req, res) {
res.sendFile("public/index.html", {root: __dirname + '/..'});
});
// (from express-generator boilerplate standard code)
// Errorhandling and requests without proper URLs ************************
// catch 404 and forward to error handler
app.use((req, res, next) => {
debug('Catching unmatched request to answer with 404');
let err = new Error('Not Found');
err.status = 404;
next(err);
});
// register error handlers
errorResponseWare(app);
// Start the server
if (process.env.NODE_ENV !== config.test.env) {
const mongoosePromise = mongoose.connect(config.database.uri + config.database.db, {
useMongoClient: true
});
const mongoosePromise = mongoose.connect(config.database.uri + config.database.db);
mongoosePromise.then((db) => {
app.listen(config.port, (err) => {
if (err !== undefined) {
debug('Error on startup, ', err);
error('Error on startup, ', err);
}
else {
debug('Listening on port ' + config.port);
logger('Listening on port ' + config.port);
signatureCronJob.start();
backupCronJob.start();
}
});
})
} else {
const mongoosePromise = mongoose.connect(config.database.uri + config.test.db, {
useMongoClient: true
});
const mongoosePromise = mongoose.connect(config.database.uri + config.test.db);
mongoosePromise.then((db) => {
app.listen(config.test.port);
console.log('Listening on port ' + config.test.port);
logger('Listening on port ' + config.test.port);
})
}

View File

@ -23,7 +23,7 @@ do
# provide date for restore process, if data import is needed
if [ -z "$1" ]
then
echo "table ${i}"
(>&2 echo -e "$(date "+%Y-%m-%dT%T.%3N%z")\tTable: ${i}")
mongoexport --db cc --collection $i --out ${DATE}/collections/${i}.json;
else
mongoimport --db cc --collection $i --drop --file ${1}/collections/${i}.json