# check.ps1 - a local pre-push check for the ClinicApi sample. # Run it from the folder that holds compose.yaml: .\check.ps1 # It builds the image, starts the api and its database, waits for /health, # reads /patients, prints PASS or FAIL, and always takes the stack down again. # It asks Compose which host port the api service publishes for container # port 8080, so it works whatever port your compose.yaml uses. # Works in Windows PowerShell 5.1 and PowerShell 7. $result = 'FAIL' $reason = '' $count = 0 $watch = [System.Diagnostics.Stopwatch]::StartNew() try { Write-Host '== docker compose build' docker compose build if ($LASTEXITCODE -ne 0) { throw 'docker compose build failed' } Write-Host '== docker compose up -d' docker compose up -d if ($LASTEXITCODE -ne 0) { throw 'docker compose up -d failed' } $published = @(docker compose port api 8080)[0] if ($LASTEXITCODE -ne 0 -or -not $published) { throw 'the api service publishes no host port for 8080' } $baseUrl = 'http://localhost:' + ($published -split ':')[-1] Write-Host "== api published at $published" Write-Host "== waiting for $baseUrl/health (up to 30 tries, 2 s apart)" $healthy = $false for ($try = 1; $try -le 30; $try++) { $body = curl.exe -s -f --max-time 5 "$baseUrl/health" if ($LASTEXITCODE -eq 0) { Write-Host " try $try : $body" $healthy = $true break } Write-Host " try $try : not ready (curl exit code $LASTEXITCODE)" Start-Sleep -Seconds 2 } if (-not $healthy) { throw '/health did not answer after 30 tries' } Write-Host "== GET $baseUrl/patients" $json = curl.exe -s -f --max-time 10 "$baseUrl/patients" if ($LASTEXITCODE -ne 0) { throw "/patients failed (curl exit code $LASTEXITCODE)" } $patients = ($json -join '') | ConvertFrom-Json $count = $patients.Count if ($count -lt 1) { throw '/patients returned no rows' } Write-Host " $count patients, first: $($patients[0].fullName)" $result = 'PASS' } catch { $reason = $_.Exception.Message } finally { Write-Host '== docker compose down' docker compose down } $watch.Stop() $seconds = '{0:N1}' -f $watch.Elapsed.TotalSeconds if ($result -eq 'PASS') { Write-Host "PASS ($count patients, $seconds s)" exit 0 } else { Write-Host "FAIL: $reason ($seconds s)" exit 1 }