From 313bfc8adefdc25ca7f2e2f186f764fd00291212 Mon Sep 17 00:00:00 2001 From: Jim Unger Date: Thu, 21 Apr 2016 16:22:58 -0500 Subject: [PATCH] [url shortener] Use kibana.index config value, and fix silent error --- src/server/http/index.js | 16 ++++++++++++---- src/server/http/short_url_lookup.js | 10 ++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/server/http/index.js b/src/server/http/index.js index 4668db081f595..b6440bec66044 100644 --- a/src/server/http/index.js +++ b/src/server/http/index.js @@ -110,8 +110,12 @@ module.exports = async function (kbnServer, server, config) { method: 'GET', path: '/goto/{urlId}', handler: async function (request, reply) { - const url = await shortUrlLookup.getUrl(request.params.urlId); - reply().redirect(config.get('server.basePath') + url); + try { + const url = await shortUrlLookup.getUrl(request.params.urlId); + reply().redirect(config.get('server.basePath') + url); + } catch (err) { + reply(err); + } } }); @@ -119,8 +123,12 @@ module.exports = async function (kbnServer, server, config) { method: 'POST', path: '/shorten', handler: async function (request, reply) { - const urlId = await shortUrlLookup.generateUrlId(request.payload.url); - reply(urlId); + try { + const urlId = await shortUrlLookup.generateUrlId(request.payload.url); + reply(urlId); + } catch (err) { + reply(err); + } } }); diff --git a/src/server/http/short_url_lookup.js b/src/server/http/short_url_lookup.js index 9462fc79e699c..2f0d3b3764486 100644 --- a/src/server/http/short_url_lookup.js +++ b/src/server/http/short_url_lookup.js @@ -3,10 +3,11 @@ import crypto from 'crypto'; export default function (server) { async function updateMetadata(urlId, urlDoc) { const client = server.plugins.elasticsearch.client; + const kibanaIndex = server.config().get('kibana.index'); try { await client.update({ - index: '.kibana', + index: kibanaIndex, type: 'url', id: urlId, body: { @@ -25,9 +26,10 @@ export default function (server) { async function getUrlDoc(urlId) { const urlDoc = await new Promise((resolve, reject) => { const client = server.plugins.elasticsearch.client; + const kibanaIndex = server.config().get('kibana.index'); client.get({ - index: '.kibana', + index: kibanaIndex, type: 'url', id: urlId }) @@ -45,9 +47,10 @@ export default function (server) { async function createUrlDoc(url, urlId) { const newUrlId = await new Promise((resolve, reject) => { const client = server.plugins.elasticsearch.client; + const kibanaIndex = server.config().get('kibana.index'); client.index({ - index: '.kibana', + index: kibanaIndex, type: 'url', id: urlId, body: { @@ -79,7 +82,6 @@ export default function (server) { return { async generateUrlId(url) { const urlId = createUrlId(url); - const urlDoc = await getUrlDoc(urlId); if (urlDoc) return urlId;