From e993da2869fa47e8a6cfa7d4d94591f65a83e05e Mon Sep 17 00:00:00 2001 From: "courtmanr@gmail.com" Date: Sun, 20 Apr 2025 19:58:50 +0100 Subject: [PATCH] refactor: Improve .env handling and validation --- README.md | 2 ++ docker-compose.yml | 7 +++---- server/index.js | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 078195863..354f1bccf 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,8 @@ To stop the container(s) defined in the `docker-compose.yml` file, run: docker compose down ``` +*Note: If you modify the `server/.env` file after the container is already running, you may need to restart the container for the changes to take effect. You can do this by running `docker compose down` followed by `docker compose up -d`, or by using `docker compose up -d --force-recreate`.* + ## ✨ Features - Lightweight monitoring for Proxmox VE nodes. diff --git a/docker-compose.yml b/docker-compose.yml index f761274d7..7663693da 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,3 @@ -version: '3.8' - services: pulse-server: # Build context commented out - using pre-built image from Docker Hub @@ -14,8 +12,9 @@ services: # You can change the host port (left side) if 7655 is already in use on your host - "7655:7655" env_file: - # Load environment variables from the .env file located in the server directory - - ./server/.env + # Load environment variables, prioritizing the one in the server directory + - ./server/.env # Primary location + - ./.env # Fallback location (if server/.env is missing) # Optional: Define networks if needed, otherwise uses default bridge network # networks: # - pulse_network diff --git a/server/index.js b/server/index.js index 5ae49b3c8..7fe5deeba 100644 --- a/server/index.js +++ b/server/index.js @@ -1,5 +1,44 @@ require('dotenv').config(); // Load environment variables from .env file +// --- BEGIN Environment Variable Validation --- +const requiredEnvVars = [ + 'PROXMOX_HOST', + 'PROXMOX_TOKEN_ID', + 'PROXMOX_TOKEN_SECRET' +]; +const placeholderValues = [ + 'your-proxmox-ip-or-hostname', + 'your-api-token-id@pam!your-token-name', + 'your-api-token-secret-uuid', + 'your-password' // Added just in case password fallback is used without token +]; + +let missingVars = []; +let placeholderVars = []; + +requiredEnvVars.forEach(varName => { + const value = process.env[varName]; + if (!value) { + missingVars.push(varName); + } else if (placeholderValues.some(placeholder => value.includes(placeholder))) { + placeholderVars.push(varName); + } +}); + +if (missingVars.length > 0 || placeholderVars.length > 0) { + console.error('\n--- Configuration Error ---'); + if (missingVars.length > 0) { + console.error(`Missing required environment variables in server/.env: ${missingVars.join(', ')}`); + } + if (placeholderVars.length > 0) { + console.error(`Environment variables seem to contain placeholder values in server/.env: ${placeholderVars.join(', ')}`); + } + console.error('Please ensure server/.env exists and contains valid Proxmox connection details.'); + console.error('Refer to server/.env.example for the required format.\n'); + process.exit(1); // Exit if configuration is invalid +} +// --- END Environment Variable Validation --- + const express = require('express'); const http = require('http'); const path = require('path');