79 lines
2.0 KiB
Bash
Executable File
79 lines
2.0 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Run backend (Go) and frontend (Vite) together; stop both on Ctrl+C
|
|
set -euo pipefail
|
|
|
|
# Always run from repo root
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
cd "$SCRIPT_DIR"
|
|
|
|
BACKEND_ADDR=${BACKEND_ADDR:-0.0.0.0:9119}
|
|
DATA_DIR=${DATA_DIR:-./x-storage}
|
|
|
|
# Store PIDs for cleanup
|
|
BACKEND_PID=""
|
|
FRONTEND_PID=""
|
|
|
|
cleanup() {
|
|
echo ""
|
|
echo "[dev] Shutting down..."
|
|
|
|
# Kill backend if running
|
|
if [ -n "$BACKEND_PID" ] && kill -0 "$BACKEND_PID" 2>/dev/null; then
|
|
echo "[dev] Stopping backend (PID: $BACKEND_PID)..."
|
|
kill "$BACKEND_PID" 2>/dev/null || true
|
|
wait "$BACKEND_PID" 2>/dev/null || true
|
|
fi
|
|
|
|
# Kill frontend if running
|
|
if [ -n "$FRONTEND_PID" ] && kill -0 "$FRONTEND_PID" 2>/dev/null; then
|
|
echo "[dev] Stopping frontend (PID: $FRONTEND_PID)..."
|
|
kill "$FRONTEND_PID" 2>/dev/null || true
|
|
wait "$FRONTEND_PID" 2>/dev/null || true
|
|
fi
|
|
|
|
# Kill any remaining background jobs
|
|
if command -v xargs >/dev/null 2>&1; then
|
|
jobs -p | xargs kill 2>/dev/null || true
|
|
else
|
|
# Fallback for systems without xargs -r
|
|
for job in $(jobs -p); do
|
|
kill "$job" 2>/dev/null || true
|
|
done
|
|
fi
|
|
|
|
# Wait a bit for graceful shutdown
|
|
sleep 1
|
|
|
|
# Force kill if still running
|
|
if [ -n "$BACKEND_PID" ] && kill -0 "$BACKEND_PID" 2>/dev/null; then
|
|
kill -9 "$BACKEND_PID" 2>/dev/null || true
|
|
fi
|
|
if [ -n "$FRONTEND_PID" ] && kill -0 "$FRONTEND_PID" 2>/dev/null; then
|
|
kill -9 "$FRONTEND_PID" 2>/dev/null || true
|
|
fi
|
|
|
|
echo "[dev] Shutdown complete"
|
|
exit 0
|
|
}
|
|
|
|
trap cleanup INT TERM EXIT
|
|
|
|
# Start backend
|
|
echo "[dev] Starting backend on $BACKEND_ADDR (data-dir=$DATA_DIR)"
|
|
go run . --debug --address "$BACKEND_ADDR" --data-dir "$DATA_DIR" &
|
|
BACKEND_PID=$!
|
|
|
|
# Wait a moment for backend to start
|
|
sleep 2
|
|
|
|
# Start frontend
|
|
echo "[dev] Starting frontend dev server (Vite)"
|
|
(cd frontend && pnpm run dev) &
|
|
FRONTEND_PID=$!
|
|
|
|
echo "[dev] Backend PID: $BACKEND_PID, Frontend PID: $FRONTEND_PID"
|
|
echo "[dev] Press Ctrl+C to stop both..."
|
|
|
|
# Wait for both processes to exit
|
|
wait $BACKEND_PID $FRONTEND_PID
|