Skip to content

Commit

Permalink
build: credential ui docker env variables defined in run time (#579)
Browse files Browse the repository at this point in the history
* fix: docker env variables defined in run time

* refactor: simplyfied way to handle env variables on runtime

* fix: removed unnecesary copy instruction from Dockerfile
  • Loading branch information
jorgenavben authored Jul 17, 2024
1 parent daf3fa0 commit 954382c
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 22 deletions.
6 changes: 3 additions & 3 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ services:
build:
context: ./services/credential-server-ui
dockerfile: ./Dockerfile
args:
- REACT_APP_CREDENTIAL_ISSUANCE_SERVER_URL=http://localhost:3001
- REACT_APP_CREDENTIAL_ISSUANCE_SERVER_PORT=3001
restart: unless-stopped
environment:
- REACT_APP_CREDENTIAL_ISSUANCE_SERVER_URL=http://localhost
- REACT_APP_CREDENTIAL_ISSUANCE_SERVER_PORT=3001
ports:
- 3000:80

Expand Down
4 changes: 1 addition & 3 deletions services/credential-server-ui/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@
dist
build
node_modules
npm-debug.log

.env
npm-debug.log
2 changes: 2 additions & 0 deletions services/credential-server-ui/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
REACT_APP_CREDENTIAL_ISSUANCE_SERVER_URL=http://localhost
REACT_APP_CREDENTIAL_ISSUANCE_SERVER_PORT=3001
6 changes: 6 additions & 0 deletions services/credential-server-ui/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Temporary env files
env-config.js

# Override rule from root .gitignore
!.env
24 changes: 17 additions & 7 deletions services/credential-server-ui/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
FROM node:lts-alpine as build

WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .

ARG REACT_APP_CREDENTIAL_ISSUANCE_SERVER_URL
ENV REACT_APP_CREDENTIAL_ISSUANCE_SERVER_URL $REACT_APP_CREDENTIAL_ISSUANCE_SERVER_URL

ARG REACT_APP_CREDENTIAL_ISSUANCE_SERVER_PORT
ENV REACT_APP_CREDENTIAL_ISSUANCE_SERVER_PORT $REACT_APP_CREDENTIAL_ISSUANCE_SERVER_PORT

RUN npm run build


FROM nginx:alpine

# Nginx config
RUN rm -rf /etc/nginx/conf.d
COPY conf /etc/nginx

COPY --from=build /app/build /usr/share/nginx/html

# Copy .env file and shell script to container
WORKDIR /usr/share/nginx/html
COPY ./env.sh .

# Make our shell script executable
RUN chmod +x env.sh

EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

# Start Nginx server
CMD ["/bin/sh", "-c", "/usr/share/nginx/html/env.sh && nginx -g \"daemon off;\""]
13 changes: 13 additions & 0 deletions services/credential-server-ui/conf/conf.d/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
expires -1; # Set it to different value depending on your standard requirements
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
24 changes: 24 additions & 0 deletions services/credential-server-ui/conf/conf.d/gzip.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
gzip on;
gzip_http_version 1.0;
gzip_comp_level 5; # 1-9
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;

# MIME-types
gzip_types
application/atom+xml
application/javascript
application/json
application/rss+xml
application/vnd.ms-fontobject
application/x-font-ttf
application/x-web-app-manifest+json
application/xhtml+xml
application/xml
font/opentype
image/svg+xml
image/x-icon
text/css
text/plain
text/x-component;
6 changes: 6 additions & 0 deletions services/credential-server-ui/env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
echo "window._env_ = {" > ./env-config.js
for var in $(env | grep ^REACT_APP_); do
echo " \"${var%%=*}\": \"${var#*=}\"," >> ./env-config.js
done
echo "};" >> ./env-config.js
2 changes: 2 additions & 0 deletions services/credential-server-ui/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<script src="%PUBLIC_URL%/env-config.js"></script>

<title>Credential Issuance Testing Tool</title>
</head>
<body>
Expand Down
17 changes: 8 additions & 9 deletions services/credential-server-ui/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// eslint-disable-next-line no-undef
const port = process.env.REACT_APP_CREDENTIAL_ISSUANCE_SERVER_PORT
? Number(process.env.PORT)
: 3001;
// eslint-disable-next-line no-undef
const endpoint =
process.env.REACT_APP_CREDENTIAL_ISSUANCE_SERVER_URL ??
`http://127.0.0.1:${port}`;
// @ts-ignore
const port = window._env_?.REACT_APP_CREDENTIAL_ISSUANCE_SERVER_PORT || process.env.REACT_APP_CREDENTIAL_ISSUANCE_SERVER_PORT || "3001";

// @ts-ignore
const url = window._env_?.REACT_APP_CREDENTIAL_ISSUANCE_SERVER_URL || process.env.REACT_APP_CREDENTIAL_ISSUANCE_SERVER_URL || "http://localhost";
const endpoint = `${url}:${port}`;

const config = {
endpoint: endpoint,
endpoints: [endpoint],
Expand All @@ -32,4 +31,4 @@ const config = {
},
};

export { config };
export { config };

0 comments on commit 954382c

Please sign in to comment.