Files
mtusi2026lab2/scripts/init-env.ps1
T

18 lines
1017 B
PowerShell

$ErrorActionPreference = 'Stop'
Set-Location (Join-Path $PSScriptRoot '..')
$encoding = New-Object System.Text.UTF8Encoding($false)
New-Item -ItemType Directory -Force secrets | Out-Null
if (-not (Test-Path secrets/postgres_password)) {
$bytes = New-Object byte[] 32
$rng = [Security.Cryptography.RandomNumberGenerator]::Create()
$rng.GetBytes($bytes)
$rng.Dispose()
$password = [BitConverter]::ToString($bytes).Replace('-', '').ToLowerInvariant()
[IO.File]::WriteAllText((Join-Path $PWD 'secrets/postgres_password'), $password, $encoding)
}
$password = [IO.File]::ReadAllText((Join-Path $PWD 'secrets/postgres_password')).Trim()
[IO.File]::WriteAllText((Join-Path $PWD 'secrets/database_url'), "postgresql://messenger:${password}@postgres:5432/messenger", $encoding)
[IO.File]::WriteAllText((Join-Path $PWD 'secrets/cache_url'), 'redis://valkey:6379/0', $encoding)
if (-not (Test-Path .env)) { Copy-Item .env.example .env }
Write-Output 'Local configuration ready; secrets were not printed.'