Merge branch '15424-wb2-welcome-page' refs #15424
[arvados-workbench2.git] / src / common / config.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import Axios from "axios";
6
7 export const WORKBENCH_CONFIG_URL = process.env.REACT_APP_ARVADOS_CONFIG_URL || "/config.json";
8
9 interface WorkbenchConfig {
10     API_HOST: string;
11     VOCABULARY_URL?: string;
12     FILE_VIEWERS_CONFIG_URL?: string;
13 }
14
15 export interface ClusterConfigJSON {
16     ClusterID: string;
17     RemoteClusters: {
18         [key: string]: {
19             ActivateUsers: boolean
20             Host: string
21             Insecure: boolean
22             Proxy: boolean
23             Scheme: string
24         }
25     };
26     Services: {
27         Controller: {
28             ExternalURL: string
29         }
30         Workbench1: {
31             ExternalURL: string
32         }
33         Workbench2: {
34             ExternalURL: string
35         }
36         Websocket: {
37             ExternalURL: string
38         }
39         WebDAV: {
40             ExternalURL: string
41         }
42     };
43     Workbench: {
44         ArvadosDocsite: string;
45         VocabularyURL: string;
46         FileViewersConfigURL: string;
47         WelcomePageHTML: string;
48     };
49 }
50
51 export class Config {
52     baseUrl: string;
53     keepWebServiceUrl: string;
54     remoteHosts: {
55         [key: string]: string
56     };
57     rootUrl: string;
58     uuidPrefix: string;
59     websocketUrl: string;
60     workbenchUrl: string;
61     workbench2Url: string;
62     vocabularyUrl: string;
63     fileViewersConfigUrl: string;
64     clusterConfig: ClusterConfigJSON;
65 }
66
67 export const fetchConfig = () => {
68     return Axios
69         .get<WorkbenchConfig>(WORKBENCH_CONFIG_URL + "?nocache=" + (new Date()).getTime())
70         .then(response => response.data)
71         .catch(() => {
72             console.warn(`There was an exception getting the Workbench config file at ${WORKBENCH_CONFIG_URL}. Using defaults instead.`);
73             return Promise.resolve(getDefaultConfig());
74         })
75         .then(workbenchConfig => {
76             if (workbenchConfig.API_HOST === undefined) {
77                 throw new Error(`Unable to start Workbench. API_HOST is undefined in ${WORKBENCH_CONFIG_URL} or the environment.`);
78             }
79             return Axios.get<ClusterConfigJSON>(getClusterConfigURL(workbenchConfig.API_HOST)).then(response => {
80                 const config = new Config();
81                 const clusterConfigJSON = response.data;
82                 const warnLocalConfig = (varName: string) => console.warn(
83                     `A value for ${varName} was found in ${WORKBENCH_CONFIG_URL}. To use the Arvados centralized configuration instead, \
84 remove the entire ${varName} entry from ${WORKBENCH_CONFIG_URL}`);
85
86                 // Check if the workbench config has an entry for vocabulary and file viewer URLs
87                 // If so, use these values (even if it is an empty string), but print a console warning.
88                 // Otherwise, use the cluster config.
89                 let fileViewerConfigUrl;
90                 if (workbenchConfig.FILE_VIEWERS_CONFIG_URL !== undefined) {
91                     warnLocalConfig("FILE_VIEWERS_CONFIG_URL");
92                     fileViewerConfigUrl = workbenchConfig.FILE_VIEWERS_CONFIG_URL;
93                 }
94                 else {
95                     fileViewerConfigUrl = clusterConfigJSON.Workbench.FileViewersConfigURL || "/file-viewers-example.json";
96                 }
97                 config.fileViewersConfigUrl = fileViewerConfigUrl;
98
99                 let vocabularyUrl;
100                 if (workbenchConfig.VOCABULARY_URL !== undefined) {
101                     warnLocalConfig("VOCABULARY_URL");
102                     vocabularyUrl = workbenchConfig.VOCABULARY_URL;
103                 }
104                 else {
105                     vocabularyUrl = clusterConfigJSON.Workbench.VocabularyURL || "/vocabulary-example.json";
106                 }
107                 config.vocabularyUrl = vocabularyUrl;
108
109                 config.rootUrl = clusterConfigJSON.Services.Controller.ExternalURL;
110                 config.baseUrl = `${config.rootUrl}/${ARVADOS_API_PATH}`;
111                 config.uuidPrefix = clusterConfigJSON.ClusterID;
112                 config.websocketUrl = clusterConfigJSON.Services.Websocket.ExternalURL;
113                 config.workbench2Url = clusterConfigJSON.Services.Workbench2.ExternalURL;
114                 config.workbenchUrl = clusterConfigJSON.Services.Workbench1.ExternalURL;
115                 config.keepWebServiceUrl = clusterConfigJSON.Services.WebDAV.ExternalURL;
116                 config.clusterConfig = clusterConfigJSON;
117                 mapRemoteHosts(clusterConfigJSON, config);
118
119                 return { config, apiHost: workbenchConfig.API_HOST };
120             });
121         });
122 };
123
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["*"];
129 };
130
131 export const mockClusterConfigJSON = (config: Partial<ClusterConfigJSON>): ClusterConfigJSON => ({
132     ClusterID: "",
133     RemoteClusters: {},
134     Services: {
135         Controller: { ExternalURL: "" },
136         Workbench1: { ExternalURL: "" },
137         Workbench2: { ExternalURL: "" },
138         Websocket: { ExternalURL: "" },
139         WebDAV: { ExternalURL: "" },
140     },
141     Workbench: {
142         ArvadosDocsite: "",
143         VocabularyURL: "",
144         FileViewersConfigURL: "",
145         WelcomePageHTML: "",
146     },
147     ...config
148 });
149
150 export const mockConfig = (config: Partial<Config>): Config => ({
151     baseUrl: "",
152     keepWebServiceUrl: "",
153     remoteHosts: {},
154     rootUrl: "",
155     uuidPrefix: "",
156     websocketUrl: "",
157     workbenchUrl: "",
158     workbench2Url: "",
159     vocabularyUrl: "",
160     fileViewersConfigUrl: "",
161     clusterConfig: mockClusterConfigJSON({}),
162     ...config
163 });
164
165 const getDefaultConfig = (): WorkbenchConfig => {
166     let apiHost = "";
167     const envHost = process.env.REACT_APP_ARVADOS_API_HOST;
168     if (envHost !== undefined) {
169         console.warn(`Using default API host ${envHost}.`);
170         apiHost = envHost;
171     }
172     else {
173         console.warn(`No API host was found in the environment. Workbench may not be able to communicate with Arvados components.`);
174     }
175     return {
176         API_HOST: apiHost,
177         VOCABULARY_URL: undefined,
178         FILE_VIEWERS_CONFIG_URL: undefined,
179     };
180 };
181
182 export const ARVADOS_API_PATH = "arvados/v1";
183 export const CLUSTER_CONFIG_URL = "arvados/v1/config";
184 export const getClusterConfigURL = (apiHost: string) => `${window.location.protocol}//${apiHost}/${CLUSTER_CONFIG_URL}?nocache=${(new Date()).getTime()}`;