veza/apps/web/scripts/start-backend-and-dev.sh

47 lines
1.2 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# Start the Go backend in the background, then run Vite dev server.
# Run from apps/web (e.g. npm run dev:with-api).
# Backend will listen on port 8080 (veza.fr:8080 when using VITE_DOMAIN=veza.fr).
set -e
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
BACKEND_DIR="$ROOT/veza-backend-api"
START_BACKEND="$ROOT/scripts/start-backend.sh"
if [ ! -f "$BACKEND_DIR/go.mod" ]; then
echo "Error: backend not found at $BACKEND_DIR"
exit 1
fi
# Start backend in background (same entry point as make dev-backend-api)
echo "Starting backend API on port 8080..."
(
cd "$BACKEND_DIR"
if command -v air &>/dev/null; then
air
else
go run ./cmd/api/main.go
fi
) &
BACKEND_PID=$!
# Kill backend when this script exits (e.g. Ctrl+C)
cleanup() { kill $BACKEND_PID 2>/dev/null || true; exit 0; }
trap cleanup EXIT INT TERM
# Wait for backend to listen
echo "Waiting for backend to be ready..."
for i in 1 2 3 4 5 6 7 8 9 10; do
if curl -s -o /dev/null -w "%{http_code}" "http://127.0.0.1:8080/api/v1/health" 2>/dev/null | grep -q 200; then
echo "Backend ready."
break
fi
if [ $i -eq 10 ]; then
echo "Warning: backend did not respond in time. Starting Vite anyway."
fi
sleep 1
done
# Run Vite (foreground)
exec npm run dev