diff options
author | Tristan Cacqueray <tdecacqu@redhat.com> | 2018-08-14 05:13:15 +0000 |
---|---|---|
committer | Tristan Cacqueray <tdecacqu@redhat.com> | 2018-09-27 02:14:46 +0000 |
commit | 1082faae958bffa719ab333c3f5ae9776a8b26d7 (patch) | |
tree | 73b58bd8462d6d446f5c8caabb6a5f30695765f4 /web/src/api.js | |
parent | a74dc74ea1975388dc9f38cafe2cfb68de53811f (diff) | |
download | zuul-1082faae958bffa719ab333c3f5ae9776a8b26d7.tar.gz |
web: rewrite interface in react
This change rewrites the web interface using React:
http://lists.zuul-ci.org/pipermail/zuul-discuss/2018-August/000528.html
Depends-On: https://review.openstack.org/591964
Change-Id: Ic6c33102ac3da69ebd0b8e9c6c8b431d51f3cfd4
Co-Authored-By: Monty Taylor <mordred@inaugust.com>
Co-Authored-By: James E. Blair <jeblair@redhat.com>
Diffstat (limited to 'web/src/api.js')
-rw-r--r-- | web/src/api.js | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/web/src/api.js b/web/src/api.js new file mode 100644 index 000000000..9af4adb8e --- /dev/null +++ b/web/src/api.js @@ -0,0 +1,133 @@ +/* global process, window */ +// Copyright 2018 Red Hat, Inc +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. + +import Axios from 'axios' + +function getHomepageUrl (url) { + // + // Discover serving location from href. + // + // This is only needed for sub-directory serving. + // Serving the application from '/' may simply default to '/' + // + // Note that this is not enough for sub-directory serving, + // The static files location also needs to be adapted with the 'homepage' + // settings of the package.json file. + // + // This homepage url is used for the Router and Link resolution logic + // + let baseUrl + if (url) { + baseUrl = url + } else { + baseUrl = window.location.href + } + // Get dirname of the current url + baseUrl = baseUrl.replace(/\\/g, '/').replace(/\/[^/]*$/, '/') + + // Remove any query strings + if (baseUrl.includes('?')) { + baseUrl = baseUrl.slice(0, baseUrl.lastIndexOf('?')) + } + // Remove any hash anchor + if (baseUrl.includes('/#')) { + baseUrl = baseUrl.slice(0, baseUrl.lastIndexOf('/#') + 1) + } + + // Remove known sub-path + const subDir = [ + '/build/', + '/job/', + '/project/', + '/stream/', + ] + subDir.forEach(path => { + if (baseUrl.includes(path)) { + baseUrl = baseUrl.slice(0, baseUrl.lastIndexOf(path) + 1) + } + }) + + // Remove tenant scope + if (baseUrl.includes('/t/')) { + baseUrl = baseUrl.slice(0, baseUrl.lastIndexOf('/t/') + 1) + } + if (! baseUrl.endsWith('/')) { + baseUrl = baseUrl + '/' + } + // console.log('Homepage url is ', baseUrl) + return baseUrl +} + +function getZuulUrl () { + // Return the zuul root api absolute url + const ZUUL_API = process.env.REACT_APP_ZUUL_API + let apiUrl + + if (ZUUL_API) { + // Api url set at build time, use it + apiUrl = ZUUL_API + } else { + // Api url is relative to homepage path + apiUrl = getHomepageUrl () + 'api/' + } + if (! apiUrl.endsWith('/')) { + apiUrl = apiUrl + '/' + } + if (! apiUrl.endsWith('/api/')) { + apiUrl = apiUrl + 'api/' + } + // console.log('Api url is ', apiUrl) + return apiUrl +} +const apiUrl = getZuulUrl() + + +function getStreamUrl (apiPrefix) { + const streamUrl = (apiUrl + apiPrefix) + .replace(/(http)(s)?:\/\//, 'ws$2://') + 'console-stream' + // console.log('Stream url is ', streamUrl) + return streamUrl +} + +// Direct APIs +function fetchInfo () { + return Axios.get(apiUrl + 'info') +} +function fetchTenants () { + return Axios.get(apiUrl + 'tenants') +} +function fetchStatus (apiPrefix) { + return Axios.get(apiUrl + apiPrefix + 'status') +} +function fetchBuilds (apiPrefix, queryString) { + let path = 'builds' + if (queryString) { + path += '?' + queryString.slice(1) + } + return Axios.get(apiUrl + apiPrefix + path) +} +function fetchJobs (apiPrefix) { + return Axios.get(apiUrl + apiPrefix + 'jobs') +} + +export { + getHomepageUrl, + getStreamUrl, + fetchStatus, + fetchBuilds, + fetchJobs, + fetchTenants, + fetchInfo +} |