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

Porting Spencers changes over #18664

Merged
merged 1 commit into from
Apr 30, 2018
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
6 changes: 3 additions & 3 deletions docs/development/core/development-basepath.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ $http.get(chrome.addBasePath('/api/plugin/things'));
[float]
==== Server side

Append `config.get('server.basePath')` to any absolute URL path.
Append `request.getBasePath()` to any absolute URL path.

["source","shell"]
-----------
const basePath = server.config().get('server.basePath');
server.route({
path: '/redirect',
handler(req, reply) {
reply.redirect(`${basePath}/otherLocation`);
handler(request, reply) {
reply.redirect(`${request.getBasePath()}/otherLocation`);
}
});
-----------
Expand Down
4 changes: 4 additions & 0 deletions src/server/config/complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export default function (kbnServer, server, config) {
return kbnServer.config;
});

server.decorate('request', 'getBasePath', function () {
return kbnServer.config.get('server.basePath');
});

const unusedKeys = getUnusedConfigKeys(kbnServer.disabledPluginSpecs, kbnServer.settings, config.get())
.map(key => `"${key}"`);

Expand Down
10 changes: 5 additions & 5 deletions src/server/config/complete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ describe('server/config completeMixin()', function () {
};

describe('server decoration', () => {
it('adds a config() function to the server', () => {
const { config, callCompleteMixin, server } = setup({
it('adds several server/request decorations', () => {
const { callCompleteMixin, server } = setup({
settings: {},
configValues: {}
});

callCompleteMixin();
sinon.assert.calledOnce(server.decorate);
sinon.assert.calledWith(server.decorate, 'server', 'config', sinon.match.func);
expect(server.decorate.firstCall.args[2]()).toBe(config);
sinon.assert.callCount(server.decorate, 2);
sinon.assert.calledWithExactly(server.decorate, 'server', 'config', sinon.match.func);
sinon.assert.calledWithExactly(server.decorate, 'request', 'getBasePath', sinon.match.func);
});
});

Expand Down
6 changes: 3 additions & 3 deletions src/server/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default async function (kbnServer, server, config) {
path: '/',
method: 'GET',
handler(req, reply) {
const basePath = config.get('server.basePath');
const basePath = req.getBasePath();
const defaultRoute = config.get('server.defaultRoute');
reply.redirect(`${basePath}${defaultRoute}`);
}
Expand All @@ -90,7 +90,7 @@ export default async function (kbnServer, server, config) {
if (path === '/' || path.charAt(path.length - 1) !== '/') {
return reply(Boom.notFound());
}
const pathPrefix = config.get('server.basePath') ? `${config.get('server.basePath')}/` : '';
const pathPrefix = req.getBasePath() ? `${req.getBasePath()}/` : '';
return reply.redirect(format({
search: req.url.search,
pathname: pathPrefix + path.slice(0, -1),
Expand All @@ -110,7 +110,7 @@ export default async function (kbnServer, server, config) {
const uiSettings = request.getUiSettingsService();
const stateStoreInSessionStorage = await uiSettings.get('state:storeInSessionStorage');
if (!stateStoreInSessionStorage) {
reply().redirect(config.get('server.basePath') + url);
reply().redirect(request.getBasePath() + url);
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/ui/public/chrome/api/nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ export function initChromeNavApi(chrome, internals) {
};

internals.nav.forEach(link => {
link.url = relativeToAbsolute(link.url);
link.subUrlBase = relativeToAbsolute(link.subUrlBase);
link.url = relativeToAbsolute(chrome.addBasePath(link.url));
link.subUrlBase = relativeToAbsolute(chrome.addBasePath(link.subUrlBase));
});

// simulate a possible change in url to initialize the
Expand Down
2 changes: 1 addition & 1 deletion src/ui/ui_apps/ui_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class UiApp {
// unless an app is hidden it gets a navlink, but we only respond to `getNavLink()`
// if the app is also listed. This means that all apps in the kibanaPayload will
// have a navLink property since that list includes all normally accessible apps
this._navLink = new UiNavLink(kbnServer.config.get('server.basePath'), {
this._navLink = new UiNavLink({
id: this._id,
title: this._title,
order: this._order,
Expand Down
39 changes: 9 additions & 30 deletions src/ui/ui_nav_links/__tests__/ui_nav_link.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { UiNavLink } from '../ui_nav_link';
describe('UiNavLink', () => {
describe('constructor', () => {
it('initializes the object properties as expected', () => {
const urlBasePath = 'http://localhost:5601/rnd';
const spec = {
id: 'kibana:discover',
title: 'Discover',
Expand All @@ -17,13 +16,13 @@ describe('UiNavLink', () => {
disabled: true
};

const link = new UiNavLink(urlBasePath, spec);
const link = new UiNavLink(spec);
expect(link.toJSON()).to.eql({
id: spec.id,
title: spec.title,
order: spec.order,
url: `${urlBasePath}${spec.url}`,
subUrlBase: `${urlBasePath}${spec.url}`,
url: spec.url,
subUrlBase: spec.url,
description: spec.description,
icon: spec.icon,
hidden: spec.hidden,
Expand All @@ -35,36 +34,20 @@ describe('UiNavLink', () => {
});
});

it('initializes the url property without a base path when one is not specified in the spec', () => {
const urlBasePath = undefined;
const spec = {
id: 'kibana:discover',
title: 'Discover',
order: -1003,
url: '/app/kibana#/discover',
description: 'interactively explore your data',
icon: 'plugins/kibana/assets/discover.svg',
};
const link = new UiNavLink(urlBasePath, spec);
expect(link.toJSON()).to.have.property('url', spec.url);
});

it('initializes the order property to 0 when order is not specified in the spec', () => {
const urlBasePath = undefined;
const spec = {
id: 'kibana:discover',
title: 'Discover',
url: '/app/kibana#/discover',
description: 'interactively explore your data',
icon: 'plugins/kibana/assets/discover.svg',
};
const link = new UiNavLink(urlBasePath, spec);
const link = new UiNavLink(spec);

expect(link.toJSON()).to.have.property('order', 0);
});

it('initializes the linkToLastSubUrl property to false when false is specified in the spec', () => {
const urlBasePath = undefined;
const spec = {
id: 'kibana:discover',
title: 'Discover',
Expand All @@ -74,13 +57,12 @@ describe('UiNavLink', () => {
icon: 'plugins/kibana/assets/discover.svg',
linkToLastSubUrl: false
};
const link = new UiNavLink(urlBasePath, spec);
const link = new UiNavLink(spec);

expect(link.toJSON()).to.have.property('linkToLastSubUrl', false);
});

it('initializes the linkToLastSubUrl property to true by default', () => {
const urlBasePath = undefined;
const spec = {
id: 'kibana:discover',
title: 'Discover',
Expand All @@ -89,13 +71,12 @@ describe('UiNavLink', () => {
description: 'interactively explore your data',
icon: 'plugins/kibana/assets/discover.svg',
};
const link = new UiNavLink(urlBasePath, spec);
const link = new UiNavLink(spec);

expect(link.toJSON()).to.have.property('linkToLastSubUrl', true);
});

it('initializes the hidden property to false by default', () => {
const urlBasePath = undefined;
const spec = {
id: 'kibana:discover',
title: 'Discover',
Expand All @@ -104,13 +85,12 @@ describe('UiNavLink', () => {
description: 'interactively explore your data',
icon: 'plugins/kibana/assets/discover.svg',
};
const link = new UiNavLink(urlBasePath, spec);
const link = new UiNavLink(spec);

expect(link.toJSON()).to.have.property('hidden', false);
});

it('initializes the disabled property to false by default', () => {
const urlBasePath = undefined;
const spec = {
id: 'kibana:discover',
title: 'Discover',
Expand All @@ -119,13 +99,12 @@ describe('UiNavLink', () => {
description: 'interactively explore your data',
icon: 'plugins/kibana/assets/discover.svg',
};
const link = new UiNavLink(urlBasePath, spec);
const link = new UiNavLink(spec);

expect(link.toJSON()).to.have.property('disabled', false);
});

it('initializes the tooltip property to an empty string by default', () => {
const urlBasePath = undefined;
const spec = {
id: 'kibana:discover',
title: 'Discover',
Expand All @@ -134,7 +113,7 @@ describe('UiNavLink', () => {
description: 'interactively explore your data',
icon: 'plugins/kibana/assets/discover.svg',
};
const link = new UiNavLink(urlBasePath, spec);
const link = new UiNavLink(spec);

expect(link.toJSON()).to.have.property('tooltip', '');
});
Expand Down
6 changes: 3 additions & 3 deletions src/ui/ui_nav_links/ui_nav_link.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export class UiNavLink {
constructor(urlBasePath, spec) {
constructor(spec) {
const {
id,
title,
Expand All @@ -17,8 +17,8 @@ export class UiNavLink {
this._id = id;
this._title = title;
this._order = order;
this._url = `${urlBasePath || ''}${url}`;
this._subUrlBase = `${urlBasePath || ''}${subUrlBase || url}`;
this._url = url;
this._subUrlBase = subUrlBase || url;
this._description = description;
this._icon = icon;
this._linkToLastSubUrl = linkToLastSubUrl;
Expand Down
5 changes: 2 additions & 3 deletions src/ui/ui_nav_links/ui_nav_links_mixin.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { UiNavLink } from './ui_nav_link';

export function uiNavLinksMixin(kbnServer, server, config) {
export function uiNavLinksMixin(kbnServer, server) {
const uiApps = server.getAllUiApps();

const { navLinkSpecs = [] } = kbnServer.uiExports;
const urlBasePath = config.get('server.basePath');

const fromSpecs = navLinkSpecs
.map(navLinkSpec => new UiNavLink(urlBasePath, navLinkSpec));
.map(navLinkSpec => new UiNavLink(navLinkSpec));

const fromApps = uiApps
.map(app => app.getNavLink())
Expand Down
2 changes: 1 addition & 1 deletion src/ui/ui_render/ui_render_mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export function uiRenderMixin(kbnServer, server, config) {
branch: config.get('pkg.branch'),
buildNum: config.get('pkg.buildNum'),
buildSha: config.get('pkg.buildSha'),
basePath: config.get('server.basePath'),
basePath: request.getBasePath(),
serverName: config.get('server.name'),
devMode: config.get('env.dev'),
translations: translations,
Expand Down