diff --git a/docs/development/core/development-basepath.asciidoc b/docs/development/core/development-basepath.asciidoc index b7e0dec88bf50e..2da65079350150 100644 --- a/docs/development/core/development-basepath.asciidoc +++ b/docs/development/core/development-basepath.asciidoc @@ -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`); } }); ----------- diff --git a/src/server/config/complete.js b/src/server/config/complete.js index 70acb2edf043f9..3a9da4911c2194 100644 --- a/src/server/config/complete.js +++ b/src/server/config/complete.js @@ -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}"`); diff --git a/src/server/config/complete.test.js b/src/server/config/complete.test.js index 437a05c06c4a13..2db0b1641dd19a 100644 --- a/src/server/config/complete.test.js +++ b/src/server/config/complete.test.js @@ -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); }); }); diff --git a/src/server/http/index.js b/src/server/http/index.js index 7d22dc93bd71fa..d42e2bcc031d8d 100644 --- a/src/server/http/index.js +++ b/src/server/http/index.js @@ -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}`); } @@ -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), @@ -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; } diff --git a/src/ui/public/chrome/api/nav.js b/src/ui/public/chrome/api/nav.js index c1d0ceea932758..6e00e4ad51e66c 100644 --- a/src/ui/public/chrome/api/nav.js +++ b/src/ui/public/chrome/api/nav.js @@ -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 diff --git a/src/ui/ui_apps/ui_app.js b/src/ui/ui_apps/ui_app.js index 9f9c73bd8d345d..b41f178d2656fd 100644 --- a/src/ui/ui_apps/ui_app.js +++ b/src/ui/ui_apps/ui_app.js @@ -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, diff --git a/src/ui/ui_nav_links/__tests__/ui_nav_link.js b/src/ui/ui_nav_links/__tests__/ui_nav_link.js index ee3a8c6c977c96..3f4294408fc6e4 100644 --- a/src/ui/ui_nav_links/__tests__/ui_nav_link.js +++ b/src/ui/ui_nav_links/__tests__/ui_nav_link.js @@ -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', @@ -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, @@ -35,22 +34,7 @@ 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', @@ -58,13 +42,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('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', @@ -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', @@ -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', @@ -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', @@ -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', @@ -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', ''); }); diff --git a/src/ui/ui_nav_links/ui_nav_link.js b/src/ui/ui_nav_links/ui_nav_link.js index 322717c446d96f..c4b2c4be78e801 100644 --- a/src/ui/ui_nav_links/ui_nav_link.js +++ b/src/ui/ui_nav_links/ui_nav_link.js @@ -1,5 +1,5 @@ export class UiNavLink { - constructor(urlBasePath, spec) { + constructor(spec) { const { id, title, @@ -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; diff --git a/src/ui/ui_nav_links/ui_nav_links_mixin.js b/src/ui/ui_nav_links/ui_nav_links_mixin.js index 38411c799bf348..d769d065fe842f 100644 --- a/src/ui/ui_nav_links/ui_nav_links_mixin.js +++ b/src/ui/ui_nav_links/ui_nav_links_mixin.js @@ -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()) diff --git a/src/ui/ui_render/ui_render_mixin.js b/src/ui/ui_render/ui_render_mixin.js index 6387177ed7c5ac..9bcc1aea9d16f8 100644 --- a/src/ui/ui_render/ui_render_mixin.js +++ b/src/ui/ui_render/ui_render_mixin.js @@ -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,