From 22152102405a1407355d42a32385efd0d83bbc9c Mon Sep 17 00:00:00 2001 From: Michal Klobukowski Date: Mon, 16 Jul 2018 11:31:40 +0200 Subject: [PATCH] Implement configuration fetching during runtime Feature #13817 Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski --- .env | 1 + README.md | 14 ++++++-- src/common/api/server-api.ts | 4 +++ src/common/config.ts | 23 ++++++++++++ src/index.tsx | 69 +++++++++++++++++------------------- 5 files changed, 73 insertions(+), 38 deletions(-) create mode 100644 src/common/config.ts diff --git a/.env b/.env index 13aaad50..a523865a 100644 --- 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 diff --git a/README.md b/README.md index 864a54fa..998d4246 100644 --- a/README.md +++ b/README.md @@ -26,12 +26,22 @@ yarn install yarn build -### 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 diff --git a/src/common/api/server-api.ts b/src/common/api/server-api.ts index 330ce657..5beecd48 100644 --- a/src/common/api/server-api.ts +++ b/src/common/api/server-api.ts @@ -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 index 00000000..4b4a52a3 --- /dev/null +++ b/src/common/config.ts @@ -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_URL + "?nocache=" + (new Date()).getTime()) + .then(response => response.data) + .catch(() => Promise.resolve(defaultConfig)); +}; + diff --git a/src/index.tsx b/src/index.tsx index a06b4851..10224967 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -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(getProjectList(authService.getUuid())); - -// const service = new CommonResourceService(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(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 = () => - - - -
- - -
-
-
-
; - -ReactDOM.render( - , - 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(getProjectList(authService.getUuid())); + + const App = () => + + + +
+ + +
+
+
+
; + + ReactDOM.render( + , + document.getElementById('root') as HTMLElement + ); + }); + + -- 2.30.2