CanMan/go_example_1.ps1
Jason Tudisco 23e5e0712e Add sync agent to go_example_1.ps1 for automatic P2P replication
The go script now builds and starts the can-sync agent alongside CAN
service and Paste UI. Any machine that clones the repo and runs the
script will auto-discover other instances via iroh's relay network
using the shared passphrase "duke-canman-sync" — no port forwarding
or manual peer configuration needed.

Changes:
- Add sync_api_key to root config.yaml (enables sync API)
- Update can-sync config.yaml with matching key and shared passphrase
- Update go_example_1.ps1 to build and launch can-sync agent
- Script now manages 3 processes: CAN service, Paste UI, sync agent

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-12 17:16:42 -06:00

98 lines
3.8 KiB
PowerShell

# go_example_1.ps1 — Start CanService + Paste + Sync agent, open browser
#
# Run on multiple machines (after git clone) and they will auto-sync
# all ingested assets via iroh's relay network. No port forwarding needed.
$ErrorActionPreference = "Stop"
$root = $PSScriptRoot
# Kill any leftover processes on our ports
Write-Host "Cleaning up stale processes..." -ForegroundColor Yellow
Get-NetTCPConnection -LocalPort 3210 -ErrorAction SilentlyContinue |
ForEach-Object { Stop-Process -Id $_.OwningProcess -Force -ErrorAction SilentlyContinue }
Get-NetTCPConnection -LocalPort 3211 -ErrorAction SilentlyContinue |
ForEach-Object { Stop-Process -Id $_.OwningProcess -Force -ErrorAction SilentlyContinue }
Start-Sleep -Milliseconds 500
# --- Build everything ---
Write-Host "Building CanService..." -ForegroundColor Cyan
cargo build --manifest-path "$root\Cargo.toml"
Write-Host "Building Paste example..." -ForegroundColor Cyan
cargo build --manifest-path "$root\examples\paste\Cargo.toml"
Write-Host "Building CAN Sync agent..." -ForegroundColor Cyan
cargo build --manifest-path "$root\examples\can-sync\Cargo.toml" --bin can-sync
# --- Start CanService ---
Write-Host "Starting CanService on port 3210..." -ForegroundColor Green
$canService = Start-Process -FilePath "cargo" `
-ArgumentList "run --manifest-path `"$root\Cargo.toml`"" `
-WorkingDirectory $root `
-PassThru -NoNewWindow
# Wait for CanService to be ready
Write-Host "Waiting for CanService..." -ForegroundColor Yellow
$ready = $false
for ($i = 0; $i -lt 30; $i++) {
try {
$null = Invoke-WebRequest -Uri "http://127.0.0.1:3210/api/v1/can/0/list" -TimeoutSec 1 -ErrorAction Stop
$ready = $true
break
} catch {
Start-Sleep -Milliseconds 500
}
}
if (-not $ready) {
Write-Host "CanService failed to start within 15s" -ForegroundColor Red
Stop-Process -Id $canService.Id -Force -ErrorAction SilentlyContinue
exit 1
}
Write-Host "CanService ready." -ForegroundColor Green
# --- Start Sync agent ---
$syncConfig = "$root\examples\can-sync\config.yaml"
Write-Host "Starting CAN Sync agent (P2P replication)..." -ForegroundColor Green
$syncAgent = Start-Process -FilePath "cargo" `
-ArgumentList "run --manifest-path `"$root\examples\can-sync\Cargo.toml`" --bin can-sync -- `"$syncConfig`"" `
-WorkingDirectory $root `
-PassThru -NoNewWindow
# Give sync agent a moment to connect to CAN service
Start-Sleep -Seconds 2
# --- Start Paste example ---
Write-Host "Starting Paste on port 3211..." -ForegroundColor Green
$paste = Start-Process -FilePath "cargo" `
-ArgumentList "run --manifest-path `"$root\examples\paste\Cargo.toml`"" `
-WorkingDirectory $root `
-PassThru -NoNewWindow
Write-Host ""
Write-Host "Running:" -ForegroundColor Cyan
Write-Host " CanService -> http://127.0.0.1:3210"
Write-Host " Paste UI -> http://127.0.0.1:3211"
Write-Host " CAN Sync -> P2P replication active (iroh relay)" -ForegroundColor Magenta
Write-Host ""
Write-Host "Sync passphrase: 'duke-canman-sync'" -ForegroundColor Magenta
Write-Host "Any other machine running this script with the same passphrase" -ForegroundColor Magenta
Write-Host "will automatically discover this instance and sync all assets." -ForegroundColor Magenta
Write-Host ""
Write-Host "Press Ctrl+C to stop all services." -ForegroundColor Yellow
# Wait for any process to exit, then clean up all
try {
while (-not $canService.HasExited -and -not $paste.HasExited -and -not $syncAgent.HasExited) {
Start-Sleep -Seconds 1
}
} finally {
Write-Host "Shutting down..." -ForegroundColor Yellow
Stop-Process -Id $canService.Id -Force -ErrorAction SilentlyContinue
Stop-Process -Id $paste.Id -Force -ErrorAction SilentlyContinue
Stop-Process -Id $syncAgent.Id -Force -ErrorAction SilentlyContinue
}