SPB Git

Repository Public

dev-backend.sh 3.5 KB 52 lines shellscript

1#!/usr/bin/env bash2# Author: Simon-Pierre Boucher — contact@spboucher.ai3# File: Scripts/dev-backend.sh4# Description: Starts (or stops) ONE private instance of the SP Mail dev backend for the apps' self-test and the integration tests, with5# the scripted AI stub (Scripts/dev-ai-stub.mjs) as its « local » provider AND its push pipeline enabled against the local fake APNs6# server (Scripts/dev-fake-apns.mjs, the server's test hook SPMAIL_APNS_HOST_OVERRIDE). The backend runs from the shared checkout7# (read-only for this repository); only the processes started here are ever stopped (by PID). No AI call and no push leave the Mac.8#9# Usage: Scripts/dev-backend.sh start|stop|status10# Environment: SPMAIL_DEV_PORT (default 8821), SPMAIL_DEV_DB (default postgres://127.0.0.1/sp_mail_apple2dev),11#              SPMAIL_AI_STUB_PORT (default 8853), SPMAIL_FAKE_APNS_PORT (default 8857),12#              SPMAIL_BACKEND_DIR (default ~/Desktop/Cluster/projets/sp-mail),13#              SPMAIL_AI_STUB=0 / SPMAIL_FAKE_APNS=0 to start the backend without the stub / without push.14set -euo pipefail15cd "$(dirname "$0")/.."16PORT="${SPMAIL_DEV_PORT:-8821}"; DB="${SPMAIL_DEV_DB:-postgres://127.0.0.1/sp_mail_apple2dev}"17STUB_PORT="${SPMAIL_AI_STUB_PORT:-8853}"; APNS_PORT="${SPMAIL_FAKE_APNS_PORT:-8857}"18BACKEND="${SPMAIL_BACKEND_DIR:-$HOME/Desktop/Cluster/projets/sp-mail}"19mkdir -p Build20PIDS="Build/dev-backend-$PORT.pids"21case "${1:-status}" in22  start)23    if curl -fsS "http://127.0.0.1:$PORT/api/health/live" >/dev/null 2>&1; then echo "backend already answers on :$PORT"; exit 0; fi24    createdb -h 127.0.0.1 "${DB##*/}" 2>/dev/null || true25    : > "$PIDS"26    EXTRA=()27    if [ "${SPMAIL_AI_STUB:-1}" = 1 ]; then28      SPMAIL_AI_STUB_PORT="$STUB_PORT" nohup node Scripts/dev-ai-stub.mjs > "Build/ai-stub-$STUB_PORT.log" 2>&1 &29      echo $! >> "$PIDS"30      EXTRA+=(SPMAIL_AI_LOCAL_URL="http://127.0.0.1:$STUB_PORT/v1" SPMAIL_AI_LOCAL_MODEL=stub-scripted SPMAIL_AI_EMBED_BACKEND=endpoint SPMAIL_AI_EMBED_ENDPOINT_MODEL=stub-scripted)31    fi32    if [ "${SPMAIL_FAKE_APNS:-1}" = 1 ]; then33      SPMAIL_FAKE_APNS_PORT="$APNS_PORT" nohup node Scripts/dev-fake-apns.mjs > "Build/fake-apns-$APNS_PORT.log" 2>&1 &34      echo $! >> "$PIDS"35      for _ in $(seq 1 20); do [ -f Build/fake-apns-key.p8 ] && break; sleep 0.2; done36      EXTRA+=(SPMAIL_APNS_HOST_OVERRIDE="http://127.0.0.1:$APNS_PORT" SPMAIL_APNS_KEY_FILE="$PWD/Build/fake-apns-key.p8" SPMAIL_APNS_KEY_ID=DEVKEY0001 SPMAIL_APNS_TEAM_ID=DEVTEAM001)37    fi38    LOG="$PWD/Build/dev-backend-$PORT.log"39    # `exec` all the way down (subshell → env → dev.sh → node): the recorded PID is the server's own, so `stop` really stops it40    ( cd "$BACKEND" && exec env PORT="$PORT" DATABASE_URL="$DB" SPMAIL_OPS_MONITOR=off ${EXTRA[@]+"${EXTRA[@]}"} nohup scripts/dev.sh node dist/server.mjs > "$LOG" 2>&1 ) &41    echo $! >> "$PIDS"42    for _ in $(seq 1 60); do curl -fsS "http://127.0.0.1:$PORT/api/health/live" >/dev/null 2>&1 && { echo "backend ready on :$PORT — AI stub :$STUB_PORT, fake APNs :$APNS_PORT (log $LOG)"; exit 0; }; sleep 0.5; done43    echo "backend did not start (see $LOG)"; exit 1 ;;44  stop)45    [ -f "$PIDS" ] || { echo "nothing started here"; exit 0; }46    while read -r pid; do kill "$pid" 2>/dev/null || true; done < "$PIDS"47    rm -f "$PIDS"; echo "stopped" ;;48  status)49    curl -fsS "http://127.0.0.1:$PORT/api/health/live" >/dev/null 2>&1 && echo "backend answers on :$PORT" || echo "no backend on :$PORT" ;;50  *) echo "usage: dev-backend.sh start|stop|status"; exit 2 ;;51esac52
Files main
Menu

Theme