Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[url shortener] Use kibana.index config value, and fix silent error #7009

Merged
merged 1 commit into from
Apr 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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