Skip to content

Commit

Permalink
feat: override network through env variables after build
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Sep 16, 2023
1 parent fe34dcc commit 5d442c0
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
!.postcssrc.js
!babel.config.js
!vue.config.js
!docker/nginx.conf
!docker
7 changes: 2 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ RUN npm ci --legacy-peer-deps

COPY . .

ARG VUE_APP_NETWORK_NAME
ARG VUE_APP_NODE_URL
ARG VUE_APP_MDW_URL
ARG VUE_APP_EXPLORER_URL
ARG VUE_APP_COMPILER_URL
ARG VUE_APP_BACKEND_URL

# TODO: remove legacy openssl after updating @vue/cli
RUN NODE_OPTIONS=--openssl-legacy-provider npm run build

FROM nginx:1.24-alpine
COPY docker/nginx.conf /etc/nginx/nginx.conf
COPY docker/override-env.sh /docker-entrypoint.d
COPY --from=aepp-aepp-base-build /app/dist /usr/share/nginx/html
RUN cp /usr/share/nginx/html/index.html /usr/share/nginx/html/index.template.html
4 changes: 4 additions & 0 deletions docker/override-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
set -e

envsubst < /usr/share/nginx/html/index.template.html > /usr/share/nginx/html/index.html
10 changes: 10 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
<meta name="description" content="The æternity blockchain wallet allows users to store, send, and receive æternity coins. The wallet also features an æpps (applications running on the æternity blockchain) browser.">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no, viewport-fit=cover" />

<script>
window.overrideNetwork = {
name: '$VUE_APP_NETWORK_NAME',
url: '$VUE_APP_NODE_URL',
middlewareUrl: '$VUE_APP_MIDDLEWARE_URL',
explorerUrl: '$VUE_APP_EXPLORER_URL',
compilerUrl: '$VUE_APP_COMPILER_URL',
};
</script>

<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<% googleFontsUrl = 'https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@300;400;500;600;700&family=Inter:wght@400;500;600;700&display=swap' %>
Expand Down
25 changes: 11 additions & 14 deletions src/lib/networksRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,21 @@ const testNetwork = {
compilerUrl: 'https://compiler.aepps.com',
};

let networks = process.env.NODE_ENV === 'production' ? [
mainNetwork,
testNetwork,
] : [
testNetwork,
mainNetwork,
];

export const defaultNetwork = process.env.VUE_APP_NETWORK_NAME ? {
const envNetwork = {
name: process.env.VUE_APP_NETWORK_NAME,
url: process.env.VUE_APP_NODE_URL,
middlewareUrl: process.env.VUE_APP_MDW_URL,
middlewareUrl: process.env.VUE_APP_MIDDLEWARE_URL,
explorerUrl: process.env.VUE_APP_EXPLORER_URL,
compilerUrl: process.env.VUE_APP_COMPILER_URL,
} : mainNetwork;
};

if (process.env.VUE_APP_NETWORK_NAME) {
networks = [defaultNetwork];
}
const networks = (() => {
if (!['', '$VUE_APP_NETWORK_NAME'].includes(window.overrideNetwork.name)) {
return [window.overrideNetwork];
}
if (envNetwork.name) return [envNetwork];
if (process.env.NODE_ENV === 'production') return [mainNetwork, testNetwork];
return [testNetwork, mainNetwork];
})();

export default Object.freeze(networks.map(Object.freeze));
6 changes: 3 additions & 3 deletions src/store/modules/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import Vue from 'vue';
import { update, mergeWith } from 'lodash-es';
import networksRegistry, { defaultNetwork } from '../../lib/networksRegistry';
import networksRegistry from '../../lib/networksRegistry';
import { genRandomBuffer } from '../utils';

const getAppByHost = (apps, appHost) => apps.find(({ host }) => host === appHost);
Expand All @@ -23,10 +23,10 @@ export default {
getters: {
networks: ({ customNetworks }) => [
...networksRegistry,
...customNetworks.map((network) => ({ ...defaultNetwork, ...network, custom: true })),
...customNetworks.map((network) => ({ ...networksRegistry[0], ...network, custom: true })),
],
currentNetwork: ({ sdkUrl }, { networks }) => networks.find(({ url }) => url === sdkUrl) || {
...defaultNetwork,
...networksRegistry[0],
name: sdkUrl,
url: sdkUrl,
},
Expand Down

0 comments on commit 5d442c0

Please sign in to comment.