feat: read most of the admin config from env vars

This commit is contained in:
Emmanuel BENOîT 2025-01-02 14:21:51 +01:00
parent de7ed2a996
commit bc5a70c4c6
Signed by: Emmanuel BENOîT
SSH key fingerprint: SHA256:l7PFUUF5TCDsvYeQC9OnTNz08dFY7Fvf4Hv3neIqYpg

View file

@ -1,18 +1,36 @@
<?php
function readSecret(string $varName): string {
$fileVarName = $varName . '_FILE';
$fileName = getenv($fileVarName);
if ($fileName !== false) {
$value = @file_get_contents($fileName);
if ($value === false) {
throw new \Exception("Could not read secret from file $fileName");
}
} else {
$value = getenv($varName);
if ($value === false) {
throw new \Exception("Could not read secret from variable $varName");
}
}
return $value;
}
$aConfig = array(
'host' => '127.0.0.1',
'database' => 'legacyworlds',
'user' => 'legacyworlds',
'password' => 'password for the standard database user',
'adminUser' => 'legacyworlds_admin',
'adminPassword' => 'password for the administrative database user',
'host' => getenv('LW_DB_HOST') ?: '127.0.0.1',
'database' => getenv('LW_DB_NAME') ?: 'legacyworlds',
'user' => getenv('LW_DB_USER_NAME') ?: 'legacyworlds',
'password' => readSecret('LW_DB_USER_PASS'),
'adminUser' => getenv('LW_DB_ADMIN_NAME') ?: 'legacyworlds_admin',
'adminPassword' => readSecret('LW_DB_ADMIN_PASS'),
'ctrlFifo' =>'/tmp/.lwFifo',
'ctrlPath' =>'/tmp/.lwControl'
);
function __dbConnect($admin = false) {
function __dbConnect(bool $admin = false): \PgSql\Connection|bool {
global $aConfig;
$h = $aConfig['host'];
$d = $aConfig['database'];