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 Aug 11, 2023
1 parent e87efd4 commit e828556
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 30 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
9 changes: 2 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,11 @@ 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-network.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
33 changes: 33 additions & 0 deletions docker/override-network.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/sh
set -e

if [ -z $VUE_APP_NETWORK_NAME ]
then
echo Skipping network override because VUE_APP_NETWORK_NAME is not defined
exit
fi

for name in VUE_APP_NODE_URL VUE_APP_MIDDLEWARE_URL VUE_APP_EXPLORER_URL VUE_APP_COMPILER_URL
do
value=$(eval echo \$$name)
if [ -z $value ]
then
echo Expected $name to be defined
exit 1
fi
done

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=$(echo $script | tr '\n' ' ')

cat /usr/share/nginx/html/index.template.html \
| sed -e "s|// global configuration placeholder|$script|" \
> /usr/share/nginx/html/index.html
2 changes: 2 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<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>// global configuration placeholder</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
22 changes: 3 additions & 19 deletions src/lib/networksRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,8 @@ 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 ? {
name: process.env.VUE_APP_NETWORK_NAME,
url: process.env.VUE_APP_NODE_URL,
middlewareUrl: process.env.VUE_APP_MDW_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 = (window.overrideNetwork && [window.overrideNetwork])
?? (process.env.NODE_ENV === 'production'
? [mainNetwork, testNetwork] : [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 e828556

Please sign in to comment.