SPB Git

Repository Public

dev-server.sh 5.1 KB 93 lines shellscript

1#!/usr/bin/env bash2# Author: Simon-Pierre Boucher — contact@spboucher.ai3# File: Scripts/dev-server.sh4# Description: Starts / stops the REAL SP Vault server from this repository for the development variant5#              "SP Vault Dev" (Debug builds, SPV_DEV) and creates invitations in its test database.6#7# Usage: Scripts/dev-server.sh up | down | status | invite [label] | reset8#   up      createdb if missing, start the API on http://localhost:8781 (RP ID localhost), wait until ready9#   down    stop the server — only the PID this script started10#   status  running or not, PID, readiness11#   invite  insert an invitation (token hash = base64url SHA-256) and print the token and the join link12#   reset   stop the server, drop and recreate the schema of the test database, start again13# Only port 8781 (SPV_DEV_PORT, 8781–8789) and only the database sp_vault_native_test are ever touched.14set -euo pipefail15export PATH="/opt/homebrew/bin:/usr/local/bin:$PATH"16cd "$(dirname "$0")/.."17APPLE="$PWD"; REPO="$(cd .. && pwd)"18PORT="${SPV_DEV_PORT:-8781}"; DB="sp_vault_native_test"; ORIGIN="http://localhost:$PORT"19case "$PORT" in 878[1-9]) ;; *) echo "ERROR: SPV_DEV_PORT must be 8781–8789" >&2; exit 2 ;; esac20RUN="$APPLE/build/dev-server"; PIDFILE="$RUN/server-$PORT.pid"; LOG="$RUN/server-$PORT.log"; DATA="$RUN/data-$PORT"21mkdir -p "$RUN"2223pid() { [[ -f "$PIDFILE" ]] && cat "$PIDFILE" || true; }24# the PID file holds the node process that listens on the port (the one this script started); the command line is25# checked too, so that a recycled PID is never mistaken for the server26alive() { local p c; p="$(pid)"; [[ -n "$p" ]] && kill -0 "$p" 2>/dev/null && c="$(ps -p "$p" -o command= 2>/dev/null)" && [[ "$c" == *tsx* ]]; }27listener() { lsof -ti "tcp:$PORT" -sTCP:LISTEN 2>/dev/null | head -1; }28ready() { curl -fsS -o /dev/null --max-time 2 "$ORIGIN/api/health/ready" 2>/dev/null; }29sql() { psql -h 127.0.0.1 -d "$DB" -X -q -At -v ON_ERROR_STOP=1 -c "$1"; }3031up() {32  if alive; then33    if ready; then echo "dev server already running: $ORIGIN (pid $(pid))"; return 0; fi34    echo "dev server process $(pid) is alive but not ready; see $LOG" >&2; return 135  fi36  if lsof -ti "tcp:$PORT" -sTCP:LISTEN >/dev/null 2>&1; then37    echo "ERROR: port $PORT is held by another process (pid $(lsof -ti "tcp:$PORT" -sTCP:LISTEN | tr '\n' ' ')) that this script did not start. Not touching it." >&2; return 138  fi39  [[ -x "$REPO/node_modules/.bin/tsx" ]] || { echo "ERROR: $REPO/node_modules/.bin/tsx missing (run pnpm install in the repository)." >&2; return 1; }40  psql -h 127.0.0.1 -d "$DB" -X -q -At -c "select 1" >/dev/null 2>&1 || createdb -h 127.0.0.1 "$DB"41  mkdir -p "$DATA"42  ( cd "$REPO" && DATABASE_URL="postgres://127.0.0.1/$DB" PORT="$PORT" SPV_ORIGIN="$ORIGIN" SPV_RP_ID=localhost SPV_DATA_DIR="$DATA" \43      SPV_RATE_SCALE=50 SPV_LOG_LEVEL=warn SPV_BREACH_UPSTREAM="$ORIGIN/api/health/ready?" \44      nohup node_modules/.bin/tsx services/api/src/index.ts </dev/null >>"$LOG" 2>&1 & echo $! > "$RUN/launcher-$PORT.pid" )45  local launcher; launcher="$(cat "$RUN/launcher-$PORT.pid")"; rm -f "$RUN/launcher-$PORT.pid"46  for _ in $(seq 1 150); do47    if ready; then48      # tsx starts node as a child: the process to stop later is the one that listens (the port was free before we started)49      listener > "$PIDFILE"50      echo "dev server up: $ORIGIN (pid $(pid), database $DB, log build/dev-server/$(basename "$LOG"))"; return 051    fi52    kill -0 "$launcher" 2>/dev/null || { echo "ERROR: the server exited during start-up:" >&2; tail -15 "$LOG" >&2; return 1; }53    perl -e 'select(undef,undef,undef,0.2)'54  done55  kill "$launcher" 2>/dev/null || true56  echo "ERROR: the server did not become ready; see $LOG" >&2; return 157}5859down() {60  if alive; then61    local p; p="$(pid)"62    kill "$p"63    for _ in $(seq 1 50); do kill -0 "$p" 2>/dev/null || break; perl -e 'select(undef,undef,undef,0.1)'; done64    echo "dev server stopped (pid $p)"65  else echo "dev server not running"; fi66  rm -f "$PIDFILE"67}6869case "${1:-}" in70  up) up ;;71  down) down ;;72  status)73    if alive; then echo "running: $ORIGIN (pid $(pid)) · $(ready && echo ready || echo NOT ready) · accounts: $(sql 'select count(*) from accounts' 2>/dev/null || echo '?')"74    else echo "not running"; exit 1; fi ;;75  invite)76    alive && ready || up >/dev/null77    TOKEN="$(openssl rand 24 | basenc --base64url 2>/dev/null | tr -d '=\n' || true)"78    [[ -n "$TOKEN" ]] || TOKEN="$(openssl rand -base64 24 | tr '+/' '-_' | tr -d '=\n')"79    HASH="$(printf '%s' "$TOKEN" | openssl dgst -sha256 -binary | openssl base64 -A | tr '+/' '-_' | tr -d '=')"80    LABEL="$(printf '%s' "${2:-dev}" | tr -cd 'A-Za-z0-9 ._-')"81    sql "insert into invites (token_hash, label, expires_at) values ('$HASH', '$LABEL', now() + interval '1 day')"82    echo "token: $TOKEN"83    echo "link:  $ORIGIN/#/join/$TOKEN" ;;84  reset)85    down >/dev/null86    psql -h 127.0.0.1 -d "$DB" -X -q -At -c "select 1" >/dev/null 2>&1 || createdb -h 127.0.0.1 "$DB"87    sql "drop schema public cascade; create schema public;"88    rm -rf "$DATA"89    echo "database $DB reset"90    up ;;91  *) echo "usage: $0 up|down|status|invite [label]|reset"; exit 2 ;;92esac93
Files main
Menu

Theme