From 8ffcccb1e816990675ccbe73693a6020142c60d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20BENO=C3=8ET?= Date: Thu, 2 Jan 2025 14:41:59 +0100 Subject: [PATCH] feat: configure the game from env variables --- docker/compose.yml | 9 +++++++++ docker/env.example | 5 +++++ game/scripts/config.inc | 4 ++-- game/scripts/legacyworlds.xml | 8 ++++---- game/scripts/lib/xml_config.inc | 21 +++++++++++++++++++-- 5 files changed, 39 insertions(+), 8 deletions(-) diff --git a/docker/compose.yml b/docker/compose.yml index e93eedf..7aea935 100644 --- a/docker/compose.yml +++ b/docker/compose.yml @@ -43,6 +43,15 @@ services: size: 1g - planets:/opt/lwb5/site/static/beta5/pics/pl:ro - pgen:/var/spool/pgen:rw + environment: + LW_DB_HOST: db + LW_DB_ADMIN_PASS_FILE: /run/secrets/lw_db_admin_pass + LW_DB_USER_PASS_FILE: /run/secrets/lw_db_user_pass + LW_SEND_MAIL: ${LW_SEND_MAIL} + LW_STATIC_URL: ${LW_STATIC_URL} + secrets: + - lw_db_user_pass + - lw_db_admin_pass planetgen: build: diff --git a/docker/env.example b/docker/env.example index 7028781..4c1da2d 100644 --- a/docker/env.example +++ b/docker/env.example @@ -1,6 +1,11 @@ # Password for the database superuser DB_PASSWORD=... +# URL of the static assets from an external perspective +LW_STATIC_URL=http://localhost/static +# Send email? (yes or no) +LW_SEND_MAIL=no + # Legacyworlds database - Main user LW_DB_USER_PASS=... # Legacyworlds database - Admin user diff --git a/game/scripts/config.inc b/game/scripts/config.inc index 8ab50cb..e4a8bc1 100644 --- a/game/scripts/config.inc +++ b/game/scripts/config.inc @@ -6,7 +6,7 @@ $config = array( // Path and URL to static contents "staticdir" => __DIR__ . "/../site/static", - "staticurl" => "http://localhost/static", + "staticurl" => getenv("LW_STATIC_URL") ?: "http://localhost/static", // Path to game scripts "scriptdir" => __DIR__, @@ -40,7 +40,7 @@ $config = array( "trace" => array(), // Do we need to actually send emails? - "sendmails" => false + "sendmails" => (getenv("LW_SEND_MAIL") ?: "no") == "yes", ); if (file_exists($config['cachedir'] . "/maintenance.ser")) { diff --git a/game/scripts/legacyworlds.xml b/game/scripts/legacyworlds.xml index 22dff12..a3af3d8 100644 --- a/game/scripts/legacyworlds.xml +++ b/game/scripts/legacyworlds.xml @@ -4,10 +4,10 @@ - - - - + + + + diff --git a/game/scripts/lib/xml_config.inc b/game/scripts/lib/xml_config.inc index 67f8371..1388edc 100644 --- a/game/scripts/lib/xml_config.inc +++ b/game/scripts/lib/xml_config.inc @@ -12,16 +12,33 @@ class xml_config { private static $games = null; private static $defGame = null; + private static function readFromEnv(string $varName, string $default): string { + if ($varName == '') { + return $default; + } + + $fileVarName = $varName . '_FILE'; + $fileName = getenv($fileVarName); + if ($fileName !== false) { + $value = @file_get_contents($fileName); + } else { + $value = getenv($varName); + } + return $value ?: $default; + } + private static function parseMainParams($root) { $node = $root->firstChild; while ($node) { if ($node->nodeType == XML_ELEMENT_NODE && $node->nodeName == 'Param') { $aName = $node->getAttribute('name'); - $aValue = $node->getAttribute('value'); - if ($aName == "") { + if ($aName == '') { l::warn("CONFIG: a main parameter is missing a 'name' attribute"); } elseif (!isset(xml_config::$mGame->params[$aName])) { + $aValue = $node->getAttribute('value'); + $aFromEnv = $node->getAttribute('from-env'); + $aValue = self::readFromEnv($aFromEnv, $aValue); xml_config::$mGame->params[$aName] = $aValue; } else { l::notice("CONFIG: duplicate main parameter '$aName'");