From 51a1be22d2d6c3df6a80f74f643ab68ade7c412a Mon Sep 17 00:00:00 2001 From: cpu Date: Mon, 31 Mar 2025 01:26:50 +0200 Subject: [PATCH] env variables handling --- .dockerignore | 4 +- .gitignore | 1 + Dockerfile | 25 ++++++- README.md | 9 ++- dev-start.sh | 30 ++++++++ generate-config.sh | 39 ++++++++++ index.html | 5 +- js/app.js | 10 ++- js/config.js | 36 +++++++-- js/env-loader.js | 111 ++++++++++++++-------------- js/services/pushFlicIntegration.js | 19 +---- js/services/serviceWorkerManager.js | 1 - js/ui/pushSettingsUI.js | 15 +--- package.json | 6 +- test.html | 4 +- 15 files changed, 209 insertions(+), 106 deletions(-) create mode 100755 dev-start.sh create mode 100755 generate-config.sh diff --git a/.dockerignore b/.dockerignore index c152b46..95321c7 100644 --- a/.dockerignore +++ b/.dockerignore @@ -49,9 +49,11 @@ build/ # We need .env for our application #.env .env.* - +# Don't ignore config.env.js +!config.env.js # Project specific files +dev-start.sh labels.example virt-game-timer.service package.json diff --git a/.gitignore b/.gitignore index eb125ff..291e807 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ yarn-error.log* .env.development.local .env.test.local .env.production.local +config.env.js # Editor directories and files .idea/ diff --git a/Dockerfile b/Dockerfile index d51d60d..98996ed 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,35 @@ # Use a lightweight server FROM nginx:alpine +# Install bash for the script execution +RUN apk add --no-cache bash + # Set working directory WORKDIR /usr/share/nginx/html # Copy all the application files COPY . . -# Copy the .env file -COPY .env . +# Create a simple script to generate config.env.js +RUN echo '#!/bin/sh' > /usr/share/nginx/html/docker-generate-config.sh && \ + echo 'echo "// config.env.js - Generated from .env" > config.env.js' >> /usr/share/nginx/html/docker-generate-config.sh && \ + echo 'echo "// This file contains environment variables for the PWA" >> config.env.js' >> /usr/share/nginx/html/docker-generate-config.sh && \ + echo 'echo "// Generated on $(date)" >> config.env.js' >> /usr/share/nginx/html/docker-generate-config.sh && \ + echo 'echo "" >> config.env.js' >> /usr/share/nginx/html/docker-generate-config.sh && \ + echo 'echo "window.ENV_CONFIG = {" >> config.env.js' >> /usr/share/nginx/html/docker-generate-config.sh && \ + echo 'grep -v "^#" .env | grep "=" | while read line; do' >> /usr/share/nginx/html/docker-generate-config.sh && \ + echo ' key=$(echo $line | cut -d= -f1)' >> /usr/share/nginx/html/docker-generate-config.sh && \ + echo ' value=$(echo $line | cut -d= -f2-)' >> /usr/share/nginx/html/docker-generate-config.sh && \ + echo ' echo " $key: \"$value\"," >> config.env.js' >> /usr/share/nginx/html/docker-generate-config.sh && \ + echo 'done' >> /usr/share/nginx/html/docker-generate-config.sh && \ + echo 'echo "};" >> config.env.js' >> /usr/share/nginx/html/docker-generate-config.sh && \ + chmod +x /usr/share/nginx/html/docker-generate-config.sh + +# Generate config.env.js from .env +RUN /usr/share/nginx/html/docker-generate-config.sh + +# Remove the .env file and the generation script for security +RUN rm .env docker-generate-config.sh # Expose port 80 EXPOSE 80 diff --git a/README.md b/README.md index 79110fa..0d6fa21 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ game-timer/ ## Environment Variables -The application uses environment variables for configuration. These are loaded from a `.env` file at runtime. +The application uses environment variables for configuration. These are loaded from a `.env` file and converted to a `config.env.js` file that is served by the web server. ### Setting Up Environment Variables @@ -45,7 +45,12 @@ The application uses environment variables for configuration. These are loaded f BACKEND_URL=https://your-push-server.example.com ``` -3. For security, never commit your `.env` file to version control. It's already included in `.gitignore`. +3. Generate the `config.env.js` file using the provided script: + ```bash + ./generate-config.sh + ``` + +4. For security, never commit your `.env` file to version control. It's already included in `.gitignore`. ### Generating VAPID Keys diff --git a/dev-start.sh b/dev-start.sh new file mode 100755 index 0000000..24e4446 --- /dev/null +++ b/dev-start.sh @@ -0,0 +1,30 @@ +#!/bin/bash +# Script to start a local development server with environment variables + +# Check if .env file exists +if [ ! -f .env ]; then + echo "Error: .env file not found!" + echo "Please create a .env file based on .env.example" + exit 1 +fi + +# Generate config.env.js from .env +echo "Generating config.env.js from .env..." +./generate-config.sh + +# Start a local development server +echo "Starting development server..." +if command -v python3 &> /dev/null; then + echo "Using Python3 HTTP server on port 8000..." + python3 -m http.server 8000 +elif command -v python &> /dev/null; then + echo "Using Python HTTP server on port 8000..." + python -m SimpleHTTPServer 8000 +elif command -v npx &> /dev/null; then + echo "Using npx serve on port 8000..." + npx serve -l 8000 +else + echo "Error: Could not find a suitable static file server." + echo "Please install Python or Node.js, or manually start a server." + exit 1 +fi diff --git a/generate-config.sh b/generate-config.sh new file mode 100755 index 0000000..0ce6951 --- /dev/null +++ b/generate-config.sh @@ -0,0 +1,39 @@ +#!/bin/bash +# Script to generate config.env.js from .env file +# Usage: ./generate-config.sh + +# Check if .env file exists +if [ ! -f .env ]; then + echo "Error: .env file not found!" + echo "Please create a .env file based on .env.example" + exit 1 +fi + +echo "Generating config.env.js from .env..." + +# Create config.env.js file +echo "// config.env.js - Generated from .env" > config.env.js +echo "// This file contains environment variables for the PWA" >> config.env.js +echo "// Generated on $(date)" >> config.env.js +echo "" >> config.env.js +echo "window.ENV_CONFIG = {" >> config.env.js + +# Read .env file line by line +while IFS="=" read -r key value || [ -n "$key" ]; do + # Skip comments and empty lines + [[ $key =~ ^#.*$ ]] && continue + [[ -z $key ]] && continue + + # Remove quotes if present + value="${value%\"}" + value="${value#\"}" + value="${value%\'}" + value="${value#\'}" + + # Add the key-value pair to config.env.js + echo " $key: \"$value\"," >> config.env.js +done < .env + +echo "};" >> config.env.js + +echo "config.env.js generated successfully!" diff --git a/index.html b/index.html index f494ae9..d4e1ee5 100644 --- a/index.html +++ b/index.html @@ -154,6 +154,9 @@ + + +