From ca6dbcfbd5fb03b84a34818a8929c7a299bfb311 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Sun, 4 Sep 2016 19:31:33 +0200 Subject: [PATCH] Add config field for lstu; --- config.example.js | 3 +++ index.js | 34 +++++++++++++++++++++------------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/config.example.js b/config.example.js index 8c807fe..7ef53bc 100644 --- a/config.example.js +++ b/config.example.js @@ -21,4 +21,7 @@ module.exports = { // Network interface on which to run the webhook server. hostname: '0.0.0.0', + + // Instance of lstu to shorten links -- keep empty to not use. + lstu: 'https://lstu.fr' } diff --git a/index.js b/index.js index 350d0d4..fa6bbee 100644 --- a/index.js +++ b/index.js @@ -33,19 +33,27 @@ var app = express(); app.use(bodyParser.json()); // for parsing application/json app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded -function shortenURL(url, callback) { - request('https://lstu.fr/a', { method: 'POST', form: { lsturl: url, format: 'json' } }, function (err, res, body) { - try { - body = JSON.parse(body); - } catch(err) { - body = {err: 'cant parse JSON'}; - } - if (err || !body.success) { - console.error("Error when shortening link: (status: " + res.statusCode + ")", '\nerror:', err, '\nfailure reason:', body.msg); - } else { - callback(body.short); - } - }); +var shortenURL = function(url, callback) { callback(url); }; + +if (config.lstu) { + if (config.lstu[config.lstu.length - 1] !== '/') { + config.lstu += '/'; + } + + shortenURL = function shortenURL(url, callback) { + request(config.lstu + 'a', { method: 'POST', form: { lsturl: url, format: 'json' } }, function (err, res, body) { + try { + body = JSON.parse(body); + } catch(err) { + body = {err: 'cant parse JSON'}; + } + if (err || !body.success) { + console.error("Error when shortening link: (status: " + res.statusCode + ")", '\nerror:', err, '\nfailure reason:', body.msg); + } else { + callback(body.short); + } + }); + }; } var handlers = {