From 40a1bdd3798f20db75f86899d791502c369340d9 Mon Sep 17 00:00:00 2001 From: Fabio Herzig Date: Sat, 25 Jul 2026 22:22:05 +0200 Subject: [PATCH] Some more changes for Docker --- compose.yaml | 33 ++++++++++++++--- setup/init-env.php | 89 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 setup/init-env.php diff --git a/compose.yaml b/compose.yaml index f6e3013..3797e8b 100644 --- a/compose.yaml +++ b/compose.yaml @@ -15,6 +15,14 @@ services: networks: - wkvs + env-init: + build: ./config/apache + container_name: wkvs-env-init + volumes: + - .:/var/wkvs + working_dir: /var/wkvs + command: php setup/init-env.php + composer-init: image: composer:latest container_name: wkvs-composer-init @@ -22,6 +30,9 @@ services: - .:/app working_dir: /app/composer command: composer install --no-interaction --prefer-dist + depends_on: + env-init: + condition: service_completed_successfully apache: build: ./config/apache @@ -42,7 +53,8 @@ services: container_name: wkvs-mariadb restart: unless-stopped env_file: - - ./config/.env.db + - path: ./config/.env.db + required: false volumes: - db_data:/var/lib/mysql healthcheck: @@ -50,6 +62,9 @@ services: interval: 5s timeout: 5s retries: 5 + depends_on: + env-init: + condition: service_completed_successfully networks: - wkvs @@ -62,6 +77,8 @@ services: depends_on: mariadb: condition: service_healthy + env-init: + condition: service_completed_successfully networks: - wkvs command: php setup/database/init-db.php @@ -71,8 +88,12 @@ services: container_name: wkvs-redis restart: unless-stopped env_file: - - ./config/.env.redis + - path: ./config/.env.redis + required: false command: ["sh", "-c", "exec redis-server --requirepass \"$REDDIS_PASSWORD\""] + depends_on: + env-init: + condition: service_completed_successfully networks: - wkvs @@ -84,12 +105,16 @@ services: - .:/var/wkvs working_dir: /var/wkvs/websocket env_file: - - ./config/.env.redis + - path: ./config/.env.redis + required: false environment: - REDDIS_HOST=redis command: sh -c "npm install && npm install -g pm2 && pm2-runtime start index.js --max-memory-restart 1G --node-args=\"--max-old-space-size=1024\" --name=\"WebSocket WKVS\"" depends_on: - - redis + redis: + condition: service_started + env-init: + condition: service_completed_successfully networks: - wkvs diff --git a/setup/init-env.php b/setup/init-env.php new file mode 100644 index 0000000..649b576 --- /dev/null +++ b/setup/init-env.php @@ -0,0 +1,89 @@ + [ + 'example' => 'env.redis.example', + 'replacements' => [ + '/REDDIS_HOST=127\.0\.0\.1/' => 'REDDIS_HOST=redis', + '/REDDIS_HOST=localhost/' => 'REDDIS_HOST=redis', + '/REDDIS_HOST=YOUR_REDDIS_IP.*/' => 'REDDIS_HOST=redis', + '/REDDIS_PORT=YOUR_REDDIS_PORT.*/' => 'REDDIS_PORT=6379', + '/YOUR_REDDIS_PASSWORD/' => base64_encode(random_bytes(64)), + ] + ], + 'env.db' => [ + 'example' => 'env.db.example', + 'replacements' => [ + '/MYSQL_HOST=localhost/' => 'MYSQL_HOST=mariadb', + '/MYSQL_HOST=127\.0\.0\.1/' => 'MYSQL_HOST=mariadb', + '/YOUR_PASSWORD_FOR_ROOT/' => base64_encode(random_bytes(64)), + '/YOUR_DATABASE_NAME/' => 'devdb_', + ] + ], + 'env.db-tables' => [ + 'example' => 'env.db-tables.example', + 'replacements' => [ + '/DB_PREFIX=PREFIX/' => 'DB_PREFIX=wkvs_', + ] + ], + 'env.pw-encryption-key' => [ + 'example' => 'env.pw-encryption-key.example', + 'replacements' => [ + '/YOUR_CUSTOM_PASSWORD_ENCRYPTION_KEY/' => base64_encode(random_bytes(64)), + ] + ], + 'env.wk-id' => [ + 'example' => 'env.wk-id.example', + 'replacements' => [] + ], + 'env.db-wkvs-user' => [ + 'example' => 'env.db-wkvs-user.example', + 'replacements' => [ + '/DB_HOST=localhost/' => 'DB_HOST=mariadb', + '/DB_HOST=127\.0\.0\.1/' => 'DB_HOST=mariadb', + '/DATABASE_NAME/' => 'devdb_', + ] + ], + 'env.db-guest' => [ + 'example' => 'env.db-guest.example', + 'replacements' => [ + '/DB_HOST=localhost/' => 'DB_HOST=mariadb', + '/DB_HOST=127\.0\.0\.1/' => 'DB_HOST=mariadb', + '/DATABASE_NAME/' => 'devdb_', + ] + ], +]; + +echo "========================================\n"; +echo " WKVS Environment Initializer \n"; +echo "========================================\n\n"; + +foreach ($envMappings as $targetName => $config) { + $targetPath = $configDir . '/.' . $targetName; + $examplePath = $configDir . '/.' . $config['example']; + + if (!file_exists($targetPath)) { + if (file_exists($examplePath)) { + echo "Creating missing ." . $targetName . " from ." . $config['example'] . "...\n"; + $content = file_get_contents($examplePath); + foreach ($config['replacements'] as $pattern => $replacement) { + $content = preg_replace($pattern, $replacement, $content); + } + file_put_contents($targetPath, $content); + } else { + echo "⚠️ Warning: Example file ." . $config['example'] . " not found.\n"; + } + } else { + echo "Skipping ." . $targetName . " (file already exists).\n"; + } +} + +echo "✓ Environment initialization complete.\n\n";