Skip to content

Commit

Permalink
Backport PR #7009
Browse files Browse the repository at this point in the history
---------

**Commit 1:**
[url shortener] Use kibana.index config value, and fix silent error

* Original sha: 313bfc8
* Authored by Jim Unger <[email protected]> on 2016-04-21T21:22:58Z
  • Loading branch information
elastic-jasper committed Jun 17, 2016
1 parent 5099549 commit ee39b00
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
16 changes: 12 additions & 4 deletions src/server/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,25 @@ module.exports = 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);
}
}
});

server.route({
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);
}
}
});

Expand Down
10 changes: 6 additions & 4 deletions src/server/http/short_url_lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ const crypto = require('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: {
Expand All @@ -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
})
Expand All @@ -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: {
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit ee39b00

Please sign in to comment.