From f74c10d4aa1daaf519601e1678b4d3dd481e4dcb Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Mon, 6 Jan 2025 15:04:47 +0100 Subject: [PATCH] fix: availability of env variables --- .env.example | 5 ++++- .env.production | 4 ++++ Dockerfile | 5 ++++- README.md | 9 ++++++--- entrypoint.sh | 20 ++++++++++++++++++++ src/components/FormBottomBar.vue | 11 ++++++++--- src/components/UserMenu.vue | 2 +- src/config.ts | 13 +++++++++++++ src/serverCom.ts | 3 ++- vite.config.ts | 16 ++++++++++++++++ 10 files changed, 78 insertions(+), 10 deletions(-) create mode 100644 .env.production create mode 100644 entrypoint.sh create mode 100644 src/config.ts diff --git a/.env.example b/.env.example index f968fcf..3cb19a1 100644 --- a/.env.example +++ b/.env.example @@ -1 +1,4 @@ -SERVER_ADDRESS = serveradress +VITE_SERVER_ADDRESS = serveradress #ohne https und ohne pfad +VITE_IMPRINT_LINK = https://mywebsite-imprint-url +VITE_PRIVACY_LINK = https://mywebsite-privacy-url +VITE_CUSTOM_LOGIN_MESSAGE = betrieben von xy diff --git a/.env.production b/.env.production new file mode 100644 index 0000000..5ecf896 --- /dev/null +++ b/.env.production @@ -0,0 +1,4 @@ +VITE_SERVER_ADDRESS = __SERVERADDRESS__ +VITE_IMPRINT_LINK = __IMPRINTLINK__ +VITE_PRIVACY_LINK = __PRIVACYLINK__ +VITE_CUSTOM_LOGIN_MESSAGE = __CUSTOMLOGINMESSAGE__ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index c6de657..ce8bb1b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,4 +19,7 @@ COPY ./nginx.conf /etc/nginx/nginx.conf EXPOSE 80 -CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file +COPY ./entrypoint.sh /entrypoint.sh +RUN apk add --no-cache dos2unix +RUN dos2unix /entrypoint.sh && chmod +x /entrypoint.sh +ENTRYPOINT [ "/entrypoint.sh" ] \ No newline at end of file diff --git a/README.md b/README.md index e8eb496..5817b17 100644 --- a/README.md +++ b/README.md @@ -31,10 +31,13 @@ services: restart: unless-stopped #environment: - # - SERVER_ADRESS= # wichtig: ohne https:// bzw http:// + # - SERVERADRESS= # wichtig: ohne https:// bzw http:// und ohne pfad + # - IMPRINTLINK=https://mywebsite-imprint-url + # - PRIVACYLINK=https://mywebsite-privacy-url + # - CUSTOMLOGINMESSAGE=betrieben von xy #volumes: - # - /myfavicon.png:/app/public/favicon.png - # - /mylogo.png:/app/public/logo.png + # - /myfavicon.png:/usr/share/nginx/html/favicon.png + # - /mylogo.png:/usr/share/nginx/html/Logo.png ``` Wenn keine Server-Adresse angegeben wird, wird versucht das Backend unter der URL des Frontends zu erreichen. Dazu muss das Backend auf der gleichen URL wie das Frontend laufen. Zur Unterscheidung von Frontend und Backend bei gleicher URL müssen alle Anfragen mit dem PathPrefix `/api` an das Backend weitergeleitet werden. diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..7c29fdb --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,20 @@ +#!/bin/sh + +keys="SERVERADDRESS IMPRINTLINK PRIVACYLINK CUSTOMLOGINMESSAGE" + +# Replace env vars in files served by NGINX +for file in /usr/share/nginx/html/assets/config-*.js +do + echo "Processing $file ..."; + for key in $keys + do + # Get environment variable + value=$(eval echo "\$$key") + echo "replace $key by $value" + + # replace __[variable_name]__ value with environment variable + sed -i 's|__'"$key"'__|'"$value"'|g' $file + done +done + +nginx -g 'daemon off;' \ No newline at end of file diff --git a/src/components/FormBottomBar.vue b/src/components/FormBottomBar.vue index 3f96f55..6d46114 100644 --- a/src/components/FormBottomBar.vue +++ b/src/components/FormBottomBar.vue @@ -1,9 +1,14 @@ \ No newline at end of file + + + \ No newline at end of file diff --git a/src/components/UserMenu.vue b/src/components/UserMenu.vue index 4b763ba..51aefc4 100644 --- a/src/components/UserMenu.vue +++ b/src/components/UserMenu.vue @@ -26,7 +26,7 @@ - + diff --git a/src/config.ts b/src/config.ts new file mode 100644 index 0000000..856263a --- /dev/null +++ b/src/config.ts @@ -0,0 +1,13 @@ +export interface Config { + server_address: string; + imprint_link: string; + privacy_link: string; + custom_login_message: string; +} + +export const config: Config = { + server_address: import.meta.env.VITE_SERVER_ADDRESS, + imprint_link: import.meta.env.VITE_IMPRINT_LINK, + privacy_link: import.meta.env.VITE_PRIVACY_LINK, + custom_login_message: import.meta.env.VITE_CUSTOM_LOGIN_MESSAGE, +}; diff --git a/src/serverCom.ts b/src/serverCom.ts index cc0e4bb..6e5c131 100644 --- a/src/serverCom.ts +++ b/src/serverCom.ts @@ -3,10 +3,11 @@ import { isAuthenticatedPromise, type Payload } from "./router/authGuard"; import router from "./router"; import { useNotificationStore } from "./stores/notification"; import { EventSourcePolyfill } from "event-source-polyfill"; +import { config } from "./config"; let devMode = process.env.NODE_ENV === "development"; -let host = devMode ? "localhost:5000" : process.env.SERVER_ADDRESS || ""; +let host = devMode ? "localhost:5000" : (config.server_address ?? ""); let url = devMode ? "http://" + host : (host ? "https://" : "") + host; const http = axios.create({ diff --git a/vite.config.ts b/vite.config.ts index 8b8b6a3..18ded37 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -41,4 +41,20 @@ export default defineConfig({ $: fileURLToPath(new URL("./docs", import.meta.url)), }, }, + build: { + rollupOptions: { + output: { + // Define the main output bundle (e.g., for your application) + entryFileNames: "[name]-[hash].js", + // Define the directory where the main bundle should be output + dir: "dist", + // Create a separate output bundle for your specific file + manualChunks(id) { + if (id.endsWith("src/config.ts")) { + return "config"; // This will create a separate bundle for config-[hash].js + } + }, + }, + }, + }, });