Skip to content

Instantly share code, notes, and snippets.

@bertt
Last active April 8, 2026 12:22
Show Gist options
  • Select an option

  • Save bertt/719718bdecfa34a03657e49b1f7fbdb4 to your computer and use it in GitHub Desktop.

Select an option

Save bertt/719718bdecfa34a03657e49b1f7fbdb4 to your computer and use it in GitHub Desktop.
TSG Dataspace startup - sample/http/start.sh - run met bash sample/http/start.sh
#!/usr/bin/env bash
# start.sh – Start the HTTP scenario services in the correct order.
#
# Each app type is compiled ONCE, then multiple instances are started with
# different CONFIG_PATH values. UIs are started as Vite dev servers after
# their respective backends are ready.
#
# Logs are written to sample/http/logs/<service>.log
#
# Usage (from any directory):
# bash sample/http/start.sh
set -euo pipefail
ROOT=$(git -C "$(dirname "$0")" rev-parse --show-toplevel)
CFG="$ROOT/sample/http/configs"
LOG_DIR="$ROOT/sample/http/logs"
PIDS=()
READY_TIMEOUT=120
mkdir -p "$LOG_DIR"
GREEN="\033[0;32m"
YELLOW="\033[0;33m"
RED="\033[0;31m"
CYAN="\033[0;36m"
RESET="\033[0m"
log() { echo -e "${GREEN}[start.sh]${RESET} $*"; }
warn() { echo -e "${YELLOW}[start.sh]${RESET} $*"; }
error() { echo -e "${RED}[start.sh]${RESET} $*" >&2; }
cleanup() {
warn "Stopping all services..."
for pid in "${PIDS[@]}"; do
kill "$pid" 2>/dev/null || true
done
wait 2>/dev/null || true
warn "All services stopped."
}
trap cleanup EXIT INT TERM
free_port() {
local port="$1"
local pid
pid=$(lsof -ti tcp:"$port" 2>/dev/null || true)
if [[ -n "$pid" ]]; then
warn "Port $port in use (pid $pid) – killing..."
kill -9 $pid 2>/dev/null || true
sleep 1
fi
}
# Poll a health endpoint until HTTP 2xx (for NestJS backends).
# Usage: wait_for_service <name> <port> [health_path] (default health_path: /status)
wait_for_service() {
local name="$1"
local port="$2"
local health_path="${3:-/status}"
local elapsed=0
local interval=3
printf "${CYAN}[start.sh]${RESET} Waiting for %s on :%s " "$name" "$port"
while true; do
local status
status=$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:${port}${health_path}" 2>/dev/null || true)
if [[ "$status" =~ ^2 ]]; then
echo ""
log "$name (:$port) ready ✓ (${elapsed}s)"
return 0
fi
if (( elapsed >= READY_TIMEOUT )); then
echo ""
error "$name did not become ready within ${READY_TIMEOUT}s."
error "Last 20 lines of $LOG_DIR/$name.log:"
tail -20 "$LOG_DIR/$name.log" >&2 || true
exit 1
fi
printf "."
sleep "$interval"
(( elapsed += interval ))
done
}
# Poll / until any HTTP response (for Vite UI dev servers).
wait_for_ui() {
local name="$1"
local port="$2"
local elapsed=0
local interval=2
printf "${CYAN}[start.sh]${RESET} Waiting for %s on :%s " "$name" "$port"
while true; do
local status
status=$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:${port}/" 2>/dev/null || true)
if [[ "$status" =~ ^[23] ]]; then
echo ""
log "$name (:$port) ready ✓ (${elapsed}s)"
return 0
fi
if (( elapsed >= READY_TIMEOUT )); then
echo ""
error "$name did not become ready within ${READY_TIMEOUT}s."
error "Last 20 lines of $LOG_DIR/$name.log:"
tail -20 "$LOG_DIR/$name.log" >&2 || true
exit 1
fi
printf "."
sleep "$interval"
(( elapsed += interval ))
done
}
# Build an app package (compilation happens once per app type).
build_app() {
local label="$1"
local filter="$2"
log "Building $label ..."
if ! (cd "$ROOT" && pnpm --filter "$filter" build) >> "$LOG_DIR/build-$label.log" 2>&1; then
error "Build failed for $label. See $LOG_DIR/build-$label.log"
exit 1
fi
log "$label built ✓"
}
# Start a pre-built backend (node ./dist/app.js) in the background.
start_service() {
local name="$1"
local config="$2"
local filter="$3"
local db="$4"
local port="$5"
free_port "$port"
log "Starting $name ..."
rm -f "$db"
(cd "$ROOT" && CONFIG_PATH="$config" pnpm --filter "$filter" start) \
> "$LOG_DIR/$name.log" 2>&1 &
PIDS+=($!)
}
# Start a Vite UI dev server in the background.
start_ui() {
local name="$1"
local backend="$2"
local script="$3" # root-level pnpm script, e.g. dev:wallet-ui
local port="$4"
free_port "$port"
log "Starting $name ..."
(cd "$ROOT" && BACKEND="$backend" pnpm "$script" --port "$port") \
> "$LOG_DIR/$name.log" 2>&1 &
PIDS+=($!)
}
# ---------------------------------------------------------------------------
# Build phase
# ---------------------------------------------------------------------------
log "=== Build phase ==="
build_app "wallet-api" "@apps/wallet-api"
build_app "control-plane-api" "@apps/control-plane-api"
build_app "http-data-plane-api" "@apps/http-data-plane-api"
# ---------------------------------------------------------------------------
# Wave 1 – Wallets
# ---------------------------------------------------------------------------
log "=== Wave 1: Wallets ==="
start_service "authority-wallet" "$CFG/authority-wallet.yaml" "@apps/wallet-api" \
"$ROOT/apps/wallet-api/authority.db" 3400
start_service "alfa-wallet" "$CFG/alfa-wallet.yaml" "@apps/wallet-api" \
"$ROOT/apps/wallet-api/alfa.db" 3500
start_service "bravo-wallet" "$CFG/bravo-wallet.yaml" "@apps/wallet-api" \
"$ROOT/apps/wallet-api/bravo.db" 3600
wait_for_service "authority-wallet" 3400
wait_for_service "alfa-wallet" 3500
wait_for_service "bravo-wallet" 3600
start_ui "authority-wallet-ui" "http://localhost:3400" "dev:wallet-ui" 3450
start_ui "alfa-wallet-ui" "http://localhost:3500" "dev:wallet-ui" 3550
start_ui "bravo-wallet-ui" "http://localhost:3600" "dev:wallet-ui" 3650
# ---------------------------------------------------------------------------
# Wave 2 – Control Planes
# ---------------------------------------------------------------------------
log "=== Wave 2: Control Planes ==="
start_service "alfa-control-plane" "$CFG/alfa-control-plane.yaml" "@apps/control-plane-api" \
"$ROOT/apps/control-plane-api/alfa.db" 3501
start_service "bravo-control-plane" "$CFG/bravo-control-plane.yaml" "@apps/control-plane-api" \
"$ROOT/apps/control-plane-api/bravo.db" 3601
wait_for_service "alfa-control-plane" 3501
wait_for_service "bravo-control-plane" 3601
start_ui "alfa-control-plane-ui" "http://localhost:3501" "dev:control-plane-ui" 3551
start_ui "bravo-control-plane-ui" "http://localhost:3601" "dev:control-plane-ui" 3651
# ---------------------------------------------------------------------------
# Wave 3 – Data Planes
# ---------------------------------------------------------------------------
log "=== Wave 3: Data Planes ==="
start_service "alfa-data-plane" "$CFG/alfa-data-plane.yaml" "@apps/http-data-plane-api" \
"$ROOT/apps/http-data-plane-api/alfa.db" 3502
start_service "bravo-data-plane" "$CFG/bravo-data-plane.yaml" "@apps/http-data-plane-api" \
"$ROOT/apps/http-data-plane-api/bravo.db" 3602
wait_for_service "alfa-data-plane" 3502 /health
wait_for_service "bravo-data-plane" 3602 /health
start_ui "alfa-data-plane-ui" "http://localhost:3502" "dev:http-data-plane-ui" 3552
start_ui "bravo-data-plane-ui" "http://localhost:3602" "dev:http-data-plane-ui" 3652
# ---------------------------------------------------------------------------
# Wait for all UIs
# ---------------------------------------------------------------------------
log "=== Waiting for UIs ==="
wait_for_ui "authority-wallet-ui" 3450
wait_for_ui "alfa-wallet-ui" 3550
wait_for_ui "bravo-wallet-ui" 3650
wait_for_ui "alfa-control-plane-ui" 3551
wait_for_ui "bravo-control-plane-ui" 3651
wait_for_ui "alfa-data-plane-ui" 3552
wait_for_ui "bravo-data-plane-ui" 3652
# ---------------------------------------------------------------------------
echo ""
echo -e "${GREEN}╔══════════════════════════════════════════════════════════════╗${RESET}"
echo -e "${GREEN}║ All services are up! 🚀 ║${RESET}"
echo -e "${GREEN}╠══════════════════╦══════════════════════╦════════════════════╣${RESET}"
echo -e "${GREEN}║${RESET} Service ${GREEN}║${RESET} UI ${GREEN}║${RESET} API ${GREEN}║${RESET}"
echo -e "${GREEN}╠══════════════════╬══════════════════════╬════════════════════╣${RESET}"
echo -e "${GREEN}║${RESET} Authority Wallet ${GREEN}║${RESET} http://localhost:3450 ${GREEN}║${RESET} :3400 ${GREEN}║${RESET}"
echo -e "${GREEN}║${RESET} Alfa Wallet ${GREEN}║${RESET} http://localhost:3550 ${GREEN}║${RESET} :3500 ${GREEN}║${RESET}"
echo -e "${GREEN}║${RESET} Bravo Wallet ${GREEN}║${RESET} http://localhost:3650 ${GREEN}║${RESET} :3600 ${GREEN}║${RESET}"
echo -e "${GREEN}╠══════════════════╬══════════════════════╬════════════════════╣${RESET}"
echo -e "${GREEN}║${RESET} Alfa Ctrl Plane ${GREEN}║${RESET} http://localhost:3551 ${GREEN}║${RESET} :3501 ${GREEN}║${RESET}"
echo -e "${GREEN}║${RESET} Bravo Ctrl Plane ${GREEN}║${RESET} http://localhost:3651 ${GREEN}║${RESET} :3601 ${GREEN}║${RESET}"
echo -e "${GREEN}╠══════════════════╬══════════════════════╬════════════════════╣${RESET}"
echo -e "${GREEN}║${RESET} Alfa Data Plane ${GREEN}║${RESET} http://localhost:3552 ${GREEN}║${RESET} :3502 ${GREEN}║${RESET}"
echo -e "${GREEN}║${RESET} Bravo Data Plane ${GREEN}║${RESET} http://localhost:3652 ${GREEN}║${RESET} :3602 ${GREEN}║${RESET}"
echo -e "${GREEN}╚══════════════════╩══════════════════════╩════════════════════╝${RESET}"
echo ""
log "Logs: $LOG_DIR"
log "Press Ctrl+C to stop all services."
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment