1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import Axios from "axios";
7 export const WORKBENCH_CONFIG_URL = process.env.REACT_APP_ARVADOS_CONFIG_URL || "/config.json";
9 interface WorkbenchConfig {
11 VOCABULARY_URL?: string;
12 FILE_VIEWERS_CONFIG_URL?: string;
15 export interface ClusterConfigJSON {
19 ActivateUsers: boolean
44 ArvadosDocsite: string;
45 VocabularyURL: string;
46 FileViewersConfigURL: string;
52 keepWebServiceUrl: string;
60 workbench2Url: string;
61 vocabularyUrl: string;
62 fileViewersConfigUrl: string;
65 export const fetchConfig = () => {
67 .get<WorkbenchConfig>(WORKBENCH_CONFIG_URL + "?nocache=" + (new Date()).getTime())
68 .then(response => response.data)
70 console.warn(`There was an exception getting the Workbench config file at ${WORKBENCH_CONFIG_URL}. Using defaults instead.`);
71 return Promise.resolve(getDefaultConfig());
73 .then(workbenchConfig => {
74 if (workbenchConfig.API_HOST === undefined) {
75 throw new Error(`Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`);
77 return Axios.get<ClusterConfigJSON>(getClusterConfigURL(workbenchConfig.API_HOST)).then(response => {
78 const config = new Config();
79 const clusterConfigJSON = response.data;
80 const warnLocalConfig = (varName: string) => console.warn(
81 `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \
82 remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`);
84 // Check if the workbench config has an entry for vocabulary and file viewer URLs
85 // If so, use these values (even if it is an empty string), but print a console warning.
86 // Otherwise, use the cluster config.
87 let fileViewerConfigUrl;
88 if (workbenchConfig.FILE_VIEWERS_CONFIG_URL !== undefined) {
89 warnLocalConfig("FILE_VIEWERS_CONFIG_URL");
90 fileViewerConfigUrl = workbenchConfig.FILE_VIEWERS_CONFIG_URL;
93 fileViewerConfigUrl = clusterConfigJSON.Workbench.FileViewersConfigURL || "/file-viewers-example.json";
95 config.fileViewersConfigUrl = fileViewerConfigUrl;
98 if (workbenchConfig.VOCABULARY_URL !== undefined) {
99 warnLocalConfig("VOCABULARY_URL");
100 vocabularyUrl = workbenchConfig.VOCABULARY_URL;
103 vocabularyUrl = clusterConfigJSON.Workbench.VocabularyURL || "/vocabulary-example.json";
105 // FIXME: The following line is for dev testing purposes
106 vocabularyUrl = "/vocabulary-example.json";
108 config.vocabularyUrl = vocabularyUrl;
110 config.rootUrl = clusterConfigJSON.Services.Controller.ExternalURL;
111 config.baseUrl = `${config.rootUrl}/${ARVADOS_API_PATH}`;
112 config.uuidPrefix = clusterConfigJSON.ClusterID;
113 config.websocketUrl = clusterConfigJSON.Services.Websocket.ExternalURL;
114 config.workbench2Url = clusterConfigJSON.Services.Workbench2.ExternalURL;
115 config.workbenchUrl = clusterConfigJSON.Services.Workbench1.ExternalURL;
116 config.keepWebServiceUrl = clusterConfigJSON.Services.WebDAV.ExternalURL;
117 mapRemoteHosts(clusterConfigJSON, config);
119 return { config, apiHost: workbenchConfig.API_HOST };
124 // Maps remote cluster hosts and removes the default RemoteCluster entry
125 export const mapRemoteHosts = (clusterConfigJSON: ClusterConfigJSON, config: Config) => {
126 config.remoteHosts = {};
127 Object.keys(clusterConfigJSON.RemoteClusters).forEach(k => { config.remoteHosts[k] = clusterConfigJSON.RemoteClusters[k].Host; });
128 delete config.remoteHosts["*"];
131 export const mockConfig = (config: Partial<Config>): Config => ({
133 keepWebServiceUrl: "",
141 fileViewersConfigUrl: ""
144 const getDefaultConfig = (): WorkbenchConfig => {
146 const envHost = process.env.REACT_APP_ARVADOS_API_HOST;
147 if (envHost !== undefined) {
148 console.warn(`Using default API host ${envHost}.`);
152 console.warn(`No API host was found in the environment. Workbench may not be able to communicate with Arvados components.`);
156 VOCABULARY_URL: undefined,
157 FILE_VIEWERS_CONFIG_URL: undefined,
161 export const ARVADOS_API_PATH = "arvados/v1";
162 export const CLUSTER_CONFIG_URL = "arvados/v1/config";
163 export const getClusterConfigURL = (apiHost: string) => `${window.location.protocol}//${apiHost}/${CLUSTER_CONFIG_URL}?nocache=${(new Date()).getTime()}`;