diff --git a/.circleci/config.yml b/.circleci/config.yml index 2a4e1d6..4e47a6d 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -142,7 +142,7 @@ jobs: name: Build Docker local image command: | echo "Building Docker image: local" - docker build -t $DOCKER_ORG/$CIRCLE_PROJECT_REPONAME:local . + docker build -t $DOCKER_ORG/$CIRCLE_PROJECT_REPONAME:local . --build-arg REACT_APP_VERSION=`npm run version --silent` --build-arg REACT_APP_COMMIT=`git rev-parse HEAD` --build-arg PUBLIC_PATH=https://localhost:8080/ - run: name: Save docker image to workspace command: docker save -o /tmp/docker-image.tar $DOCKER_ORG/$CIRCLE_PROJECT_REPONAME:local diff --git a/.env b/.env index 51a3255..8bb515d 100644 --- a/.env +++ b/.env @@ -1,6 +1,8 @@ -REACT_APP_API_BASE_URL=/api REACT_APP_AUTH_ENABLED=true -REACT_APP_MOCK_API=true +REACT_APP_AUTH_API_BASE_URL=/api +REACT_APP_AUTH_MOCK_API=true +REACT_APP_REMOTE_API_BASE_URL=/remote +REACT_APP_REMOTE_MOCK_API=true REMOTE_1_URL=http://localhost:3012 REMOTE_2_URL=http://localhost:3013 DEV_PORT=3010 diff --git a/Dockerfile b/Dockerfile index a74e56b..8e55253 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,15 +20,6 @@ ENV REACT_APP_COMMIT=$REACT_APP_COMMIT ARG PUBLIC_PATH ENV PUBLIC_PATH=$PUBLIC_PATH -# TODO: hard coding these for now since there is no api that -# handles the microfrontend remote locations -ARG REMOTE_1_URL -ENV REMOTE_1_URL=$REMOTE_1_URL - -ARG REMOTE_2_URL -ENV REMOTE_2_URL=$REMOTE_2_URL - - RUN yarn build # Second part, create a config at boostrap via entrypoint and and serve it @@ -41,13 +32,26 @@ COPY --from=0 dist/ . COPY docker/Caddyfile /srv/Caddyfile COPY docker/entrypoint.sh /entrypoint.sh COPY docker/createJSONConfig.sh /createJSONConfig.sh +COPY docker/createRemoteConfig.sh /createRemoteConfig.sh +COPY docker/loadRuntimeConfig.sh /loadRuntimeConfig.sh RUN chmod +x /entrypoint.sh RUN chmod +x /createJSONConfig.sh +RUN chmod +x /createRemoteConfig.sh +RUN chmod +x /loadRuntimeConfig.sh # Provide environment variables for setting endpoints dynamically -ARG API_BASE_URL -ENV API_BASE_URL=$API_BASE_URL +ARG REMOTE_API_BASE_URL +ENV REMOTE_API_BASE_URL=$REMOTE_API_BASE_URL + +ARG REMOTE_MOCK_API +ENV REMOTE_MOCK_API=$REMOTE_MOCK_API + +ARG AUTH_API_BASE_URL +ENV AUTH_API_BASE_URL=$AUTH_API_BASE_URL + +ARG AUTH_MOCK_API +ENV AUTH_MOCK_API=$AUTH_MOCK_API ARG AUTH_ENABLED ENV AUTH_ENABLED=$AUTH_ENABLED @@ -58,8 +62,11 @@ ENV LOGIN_URL=$LOGIN_URL ARG LOGOUT_URL ENV LOGOUT_URL=$LOGOUT_URL -ARG MOCK_API -ENV MOCK_API=$MOCK_API +ARG REMOTE_1_URL +ENV REMOTE_1_URL=$REMOTE_1_URL + +ARG REMOTE_2_URL +ENV REMOTE_2_URL=$REMOTE_2_URL EXPOSE 8080 diff --git a/docker-compose.yaml b/docker-compose.yaml index 127c9d7..dd93de3 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -14,14 +14,16 @@ services: - mojaloop/reporting-hub-bop-shell args: - PUBLIC_PATH=https://localhost:8080/ - - REMOTE_1_URL=https://localhost:8081 - - REMOTE_2_URL=https://localhost:8082 environment: - - API_BASE_URL=/api + - AUTH_API_BASE_URL=/ + - AUTH_MOCK_API=true + - REMOTE_API_BASE_URL=/ + - REMOTE_MOCK_API=false - LOGIN_URL=https://your-login-url - LOGOUT_URL=https://your-logout-url - AUTH_ENABLED=true - - MOCK_API=true + - REMOTE_1_URL=https://localhost:8081 + - REMOTE_2_URL=https://localhost:8082 ports: - "8080:8080" networks: diff --git a/docker/createJSONConfig.sh b/docker/createJSONConfig.sh index a69fa2d..60ca3dc 100644 --- a/docker/createJSONConfig.sh +++ b/docker/createJSONConfig.sh @@ -2,12 +2,14 @@ # Creates the JSON config based on environment variables echo "{ - \"API_BASE_URL\": \"${API_BASE_URL}\", + \"AUTH_API_BASE_URL\": \"${AUTH_API_BASE_URL}\", + \"AUTH_MOCK_API\": \"${AUTH_MOCK_API}\", + \"REMOTE_API_BASE_URL\": \"${REMOTE_API_BASE_URL}\", + \"REMOTE_MOCK_API\": \"${REMOTE_MOCK_API}\", \"AUTH_ENABLED\": \"${AUTH_ENABLED}\", \"LOGIN_URL\": \"${LOGIN_URL}\", - \"LOGOUT_URL\": \"${LOGOUT_URL}\", - \"MOCK_API\": \"${MOCK_API}\" + \"LOGOUT_URL\": \"${LOGOUT_URL}\" }" | jq '.' > config.json # This will exec the CMD from your Dockerfile, i.e. "npm start" -exec "$@" \ No newline at end of file +exec "$@" diff --git a/docker/createRemoteConfig.sh b/docker/createRemoteConfig.sh new file mode 100644 index 0000000..1f4c5a0 --- /dev/null +++ b/docker/createRemoteConfig.sh @@ -0,0 +1,24 @@ +#!/bin/sh + +# Creates the Remotes config based on environment variables +echo "[ + { + \"path\": \"/iam\", + \"label\": \"Roles Microfrontend\", + \"menuComponent\": \"Menu\", + \"appComponent\": \"App\", + \"url\": \"${REMOTE_1_URL}/app.js\", + \"appName\": \"reporting_hub_bop_role_ui\" + }, + { + \"path\": \"/transfers\", + \"label\": \"Transfers Microfrontend\", + \"menuComponent\": \"Menu\", + \"appComponent\": \"App\", + \"url\": \"${REMOTE_2_URL}/app.js\", + \"appName\": \"reporting_hub_bop_trx_ui\" + } +]" | jq '.' > remotes.json + +# This will exec the CMD from your Dockerfile, i.e. "npm start" +exec "$@" diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index 2cc1f40..ff95d93 100644 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -2,6 +2,8 @@ # Run the script before starting the server sh /createJSONConfig.sh +sh /createRemoteConfig.sh +sh /loadRuntimeConfig.sh # This will exec the CMD from your Dockerfile, i.e. "npm start" -exec "$@" \ No newline at end of file +exec "$@" diff --git a/docker/loadRuntimeConfig.sh b/docker/loadRuntimeConfig.sh new file mode 100644 index 0000000..83e5310 --- /dev/null +++ b/docker/loadRuntimeConfig.sh @@ -0,0 +1,5 @@ +#!/bin/bash +sed -i 's#__REMOTE_1_URL__#'"$REMOTE_1_URL"'#g' runtime-env.js +sed -i 's#__REMOTE_1_URL__#'"$REMOTE_1_URL"'#g' index.html + +exec "$@" diff --git a/package.json b/package.json index 8fda666..dba68a1 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,7 @@ "bootstrap-icons": "^1.5.0", "classnames": "^2.2.6", "connected-react-router": "^6.8.0", + "copy-webpack-plugin": "^9.0.1", "css-loader": "4.2.1", "dotenv": "^10.0.0", "dotenv-webpack": "^7.0.3", diff --git a/public/index.html b/public/index.html index 4397b5d..35010bc 100644 --- a/public/index.html +++ b/public/index.html @@ -2,6 +2,8 @@
+ + diff --git a/public/runtime-env.js b/public/runtime-env.js new file mode 100644 index 0000000..5a57a13 --- /dev/null +++ b/public/runtime-env.js @@ -0,0 +1,5 @@ +// public/env.js +// https://medium.com/@hasniarif/how-to-handle-runtime-environment-variables-with-react-ec809cb07831 +window.env = { + REMOTE_1_URL: '__REMOTE_1_URL__', +}; diff --git a/src/App/sagas.ts b/src/App/sagas.ts index 0bcd024..9c78bfe 100644 --- a/src/App/sagas.ts +++ b/src/App/sagas.ts @@ -6,7 +6,6 @@ import { actions } from './slice'; function* requestRemotes() { try { const { status, data } = yield call(apis.remotes.read, {}); - if (is200(status)) { yield put(actions.requestRemotesSuccess(data)); } else { diff --git a/src/Config/build.ts b/src/Config/build.ts index 830949b..751b2cf 100644 --- a/src/Config/build.ts +++ b/src/Config/build.ts @@ -8,18 +8,26 @@ export default async (): Promise