From 71002939a1eb85d9efffe7adac896c69d46ef93d Mon Sep 17 00:00:00 2001 From: Jason Tudisco Date: Thu, 12 Mar 2026 17:18:36 -0600 Subject: [PATCH] Add bash version of go script for macOS/Linux MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- go_example_1.sh | 85 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 go_example_1.sh diff --git a/go_example_1.sh b/go_example_1.sh new file mode 100644 index 0000000..fe92a58 --- /dev/null +++ b/go_example_1.sh @@ -0,0 +1,85 @@ +#!/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