Skip to content

Commit

Permalink
[url shortener] Use kibana.index config value, and fix silent error
Browse files Browse the repository at this point in the history
  • Loading branch information
BigFunger committed Apr 21, 2016
1 parent c579d88 commit 313bfc8
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 @@ -110,17 +110,25 @@ 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);
}
}
});

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 @@ 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: {
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 313bfc8

Please sign in to comment.