CanMan/go_example_1.sh
Jason Tudisco 71002939a1 Add bash version of go script for macOS/Linux
Same as go_example_1.ps1 but for bash — starts CAN service, Paste UI,
and sync agent. Uses trap for clean shutdown on Ctrl+C.

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

86 lines
2.4 KiB
Bash

#!/usr/bin/env bash
# go_example_1.sh — Start CanService + Paste + Sync agent
#
# Run on multiple machines (after git clone) and they will auto-sync
# all ingested assets via iroh's relay network. No port forwarding needed.
set -e
ROOT="$(cd "$(dirname "$0")" && pwd)"
cleanup() {
echo ""
echo "Shutting down..."
[ -n "$CAN_PID" ] && kill "$CAN_PID" 2>/dev/null
[ -n "$SYNC_PID" ] && kill "$SYNC_PID" 2>/dev/null
[ -n "$PASTE_PID" ] && kill "$PASTE_PID" 2>/dev/null
wait 2>/dev/null
exit 0
}
trap cleanup INT TERM EXIT
# Kill anything on our ports
echo "Cleaning up stale processes..."
lsof -ti:3210 2>/dev/null | xargs kill -9 2>/dev/null || true
lsof -ti:3211 2>/dev/null | xargs kill -9 2>/dev/null || true
sleep 0.5
# --- Build everything ---
echo "Building CanService..."
cargo build --manifest-path "$ROOT/Cargo.toml"
echo "Building Paste example..."
cargo build --manifest-path "$ROOT/examples/paste/Cargo.toml"
echo "Building CAN Sync agent..."
cargo build --manifest-path "$ROOT/examples/can-sync/Cargo.toml" --bin can-sync
# --- Start CanService ---
echo "Starting CanService on port 3210..."
cargo run --manifest-path "$ROOT/Cargo.toml" &
CAN_PID=$!
# Wait for CanService to be ready
echo "Waiting for CanService..."
for i in $(seq 1 30); do
if curl -sf http://127.0.0.1:3210/api/v1/can/0/list >/dev/null 2>&1; then
break
fi
if [ "$i" -eq 30 ]; then
echo "CanService failed to start within 15s"
exit 1
fi
sleep 0.5
done
echo "CanService ready."
# --- Start Sync agent ---
SYNC_CONFIG="$ROOT/examples/can-sync/config.yaml"
echo "Starting CAN Sync agent (P2P replication)..."
cargo run --manifest-path "$ROOT/examples/can-sync/Cargo.toml" --bin can-sync -- "$SYNC_CONFIG" &
SYNC_PID=$!
sleep 2
# --- Start Paste example ---
echo "Starting Paste on port 3211..."
cargo run --manifest-path "$ROOT/examples/paste/Cargo.toml" &
PASTE_PID=$!
echo ""
echo "Running:"
echo " CanService -> http://127.0.0.1:3210"
echo " Paste UI -> http://127.0.0.1:3211"
echo " CAN Sync -> P2P replication active (iroh relay)"
echo ""
echo "Sync passphrase: 'duke-canman-sync'"
echo "Any other machine running this script with the same passphrase"
echo "will automatically discover this instance and sync all assets."
echo ""
echo "Press Ctrl+C to stop all services."
# Wait for any process to exit
wait -n "$CAN_PID" "$PASTE_PID" "$SYNC_PID" 2>/dev/null || true