From c2e9e098ed9222ef189f8718c1cc4ecd598ae095 Mon Sep 17 00:00:00 2001 From: Ben Sherriff Date: Mon, 14 Apr 2025 23:37:47 -0400 Subject: [PATCH] Load env var for api url into ui for production --- nginx/Dockerfile | 4 ++-- nginx/scripts/{01-configure_ssh.sh => 01-setup.sh} | 1 + ui/index.html | 1 + ui/public/config.template.js | 3 +++ ui/src/lib/index.ts | 3 ++- 5 files changed, 9 insertions(+), 3 deletions(-) rename nginx/scripts/{01-configure_ssh.sh => 01-setup.sh} (85%) create mode 100644 ui/public/config.template.js diff --git a/nginx/Dockerfile b/nginx/Dockerfile index 6210e86..c1e8a1e 100644 --- a/nginx/Dockerfile +++ b/nginx/Dockerfile @@ -20,11 +20,11 @@ ENV environment=${environment} COPY nginx/nginx.conf /etc/nginx/nginx.conf COPY nginx/templates/ /templates/ -COPY nginx/scripts/01-configure_ssh.sh /docker-entrypoint.d/01-configure_ssh.sh +COPY nginx/scripts/01-setup.sh /docker-entrypoint.d/01-setup.sh COPY --from=ui-builder /builder/dist /dist RUN echo "Copying UI assets into nginx html folder..."; \ mkdir -p /usr/share/nginx/html && \ cp -R /dist/* /usr/share/nginx/html; -RUN chmod +x /docker-entrypoint.d/01-configure_ssh.sh +RUN chmod +x /docker-entrypoint.d/01-setup.sh diff --git a/nginx/scripts/01-configure_ssh.sh b/nginx/scripts/01-setup.sh similarity index 85% rename from nginx/scripts/01-configure_ssh.sh rename to nginx/scripts/01-setup.sh index 8706288..e48aa22 100755 --- a/nginx/scripts/01-configure_ssh.sh +++ b/nginx/scripts/01-setup.sh @@ -13,6 +13,7 @@ fi if [ "$ENVIRONMENT" = "production" ]; then export UI_ROOT="/usr/share/nginx/html" + envsubst '${VITE_API_URL}' < /usr/share/nginx/html/config.template.js > /usr/share/nginx/html/config.js echo "Setting production root to ${UI_ROOT}" else export UI_ROOT="/home" diff --git a/ui/index.html b/ui/index.html index 9119d0b..feddfe0 100644 --- a/ui/index.html +++ b/ui/index.html @@ -10,6 +10,7 @@
+ diff --git a/ui/public/config.template.js b/ui/public/config.template.js new file mode 100644 index 0000000..25c1e46 --- /dev/null +++ b/ui/public/config.template.js @@ -0,0 +1,3 @@ +window.__CONFIG__ = { + API_URL: '${VITE_API_URL}' +}; \ No newline at end of file diff --git a/ui/src/lib/index.ts b/ui/src/lib/index.ts index 304afb4..73a45ef 100644 --- a/ui/src/lib/index.ts +++ b/ui/src/lib/index.ts @@ -1,4 +1,5 @@ -const baseUrl = import.meta.env.VITE_API_URL || 'http://localhost:8080/api'; +// @ts-expect-error The window.__CONFIG__ only exists in production +const baseUrl = window.__CONFIG__?.API_URL || import.meta.env.VITE_API_URL || 'http://localhost:8080/api'; export async function getRequest(endpoint: string, params: Record = {}): Promise { Object.keys(params).forEach((key) => params[key] === undefined && delete params[key]);