a little error handling

This commit is contained in:
Martin Donnelly 2018-06-14 00:37:47 +01:00
parent 355ff20898
commit 3ec2ee2dfd

View File

@ -30,6 +30,8 @@ const cache = apicache.middleware;
const ips = ['212.71.255.44', '82.35.75.161'];
const complexUrls = new Map();
// app.use(ipfilter(ips, { 'mode': 'allow' }));
app.use((err, req, res, _next) => {
@ -109,8 +111,12 @@ const bouncer = ['phpmyadmin',
const httpStart = RegExp('(http|ftp|https):\\/\\/');
function getUrl (req, res) {
const theUrl = req.params.encoded_id;
logger.debug('Want', theUrl);
let final;
const firstUrl = req.params.encoded_id;
// let theUrl;
logger.debug('Want', firstUrl);
const theUrl = complexUrls.get(firstUrl) || firstUrl;
if (theUrl === undefined || bouncer.indexOf(theUrl) !== -1 || theUrl === '') {
logger.warn(`You're not getting in ${theUrl}`);
res.status(400).send('');
@ -129,7 +135,12 @@ function getUrl (req, res) {
function urlQuery (callback) {
try {
let count = 0;
http.request(options, responseHandler).end();
final = options.href;
if (options.protocol === 'https:')
https.request(options, responseHandler).end();
else
http.request(options, responseHandler).end();
function responseHandler(response) {
response.setEncoding('utf8');
@ -161,6 +172,7 @@ function getUrl (req, res) {
};
count++;
final = rUrl.href;
if (rUrl.protocol === 'https:')
https.request(rUrl, responseHandler).end();
else
@ -188,12 +200,18 @@ function getUrl (req, res) {
urlQuery(a => {
// logger.info(a);
logger.info(`Got result for ${theUrl}`);
logger.info(`Actually used: ${final}`);
if (theUrl !== final)
complexUrls.set(theUrl, final);
// res.setHeader('Content-Type', 'application/json');
res.send(a);
});
}
app.get('/:encoded_id', cache('15 minutes'), getUrl);
// app.get('/:encoded_id', cache('15 minutes'), getUrl);
app.get('/:encoded_id', getUrl);
process.on('uncaughtException', function (err) {
console.error(err);