#!/usr/bin/env bash
# scripts/run-local-servers.sh — runs the node's Go signal and mailbox servers on
# loopback for manual testing, the same way the integration tests do.
# Needs AP2P_REPO (a checkout of the node repo) and a Go toolchain.
set -euo pipefail
: "${AP2P_REPO:?set AP2P_REPO to a checkout of the node repo}"
SIGNAL_PORT=${SIGNAL_PORT:-9020}
MAILBOX_PORT=${MAILBOX_PORT:-9023}
DIR=$(mktemp -d)
APP=11111111-2222-4333-8444-555555555555
ORIGIN=${ORIGIN:-https://test.example}
cat > "$DIR/signal-apps.json" <<JSON
{"turn":{"secret":"0123456789abcdef","realm":"ap2p.test","ttl":3600,"urls":["stun:127.0.0.1:3478"]},
"limits":{"messages_per_sec":30,"message_burst":60,"max_frame_bytes":65536},
"apps":{"$APP":{"hosts":["127.0.0.1","localhost"],"origins":["$ORIGIN"],
"limits":{"max_rooms":10,"max_room_peers":16,"max_conns_per_ip":50,"max_bytes_per_sec":262144},
"mailbox":{"ttl_hours":1,"max_mb":16,"max_message_kb":64,"mint_ip_per_min":600,"mint_app_per_min":600,"open_mint":true}}}}
JSON
cat > "$DIR/mailbox-apps.json" <<JSON
{"node":{"secret":"0123456789abcdef","max_disk_bytes":67108864},
"apps":{"$APP":{"hosts":["127.0.0.1","localhost"],"origins":["$ORIGIN"],"open_mint":true,
"limits":{"mailbox_ttl_hours":1,"mailbox_max_mb":16,"mailbox_max_message_kb":64}}}}
JSON
mkdir -p "$DIR/data"
echo "signal: ws://127.0.0.1:$SIGNAL_PORT/ws (Origin: $ORIGIN)"
echo "mailbox: http://127.0.0.1:$MAILBOX_PORT/mailbox/v1"
(cd "$AP2P_REPO/signal" && go run . -listen "127.0.0.1:$SIGNAL_PORT" -apps "$DIR/signal-apps.json") &
(cd "$AP2P_REPO/mailbox" && go run . -listen "127.0.0.1:$MAILBOX_PORT" -config "$DIR/mailbox-apps.json" -data "$DIR/data") &
trap 'kill 0' EXIT
wait