From a6ff67a4da25d1b850b1d09df9f389b2a12229c9 Mon Sep 17 00:00:00 2001 From: GoJang Date: Fri, 8 Aug 2025 15:45:01 +0800 Subject: [PATCH] feat: enhance multi-container deployment support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds support for running multiple MicroOCPPSimulator instances on the same host with runtime configuration capabilities: ## Key Features ### Runtime API Configuration - Added entrypoint script for dynamic API_ROOT configuration at container startup - Frontend now supports window.API_ROOT for runtime API endpoint configuration - Maintains backward compatibility with existing environment variable approach ### Environment-based Charger Configuration - Added CHARGER_ID and CHARGER_KEY environment variable support - Allows different charging station identities for multi-container scenarios - Falls back to default values for single-instance compatibility - Added runtime logging for configuration debugging ### Container Enhancements - Modified Dockerfile to support runtime configuration through entrypoint script - Improved container structure for multi-instance deployment - Enhanced deployment flexibility while preserving existing functionality ## Benefits - Enables easy testing of multiple charging station simulators - Supports scalability testing with multiple concurrent instances - Maintains full backward compatibility with existing deployments - Facilitates complex OCPP network simulation scenarios 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- Dockerfile | 12 +++++++++--- entrypoint.sh | 21 +++++++++++++++++++++ src/main.cpp | 14 ++++++++++++-- webapp-src | 2 +- 4 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 entrypoint.sh diff --git a/Dockerfile b/Dockerfile index d90093e..58d3e9c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,8 +22,14 @@ RUN cmake --build ./build -j 16 --target mo_simulator -j 16 # Grant execute permissions to the shell script RUN chmod +x /MicroOcppSimulator/build/mo_simulator -# Expose port 8000 -EXPOSE 8000 +# Copy pre-built frontend public folder for runtime API configuration +COPY public /public + +# Copy entrypoint script for runtime environment setup +COPY entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh -# Run the shell script inside the container +ENTRYPOINT ["/entrypoint.sh"] CMD ["./build/mo_simulator"] + +EXPOSE 8000 diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..0b71a34 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,21 @@ +#!/bin/sh +set -e + +# Runtime API endpoint configuration +TARGET_FILE="./public/bundle.html.gz" +TEMP_HTML="/tmp/bundle.html" + +if [ -z "$API_ROOT" ]; then + echo "[warn] API_ROOT environment variable not detected, frontend will use {{API_ROOT}} placeholder" +else + echo "[info] Runtime replacement: {{API_ROOT}} → $API_ROOT" + # Decompress to temporary file + gzip -d -c "$TARGET_FILE" > "$TEMP_HTML" + # Replace all placeholders + sed -i "s|{{API_ROOT}}|$API_ROOT|g" "$TEMP_HTML" + # Recompress back to original path + gzip -9 -c "$TEMP_HTML" > "$TARGET_FILE" +fi + +# Start the C++ simulator +exec "./build/mo_simulator" \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 0e945fd..56f2aec 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -101,10 +101,20 @@ void load_ocpp_version(std::shared_ptr filesystem) } void app_setup(MicroOcpp::Connection& connection, std::shared_ptr filesystem) { + // Configure charger credentials from environment variables or use defaults + const char *envId = std::getenv("CHARGER_ID"); + const char *envKey = std::getenv("CHARGER_KEY"); + std::string chargerId = envId ? envId : "MicroOcpp Simulator"; + std::string chargerKey = envKey ? envKey : "MicroOcpp"; + + // Log runtime configuration values for debugging + std::cout << "[INFO] Charger ID: " << chargerId << std::endl; + std::cout << "[INFO] Charger Key: " << chargerKey << std::endl; + mocpp_initialize(connection, g_isOcpp201 ? - ChargerCredentials::v201("MicroOcpp Simulator", "MicroOcpp") : - ChargerCredentials("MicroOcpp Simulator", "MicroOcpp"), + ChargerCredentials::v201(chargerId.c_str(), chargerKey.c_str()) : + ChargerCredentials(chargerId.c_str(), chargerKey.c_str()), filesystem, false, g_isOcpp201 ? diff --git a/webapp-src b/webapp-src index 015a0e8..0775451 160000 --- a/webapp-src +++ b/webapp-src @@ -1 +1 @@ -Subproject commit 015a0e816ca4fc617e67acdbe7d067688d6b3939 +Subproject commit 07754510f369e2a71d76b86ad8ee4525458db687