Merge branch 'master' into 13817-runtime-app-configuration
authorMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Tue, 17 Jul 2018 10:10:02 +0000 (12:10 +0200)
committerMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Tue, 17 Jul 2018 10:10:02 +0000 (12:10 +0200)
refs #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
src/services/auth-service/auth-service.ts
src/services/services.ts

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
+        );
+    });
+
+
index e953a75d14aabbcd52a2d61fcf32e260d83717f3..5b21a61634be451a75841435e156e265ab0136a7 100644 (file)
@@ -2,8 +2,9 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { API_HOST, serverApi } from "../../common/api/server-api";
+import { API_HOST } from "../../common/api/server-api";
 import { User } from "../../models/user";
+import { AxiosInstance } from "../../../node_modules/axios";
 
 export const API_TOKEN_KEY = 'apiToken';
 export const USER_EMAIL_KEY = 'userEmail';
@@ -23,6 +24,8 @@ export interface UserDetailsResponse {
 
 export default class AuthService {
 
+    constructor(protected serverApi: AxiosInstance) { }
+
     public saveApiToken(token: string) {
         localStorage.setItem(API_TOKEN_KEY, token);
     }
@@ -82,7 +85,7 @@ export default class AuthService {
     }
 
     public getUserDetails = (): Promise<User> => {
-        return serverApi
+        return this.serverApi
             .get<UserDetailsResponse>('/users/current')
             .then(resp => ({
                 email: resp.data.email,
index 143e97bdaf2dbbeff82b3e2cb5b23a691a0505e0..88f6ffaefd46527571d4a0181364a6d0663c03fa 100644 (file)
@@ -7,6 +7,6 @@ import GroupsService from "./groups-service/groups-service";
 import { serverApi } from "../common/api/server-api";
 import ProjectService from "./project-service/project-service";
 
-export const authService = new AuthService();
+export const authService = new AuthService(serverApi);
 export const groupsService = new GroupsService(serverApi);
 export const projectService = new ProjectService(serverApi);