Merge branch 'release/v1.6.10' of hardi/opt-cc into master

pull/28/head
hardi 2018-02-04 16:38:57 +01:00 committed by HardiReady
commit ea2c3e34ba
17 changed files with 1653 additions and 328 deletions

View File

@ -6,6 +6,10 @@ module.exports = {
db: 'cc',
},
prod: {
env: 'production'
},
dev: {
env: 'dev'
},

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,31 +31,31 @@ 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');
})
};

View File

@ -12,8 +12,10 @@ const apiAuthentication = (req, res, next) => {
// decode token
if (token) {
const secret = process.env.NODE_ENV === config.prod.env ? process.env.JWS_SECRET : 'dev-secret';
// verifies secret and checks exp
jwt.verify(token, config.secret, (err, decoded) => {
jwt.verify(token, secret, (err, decoded) => {
if (err) {
return res.status(403).json({success: false, message: 'Failed to authenticate token.'});
} else {

View File

@ -0,0 +1,20 @@
"use strict";
// HTTP status codes by name
const codes = require('../routes/http-codes');
/**
* check if id has valid UUID format
*/
const idValidator = (req, res, next) => {
const reqId = req.params.id;
if (!reqId.match(/^[0-9a-fA-F]{24}$/)) {
const err = new Error("Invalid request id format");
err.status = codes.notfound;
return next(err);
}
next();
};
exports.idValidator = idValidator;

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,12 +8,11 @@ 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');
const config = require('../config/config');
const routerHandling = require('../middleware/router-handling');
const AppUserModel = require('../models/app-user');
@ -55,7 +54,10 @@ let authCheck = (username, password, res) => {
}
if (user && user.activated && bcrypt.compareSync(password, user.password)) {
// authentication successful
var secret = process.env.JWS_SECRET;
const secret = process.env.NODE_ENV === config.prod.env ? process.env.JWS_SECRET : 'dev-secret';
console.log(secret)
deferred.resolve({
_id: user._id,
username: user.username,

View File

@ -7,10 +7,12 @@ const logger = require('debug')('cc:campaigns');
// HTTP status codes by name
const codes = require('./http-codes');
const routerHandling = require('../middleware/router-handling');
const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
const checkMT = require('../middleware/permission-check').checkMT;
const routerHandling = require('../middleware/router-handling');
const idValidator = require('../middleware/validators').idValidator;
// Mongoose Model using mongoDB
const CampaignModel = require('../models/campaign');
const WarModel = require('../models/war');
@ -41,7 +43,7 @@ campaigns.route('/')
);
campaigns.route('/:id')
.get((req, res, next) => {
.get(idValidator, (req, res, next) => {
CampaignModel.findById(req.params.id, (err, item) => {
if (err) {
err.status = codes.servererror;

View File

@ -13,7 +13,9 @@ const codes = require('./http-codes');
const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
const checkHl = require('../middleware/permission-check').checkHl;
const routerHandling = require('../middleware/router-handling');
const idValidator = require('../middleware/validators').idValidator;
// Mongoose Model using mongoDB
const DecorationModel = require('../models/decoration');
@ -71,7 +73,7 @@ decoration.route('/')
);
decoration.route('/:id')
.get((req, res, next) => {
.get(idValidator, (req, res, next) => {
DecorationModel.findById(req.params.id, (err, item) => {
if (err) {
err.status = codes.servererror;

View File

@ -13,16 +13,15 @@ const codes = require('./http-codes');
const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
const checkHl = require('../middleware/permission-check').checkHl;
const routerHandling = require('../middleware/router-handling');
const idValidator = require('../middleware/validators').idValidator;
// Mongoose Model using mongoDB
const RankModel = require('../models/rank');
const ranks = express.Router();
// add middleware for bonus tasks 4 and 5 to find filter and offset/limit params for GET / and GET /:id
// routes **********************
ranks.route('/')
.get((req, res, next) => {
@ -74,7 +73,7 @@ ranks.route('/')
ranks.route('/:id')
.get((req, res, next) => {
.get(idValidator, (req, res, next) => {
RankModel.findById(req.params.id, (err, item) => {
if (err) {
err.status = codes.servererror;

View File

@ -16,6 +16,7 @@ const signatures = express.Router();
// routes **********************
signatures.route('/:id')
// does not use idValidator since it works by username
.get((req, res, next) => {
// decode UTF8-escape sequences (special characters)
const uri = decodeURIComponent(req.params.id);

View File

@ -13,7 +13,9 @@ const codes = require('./http-codes');
const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
const checkHl = require('../middleware/permission-check').checkHl;
const routerHandling = require('../middleware/router-handling');
const idValidator = require('../middleware/validators').idValidator;
// Mongoose Model using mongoDB
const SquadModel = require('../models/squad');
@ -74,7 +76,7 @@ squads.route('/')
);
squads.route('/:id')
.get((req, res, next) => {
.get(idValidator, (req, res, next) => {
SquadModel.findById(req.params.id, (err, item) => {
if (err) {
err.status = codes.servererror;

View File

@ -10,9 +10,11 @@ const codes = require('./http-codes');
const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
const checkHl = require('../middleware/permission-check').checkHl;
const offsetlimitMiddleware = require('../middleware/limitoffset-middleware-mongo');
const filterHandlerCreator = require('../middleware/filter-handler-mongo');
const routerHandling = require('../middleware/router-handling');
const idValidator = require('../middleware/validators').idValidator;
// Mongoose Model using mongoDB
const UserModel = require('../models/user');
@ -86,7 +88,7 @@ users.route('/')
users.route('/:id')
.get((req, res, next) => {
.get(idValidator, (req, res, next) => {
UserModel.findById(req.params.id).populate('squadId').exec((err, user) => {
if (err) {
err.status = codes.servererror;

View File

@ -12,10 +12,14 @@ const logger = require('debug')('cc:wars');
// HTTP status codes by name
const codes = require('./http-codes');
// access check
const apiAuthenticationMiddleware = require('../middleware/auth-middleware');
const checkMT = require('../middleware/permission-check').checkMT;
const routerHandling = require('../middleware/router-handling');
const routerHandling = require('../middleware/router-handling');
const idValidator = require('../middleware/validators').idValidator;
// log paser tool
const parseWarLog = require('../tools/log-parse-tool');
// Mongoose Model using mongoDB
@ -139,7 +143,7 @@ wars.route('/')
);
wars.route('/:id')
.get((req, res, next) => {
.get(idValidator, (req, res, next) => {
WarModel.findById(req.params.id, (err, item) => {
if (err) {
err.status = codes.servererror;

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');
@ -83,8 +88,8 @@ app.use(urls.request, requestRouter);
app.use(urls.awards, awardingRouter);
app.use(urls.wars, warRouter);
app.use(urls.players, playerRouter);
app.use(urls.campaigns,campaignRouter);
app.use(urls.logs,logRouter);
app.use(urls.campaigns, campaignRouter);
app.use(urls.logs, logRouter);
app.use(urls.command, apiAuthenticationMiddleware, checkAdmin, commandRouter);
app.use(urls.account, apiAuthenticationMiddleware, checkAdmin, accountRouter);
@ -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

View File

@ -1,6 +1,6 @@
{
"name": "opt-cc",
"version": "1.6.9",
"version": "1.6.10",
"author": "Florian Hartwich <hardi@noarch.de>",
"private": true,
"scripts": {