From e7dd75bc2b35e698e6de69dd88812e4786dd66a1 Mon Sep 17 00:00:00 2001 From: "Peter.Morton" Date: Wed, 18 Oct 2023 17:51:49 -0500 Subject: [PATCH] Enable container environment variables for config. Fixes #1 --- .env.development | 2 +- .env.production | 4 +-- 40-envsub-on-vite.sh | 37 ++++++++++++++++++++++++++ Dockerfile | 5 ++++ default.conf.template | 2 +- examples/repeatable-docker-compose.yml | 7 ++++- src/app.config.js | 7 +++++ src/helpers/index.js | 6 ++--- src/router/index.js | 5 +++- src/views/InteractionsFlowView.vue | 3 ++- src/views/SearchByReferenceView.vue | 5 ++-- 11 files changed, 70 insertions(+), 13 deletions(-) create mode 100644 40-envsub-on-vite.sh create mode 100644 src/app.config.js diff --git a/.env.development b/.env.development index 80f4807..22df842 100644 --- a/.env.development +++ b/.env.development @@ -1,2 +1,2 @@ -VITE_EO_SERVICES_URL=http://localhost:3000 +VITE_API_BASE_URL=http://localhost:3000 VITE_ROUTER_BASE=/ diff --git a/.env.production b/.env.production index 6654ed9..9c65154 100644 --- a/.env.production +++ b/.env.production @@ -1,2 +1,2 @@ -VITE_EO_SERVICES_URL=https://eo-services.mortons.site -VITE_ROUTER_BASE=/eo-services +VITE_API_BASE_URL=VITE_API_BASE_URL +VITE_ROUTER_BASE=VITE_ROUTER_BASE diff --git a/40-envsub-on-vite.sh b/40-envsub-on-vite.sh new file mode 100644 index 0000000..4414d5d --- /dev/null +++ b/40-envsub-on-vite.sh @@ -0,0 +1,37 @@ +#!/bin/sh +# @see https://stackoverflow.com/questions/18185305/storing-bash-output-into-a-variable-using-eval +set -e + +ROOT_DIR=/app/eo-services +ME=$(basename $0) + +entrypoint_log() { + if [ -z "${NGINX_ENTRYPOINT_QUIET_LOGS:-}" ]; then + echo "$@" + fi +} + +auto_envsubst() { + + # Replace env vars in JavaScript files + echo "Replacing env constants in JS" + + keys="VITE_ROUTER_BASE + VITE_API_BASE_URL" + + for file in $ROOT_DIR/assets/index*.js* ; + do + echo "Processing $file ..."; + for key in $keys + do + value=$(eval echo \$$key) + echo "replace $key by $value" + sed -i 's#'"$key"'#'"$value"'#g' $file + done + done +} + +auto_envsubst + +exit 0 + diff --git a/Dockerfile b/Dockerfile index 6a4571b..d838863 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,5 +13,10 @@ COPY --from=build-stage /app/dist /app/eo-services COPY nginx.conf /etc/nginx/nginx.conf COPY default.conf.template /etc/nginx/templates/default.conf.template COPY headers.js /etc/nginx/headers.js + +COPY ./40-envsub-on-vite.sh /docker-entrypoint.d +RUN chmod u+x /docker-entrypoint.d/40-envsub-on-vite.sh + EXPOSE 80 + CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/default.conf.template b/default.conf.template index 62e40a3..889f0b5 100644 --- a/default.conf.template +++ b/default.conf.template @@ -12,7 +12,7 @@ server { location /mirror { internal; - proxy_pass ${AUTH_PROXY_PASS}; + proxy_pass ${VITE_API_BASE_URL}/auth; proxy_set_header X-Original-URI $request_uri; } diff --git a/examples/repeatable-docker-compose.yml b/examples/repeatable-docker-compose.yml index e40db80..1095253 100644 --- a/examples/repeatable-docker-compose.yml +++ b/examples/repeatable-docker-compose.yml @@ -10,11 +10,16 @@ services: image: registry.mortons.site/eo-services:latest ports: - "3000:3000" + dns: + - "8.8.8.8" eo-services-client: environment: TZ: America/Chicago - AUTH_PROXY_PASS: https://eo-services.mortons.site/api/auth + VITE_API_BASE_URL: https://em28.verint.live/eo-services/api + VITE_ROUTER_BASE: /eo-services/ image: registry.mortons.site/eo-services-client:latest ports: - "18080:80" + dns: + - "8.8.8.8" \ No newline at end of file diff --git a/src/app.config.js b/src/app.config.js new file mode 100644 index 0000000..883c849 --- /dev/null +++ b/src/app.config.js @@ -0,0 +1,7 @@ +export const apiBaseUrl = import.meta.env.VITE_API_BASE_URL +export const routerBase = import.meta.env.VITE_ROUTER_BASE + +export default { + apiBaseUrl, + routerBase, +} \ No newline at end of file diff --git a/src/helpers/index.js b/src/helpers/index.js index 88906db..87d509a 100644 --- a/src/helpers/index.js +++ b/src/helpers/index.js @@ -1,3 +1,5 @@ +import { apiBaseUrl } from '../app.config.js'; + export function getAuthKeyFromProperties(props) { var authKey; @@ -27,9 +29,7 @@ export function getTenantProperty(key, props) { var authKey = getAuthKeyFromProperties(props); console.log(`Fetching TPS Property [${key}]`); fetch( - `${ - import.meta.env.VITE_EO_SERVICES_URL - }/api/tps?propertyName=${key}&authKey=${authKey}`, + `${apiBaseUrl}/tps?propertyName=${key}&authKey=${authKey}`, { credentials: "include", // fetch won't send cookies unless you set credentials } diff --git a/src/router/index.js b/src/router/index.js index 7c81250..e8cc973 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -1,4 +1,5 @@ import { createRouter, createWebHistory } from "vue-router"; +import { routerBase } from '../app.config.js'; import HomeView from "../views/HomeView.vue"; import AboutView from "../views/AboutView.vue"; import DebugFrameView from "../views/DebugFrameView.vue"; @@ -62,9 +63,11 @@ const routes = [ }, ]; +console.log(`mounting router on ${routerBase}`) + const router = createRouter({ history: createWebHistory(), - base: `${import.meta.env.VITE_ROUTER_BASE}`, + base: routerBase, routes, }); diff --git a/src/views/InteractionsFlowView.vue b/src/views/InteractionsFlowView.vue index 100c972..36a267b 100644 --- a/src/views/InteractionsFlowView.vue +++ b/src/views/InteractionsFlowView.vue @@ -1,4 +1,5 @@