Implement configuration fetching during runtime
authorMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Mon, 16 Jul 2018 09:31:40 +0000 (11:31 +0200)
committerMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Mon, 16 Jul 2018 09:31:40 +0000 (11:31 +0200)
Feature #13817

Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski <michal.klobukowski@contractors.roche.com>

.env
README.md
src/common/api/server-api.ts
src/common/config.ts [new file with mode: 0644]
src/index.tsx

diff --git a/.env b/.env
index 13aaad5027763659d998bb48097d2c04c819fd4f..a523865a6ae43a5b4e8bc670cd28029bfee3870e 100644 (file)
--- a/.env
+++ b/.env
@@ -2,4 +2,5 @@
 # 
 # SPDX-License-Identifier: AGPL-3.0
 
+REACT_APP_ARVADOS_CONFIG_URL=/config.json
 REACT_APP_ARVADOS_API_HOST=https://qr1hi.arvadosapi.com
\ No newline at end of file
index 864a54fa89a122aab17afffa18d9828e3de0c050..998d424662ac4cb69fb75b89904d9955fe5bc25d 100644 (file)
--- a/README.md
+++ b/README.md
@@ -26,12 +26,22 @@ yarn install
 yarn build
 </pre>
 
-### Configuration
+### Build time configuration
 You can customize project global variables using env variables. Default values are placed in the `.env` file.
 
 Example:
 ```
-REACT_APP_ARVADOS_API_HOST=localhost:8000 yarn start
+REACT_APP_ARVADOS_CONFIG_URL=config.json yarn build
+```
+
+### Run time configuration
+The app will fetch runtime configuration when starting. By default it will try to fetch `/config.json`. You can customize this url using build time configuration.
+
+Currently this configuration schema is supported:
+```
+{
+    "API_HOST": "string"
+}
 ```
 
 ### Licensing
index 330ce657e23bb5cb54a21ecf4a5e82d135446348..5beecd48ee7dafabfb34b5e2c1984af964f08498 100644 (file)
@@ -18,3 +18,7 @@ export function setServerApiAuthorizationHeader(token: string) {
 export function removeServerApiAuthorizationHeader() {
     delete serverApi.defaults.headers.common.Authorization;
 }
+
+export const setBaseUrl = (url: string) => {
+    serverApi.defaults.baseURL = url + "/arvados/v1";
+};
diff --git a/src/common/config.ts b/src/common/config.ts
new file mode 100644 (file)
index 0000000..4b4a52a
--- /dev/null
@@ -0,0 +1,23 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import Axios from "../../node_modules/axios";
+
+export const CONFIG_URL = process.env.REACT_APP_ARVADOS_CONFIG_URL || "/config.json";
+
+export interface Config {
+    API_HOST: string;
+}
+
+const defaultConfig: Config = {
+    API_HOST: process.env.REACT_APP_ARVADOS_API_HOST || ""
+};
+
+export const fetchConfig = () => {
+    return Axios
+        .get<Config>(CONFIG_URL + "?nocache=" + (new Date()).getTime())
+        .then(response => response.data)
+        .catch(() => Promise.resolve(defaultConfig));
+};
+
index a06b4851a314d678f175bd8941ea11d14adf5ed4..102249672271bb1c7bd3652a1739f90b91287ee6 100644 (file)
@@ -17,39 +17,36 @@ import { authService } from "./services/services";
 import { getProjectList } from "./store/project/project-action";
 import { MuiThemeProvider } from '@material-ui/core/styles';
 import { CustomTheme } from './common/custom-theme';
-import CommonResourceService from './common/api/common-resource-service';
-import { CollectionResource } from './models/collection';
-import { serverApi } from './common/api/server-api';
-import { ProcessResource } from './models/process';
-
-const history = createBrowserHistory();
-
-const store = configureStore(history);
-
-store.dispatch(authActions.INIT());
-store.dispatch<any>(getProjectList(authService.getUuid()));
-
-// const service = new CommonResourceService<CollectionResource>(serverApi, "collections");
-// service.create({ ownerUuid: "qr1hi-j7d0g-u55bcc7fa5w7v4p", name: "Collection 1 short title"});
-// service.create({ ownerUuid: "qr1hi-j7d0g-u55bcc7fa5w7v4p", name: "Collection 2 long long long title"});
-
-// const processService = new CommonResourceService<ProcessResource>(serverApi, "container_requests");
-// processService.create({ ownerUuid: "qr1hi-j7d0g-u55bcc7fa5w7v4p", name: "Process 1 short title"});
-// processService.create({ ownerUuid: "qr1hi-j7d0g-u55bcc7fa5w7v4p", name: "Process 2 long long long title" });
-
-const App = () =>
-    <MuiThemeProvider theme={CustomTheme}>
-        <Provider store={store}>
-            <ConnectedRouter history={history}>
-                <div>
-                    <Route path="/" component={Workbench} />
-                    <Route path="/token" component={ApiToken} />
-                </div>
-            </ConnectedRouter>
-        </Provider>
-    </MuiThemeProvider>;
-
-ReactDOM.render(
-    <App />,
-    document.getElementById('root') as HTMLElement
-);
+import { fetchConfig } from './common/config';
+import { setBaseUrl } from './common/api/server-api';
+
+fetchConfig()
+    .then(config => {
+
+        setBaseUrl(config.API_HOST);
+
+        const history = createBrowserHistory();
+        const store = configureStore(history);
+
+        store.dispatch(authActions.INIT());
+        store.dispatch<any>(getProjectList(authService.getUuid()));
+
+        const App = () =>
+            <MuiThemeProvider theme={CustomTheme}>
+                <Provider store={store}>
+                    <ConnectedRouter history={history}>
+                        <div>
+                            <Route path="/" component={Workbench} />
+                            <Route path="/token" component={ApiToken} />
+                        </div>
+                    </ConnectedRouter>
+                </Provider>
+            </MuiThemeProvider>;
+
+        ReactDOM.render(
+            <App />,
+            document.getElementById('root') as HTMLElement
+        );
+    });
+
+