From bcfc82b6a967695b5804f36f1b9f5d75cbc35036 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Wed, 28 Mar 2018 11:30:05 -0500 Subject: [PATCH] Improve root api endpoint finding The relative paths here weren't really doing their job. Instead, go ahead and pull in the function we wrote for the later angular5 patch to dynamically find the base href and use it to figure out what we should be appending the api paths to. Also, Remove double api prefix from tenants call Erroneously added it in the previous patch while also updating getSourceUrl to pre-pend the api string appropriately. Change-Id: Ib58c4569dbe21824c138c57c6353454ea1e4bc71 --- web/dashboard.js | 2 +- web/util.js | 25 +++++++++++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/web/dashboard.js b/web/dashboard.js index 409d2bdf26..77884e5b50 100644 --- a/web/dashboard.js +++ b/web/dashboard.js @@ -29,7 +29,7 @@ angular.module('zuulTenants', []).controller( 'mainController', function ($scope, $http, $location) { $scope.tenants = undefined $scope.tenants_fetch = function () { - $http.get(getSourceUrl('api/tenants', $location)) + $http.get(getSourceUrl('tenants', $location)) .then(function success (result) { $scope.tenants = result.data }) diff --git a/web/util.js b/web/util.js index b8a418650d..89cff40df9 100644 --- a/web/util.js +++ b/web/util.js @@ -1,4 +1,4 @@ -/* global ZUUL_API_URL */ +/* global URL, ZUUL_API_URL */ // @licstart The following is the entire license notice for the // JavaScript code in this page. // @@ -37,15 +37,24 @@ export function getSourceUrl (filename, $location) { if (typeof ZUUL_API_URL !== 'undefined') { return `${ZUUL_API_URL}/api/${filename}` } else { - let tenant = extractTenant($location.url()) + const currentUrl = new URL(window.location) + const tenant = extractTenant(currentUrl.href) + const baseHref = getBaseHrefFromPath(currentUrl.pathname) if (tenant) { - // Multi-tenant deploy. This is at t/a-tenant/x.html. api path is at - // api/tenant/a-tenant/x, so should be at ../../api/tenant/a-tenant/x - return `../../api/tenant/${tenant}/${filename}` + // Multi-tenant deploy. This is at t/a-tenant/x.html + return `${baseHref}api/tenant/${tenant}/${filename}` } else { - // Whilelabel deploy. This is at x.html. api path is at - // api/x, so should be at api/x - return `api/${filename}` + // Whitelabel deploy or tenants list, such as /status.html, /tenants.html + // or /zuul/status.html or /zuul/tenants.html + return `${baseHref}api/${filename}` } } } + +function getBaseHrefFromPath (path) { + if (path.includes('/t/')) { + return path.slice(0, path.lastIndexOf('/t/') + 1) + } else { + return path.split('/').slice(0, -1).join('/') + '/' + } +}