From ec2be36bb2d015b87d507e33421bd3ac57cb0d03 Mon Sep 17 00:00:00 2001 From: etiennecollin Date: Wed, 13 Aug 2025 23:09:43 -0400 Subject: [PATCH] fix: made wifi environment variables accessible to frontend - Also reverted change in run_wrapper.sh where backend variables where overwritten. This is simply not an issue as variables are baked-in at build time. --- Dockerfile | 5 +++++ frontend/src/app/layout.tsx | 4 ++++ frontend/src/types/runtime.ts | 7 +++++++ frontend/src/utils/runtimeConfig.ts | 4 ++++ frontend/src/utils/wifi.ts | 12 ++++++++---- scripts/entrypoint.sh | 25 +++++++++++++++++++++++++ scripts/run_wrapper.sh | 3 +-- 7 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 frontend/src/types/runtime.ts create mode 100644 frontend/src/utils/runtimeConfig.ts create mode 100644 scripts/entrypoint.sh diff --git a/Dockerfile b/Dockerfile index 9d5d2ae..21e19cf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -81,6 +81,10 @@ WORKDIR /app RUN addgroup -g 1001 -S appgroup && \ adduser -S appuser -u 1001 -G appgroup +# Copy entrypoint script +COPY ./scripts/entrypoint.sh ./ +RUN chmod +x entrypoint.sh + # Copy run wrapper script COPY ./scripts/run_wrapper.sh ./ RUN chmod +x run_wrapper.sh @@ -108,4 +112,5 @@ ENV BACKEND_BIND_PORT="8080" HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \ CMD /usr/local/bin/healthcheck.sh +ENTRYPOINT ["./entrypoint.sh"] CMD ["./run_wrapper.sh"] diff --git a/frontend/src/app/layout.tsx b/frontend/src/app/layout.tsx index 5fd8849..99ed7be 100644 --- a/frontend/src/app/layout.tsx +++ b/frontend/src/app/layout.tsx @@ -18,6 +18,10 @@ export default function RootLayout({ }>) { return ( + + {/* Load runtime config */} + + {children} ); diff --git a/frontend/src/types/runtime.ts b/frontend/src/types/runtime.ts new file mode 100644 index 0000000..89976db --- /dev/null +++ b/frontend/src/types/runtime.ts @@ -0,0 +1,7 @@ +declare global { + interface Window { + __RUNTIME_CONFIG__?: { [key: string]: string | undefined }; + } +} + +export {}; diff --git a/frontend/src/utils/runtimeConfig.ts b/frontend/src/utils/runtimeConfig.ts new file mode 100644 index 0000000..e244422 --- /dev/null +++ b/frontend/src/utils/runtimeConfig.ts @@ -0,0 +1,4 @@ +export const getRuntimeConfig = (): Record => { + if (typeof window === "undefined") return {}; + return window.__RUNTIME_CONFIG__ ?? {}; +}; diff --git a/frontend/src/utils/wifi.ts b/frontend/src/utils/wifi.ts index 7d8eac5..d2cfcb5 100644 --- a/frontend/src/utils/wifi.ts +++ b/frontend/src/utils/wifi.ts @@ -1,3 +1,5 @@ +import { getRuntimeConfig } from "@/utils/runtimeConfig"; + type WifiType = "WPA" | "WEP" | "nopass"; export interface WifiConfig { @@ -8,10 +10,12 @@ export interface WifiConfig { } export function generateWifiConfig(): WifiConfig { - const ssid = process.env.NEXT_WIFI_SSID; - const password = process.env.NEXT_WIFI_PASSWORD; - const type = process.env.NEXT_WIFI_TYPE; - const hidden = process.env.NEXT_WIFI_HIDDEN; + const { + WIFI_SSID: ssid, + WIFI_PASSWORD: password, + WIFI_TYPE: type, + WIFI_HIDDEN: hidden, + } = getRuntimeConfig(); if (ssid == null) { throw "No SSID provided, use the environment variable WIFI_SSID to set one"; diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh new file mode 100644 index 0000000..6e92a49 --- /dev/null +++ b/scripts/entrypoint.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env sh + +set -e + +# Set variables accessible by the frontend +mkdir -p /app/frontend/public + +# Build runtime-config.js containing only env vars that are defined and non-empty. +node - <<'NODE' +const fs = require('fs'); +const outPath = '/app/frontend/public/runtime-config.js'; +const keys = ['WIFI_SSID','WIFI_PASSWORD','WIFI_TYPE','WIFI_HIDDEN']; +const cfg = {}; +for (const k of keys) { + const v = process.env[k]; + if (v !== undefined) { + cfg[k] = v; + } +} +fs.writeFileSync(outPath, 'window.__RUNTIME_CONFIG__ = ' + JSON.stringify(cfg) + ';', 'utf8'); +console.log('WROTE', outPath, 'keys=', Object.keys(cfg)); +NODE + +# exec the original command +exec "$@" diff --git a/scripts/run_wrapper.sh b/scripts/run_wrapper.sh index 1e00e54..ba5f34c 100644 --- a/scripts/run_wrapper.sh +++ b/scripts/run_wrapper.sh @@ -17,8 +17,7 @@ sleep 3 # Start frontend in foreground echo "Starting frontend..." NEXT_TELEMETRY_DISABLED="1" NODE_ENV="production" \ - HOSTNAME=${FRONTEND_BIND_HOST} PORT="${FRONTEND_BIND_PORT}" \ - UNIFI_CONTROLLER_URL="" UNIFI_API_KEY="" UNIFI_SITE_ID="" \ + HOSTNAME="${FRONTEND_BIND_HOST}" PORT="${FRONTEND_BIND_PORT}" \ node ./frontend/server.js & FRONTEND_PID=$!