Skip to content

Commit 313bfc8

Browse files
committed
[url shortener] Use kibana.index config value, and fix silent error
1 parent c579d88 commit 313bfc8

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/server/http/index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,25 @@ module.exports = async function (kbnServer, server, config) {
110110
method: 'GET',
111111
path: '/goto/{urlId}',
112112
handler: async function (request, reply) {
113-
const url = await shortUrlLookup.getUrl(request.params.urlId);
114-
reply().redirect(config.get('server.basePath') + url);
113+
try {
114+
const url = await shortUrlLookup.getUrl(request.params.urlId);
115+
reply().redirect(config.get('server.basePath') + url);
116+
} catch (err) {
117+
reply(err);
118+
}
115119
}
116120
});
117121

118122
server.route({
119123
method: 'POST',
120124
path: '/shorten',
121125
handler: async function (request, reply) {
122-
const urlId = await shortUrlLookup.generateUrlId(request.payload.url);
123-
reply(urlId);
126+
try {
127+
const urlId = await shortUrlLookup.generateUrlId(request.payload.url);
128+
reply(urlId);
129+
} catch (err) {
130+
reply(err);
131+
}
124132
}
125133
});
126134

src/server/http/short_url_lookup.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import crypto from 'crypto';
33
export default function (server) {
44
async function updateMetadata(urlId, urlDoc) {
55
const client = server.plugins.elasticsearch.client;
6+
const kibanaIndex = server.config().get('kibana.index');
67

78
try {
89
await client.update({
9-
index: '.kibana',
10+
index: kibanaIndex,
1011
type: 'url',
1112
id: urlId,
1213
body: {
@@ -25,9 +26,10 @@ export default function (server) {
2526
async function getUrlDoc(urlId) {
2627
const urlDoc = await new Promise((resolve, reject) => {
2728
const client = server.plugins.elasticsearch.client;
29+
const kibanaIndex = server.config().get('kibana.index');
2830

2931
client.get({
30-
index: '.kibana',
32+
index: kibanaIndex,
3133
type: 'url',
3234
id: urlId
3335
})
@@ -45,9 +47,10 @@ export default function (server) {
4547
async function createUrlDoc(url, urlId) {
4648
const newUrlId = await new Promise((resolve, reject) => {
4749
const client = server.plugins.elasticsearch.client;
50+
const kibanaIndex = server.config().get('kibana.index');
4851

4952
client.index({
50-
index: '.kibana',
53+
index: kibanaIndex,
5154
type: 'url',
5255
id: urlId,
5356
body: {
@@ -79,7 +82,6 @@ export default function (server) {
7982
return {
8083
async generateUrlId(url) {
8184
const urlId = createUrlId(url);
82-
8385
const urlDoc = await getUrlDoc(urlId);
8486
if (urlDoc) return urlId;
8587

0 commit comments

Comments
 (0)