Resolve username from opt board id

pull/47/head
HardiReady 2018-10-20 20:00:36 +02:00
parent ee1bdba661
commit 631eaff9f7
5 changed files with 2468 additions and 2203 deletions

View File

@ -15,6 +15,7 @@ module.exports = {
request: rootRoute + '/request', request: rootRoute + '/request',
signatures: '/signatures', signatures: '/signatures',
signUp: rootRoute + '/authenticate/signup', signUp: rootRoute + '/authenticate/signup',
slotting: rootRoute + '/slotting',
squads: rootRoute + '/squads', squads: rootRoute + '/squads',
users: rootRoute + '/users', users: rootRoute + '/users',
wars: rootRoute + '/wars', wars: rootRoute + '/wars',

4560
api/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -21,23 +21,24 @@
"dependencies": { "dependencies": {
"async": "^2.5.0", "async": "^2.5.0",
"bcryptjs": "^2.4.3", "bcryptjs": "^2.4.3",
"body-parser": "~1.13.2", "body-parser": "^1.18.3",
"cors": "^2.8.4", "cors": "^2.8.4",
"cron": "^1.3.0", "cron": "^1.4.1",
"debug": "^3.1.0", "debug": "^3.1.0",
"express": "^4.16.2", "express": "^4.16.4",
"imagemin": "^5.2.2", "imagemin": "^5.2.2",
"imagemin-pngquant": "^5.0.0", "imagemin-pngquant": "^5.0.0",
"jimp": "^0.2.27", "jimp": "^0.5.4",
"jsonwebtoken": "^7.4.3", "jsonwebtoken": "^8.3.0",
"lodash": "^4.17.4", "lodash": "^4.17.4",
"mkdirp": "^0.5.1", "mkdirp": "^0.5.1",
"mongoose": "^5.0.3", "mongoose": "^5.0.3",
"morgan": "~1.6.1", "morgan": "^1.9.1",
"multer": "^1.3.0", "multer": "^1.3.0",
"node-html-parser": "^1.1.10",
"node-sha1": "^1.0.1", "node-sha1": "^1.0.1",
"q": "^1.5.0", "q": "^1.5.0",
"serve-favicon": "~2.3.0", "serve-favicon": "^2.5.0",
"supports-color": "^5.1.0" "supports-color": "^5.1.0"
}, },
"devDependencies": { "devDependencies": {
@ -48,8 +49,8 @@
"eslint": "^4.18.2", "eslint": "^4.18.2",
"eslint-config-google": "^0.9.1", "eslint-config-google": "^0.9.1",
"hercule": "^4.1.1", "hercule": "^4.1.1",
"mocha": "^3.5.3", "mocha": "^5.2.0",
"mongodb-memory-server": "^1.7.3", "mongodb-memory-server": "^2.6.0",
"nodemon": "^1.14.12" "nodemon": "^1.14.12"
} }
} }

89
api/routes/slotting.js Normal file
View File

@ -0,0 +1,89 @@
'use strict';
// modules
const express = require('express');
const http = require("http");
const https = require("https");
const parse = require('node-html-parser').parse;
// HTTP status codes by name
const codes = require('./http-codes');
const routerHandling = require('../middleware/router-handling');
/**
* getHtml: REST get request returning HTML page response
* @param options: http options object
* @param onResult: callback to pass the results JSON object(s) back
*/
const getHtml = (options, onResult) => {
let port = (options.port === 443) ? https : http;
let req = port.request(options, (res) => {
var output = '';
console.log(options.host + ':' + res.statusCode);
res.setEncoding('utf8');
res.on('data', function (chunk) {
output += chunk;
});
res.on('end', function() {
if (res.statusCode === 301) { // follow redirect
const location = res.headers.location;
const baseUrl = ((options.port === 443) ? 'https':'http') + '://' + options.host;
options.path = location.replace(baseUrl, '');
getHtml(options, (status, redirectData) => {
onResult(status, redirectData);
})
} else {
onResult(res.statusCode, output);
}
});
});
req.end();
};
const defaulUserReqOptions = {
host: 'opt4.net',
port: 443,
path: '/dashboard/index.php?user/',
method: 'GET'
};
const slotting = new express.Router();
// routes **********************
slotting.route('/user/:id')
.get((req, res, next) => {
const userId = req.params.id;
const options = Object.assign({}, defaulUserReqOptions);
options.path = options.path.concat(userId);
getHtml(options, (status, targetRes) => {
if (status !== codes.success) {
const err = new Error('Can not resolve user from remote service');
err.status = codes.notfound;
return next(err);
}
const root = parse(targetRes);
const userNameEl = root.querySelector('.contentTitle');
let user = {
name: userNameEl.childNodes[0].rawText.trim()
};
res.locals.items = user;
res.locals.processed = true;
return next();
});
})
.all(
routerHandling.httpMethodNotAllowed
);
// this middleware function can be used, if you like or remove it
// it looks for object(s) in res.locals.items and if they exist, they are send to the client as json
slotting.use(routerHandling.emptyResponse);
module.exports = slotting;

View File

@ -48,6 +48,7 @@ const commandRouter = require('./routes/command');
const campaignRouter = require('./routes/campaigns'); const campaignRouter = require('./routes/campaigns');
const warRouter = require('./routes/wars'); const warRouter = require('./routes/wars');
const logRouter = require('./routes/logs'); const logRouter = require('./routes/logs');
const slottingRouter = require('./routes/slotting');
// Configuration *********************************** // Configuration ***********************************
// mongoose promise setup // mongoose promise setup
@ -98,6 +99,7 @@ app.use(urls.wars, warRouter);
app.use(urls.players, playerRouter); app.use(urls.players, playerRouter);
app.use(urls.campaigns, campaignRouter); app.use(urls.campaigns, campaignRouter);
app.use(urls.logs, logRouter); app.use(urls.logs, logRouter);
app.use(urls.slotting, slottingRouter);
app.use(urls.command, apiAuthenticationMiddleware, checkAdmin, commandRouter); app.use(urls.command, apiAuthenticationMiddleware, checkAdmin, commandRouter);
app.use(urls.account, apiAuthenticationMiddleware, checkAdmin, accountRouter); app.use(urls.account, apiAuthenticationMiddleware, checkAdmin, accountRouter);