forked from SmartDropLabs/smartdrop-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-stack.sh
More file actions
executable file
·111 lines (100 loc) · 3.65 KB
/
Copy pathdev-stack.sh
File metadata and controls
executable file
·111 lines (100 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env bash
# Launches the full local SmartDrop stack in one command:
# in-memory Redis -> smartdrop-backend (port 4000) -> smartdrop-frontend (port 3000)
#
# Usage:
# ./scripts/dev-stack.sh
# SMARTDROP_BACKEND_DIR=/path/to/smartdrop-backend ./scripts/dev-stack.sh
#
# Ctrl+C stops all three. Re-run any time; the admin API key is generated once
# and reused across runs from ~/.smartdrop-dev/admin-api-key.txt (handy for
# testing the /alerts page, which requires it).
set -euo pipefail
DEV_HOME="$HOME/.smartdrop-dev"
REDIS_MEM_DIR="$DEV_HOME/redis-mem"
LOG_DIR="$DEV_HOME/logs"
BACKEND_DIR="${SMARTDROP_BACKEND_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")/../../smartdrop-backend" 2>/dev/null && pwd || true)}"
mkdir -p "$DEV_HOME" "$LOG_DIR"
if [ -z "$BACKEND_DIR" ] || [ ! -d "$BACKEND_DIR" ]; then
echo "error: couldn't find smartdrop-backend. Clone it as a sibling of this repo, or set SMARTDROP_BACKEND_DIR." >&2
exit 1
fi
if [ ! -f "$DEV_HOME/admin-api-key.txt" ]; then
openssl rand -hex 32 > "$DEV_HOME/admin-api-key.txt"
fi
ADMIN_API_KEY="$(cat "$DEV_HOME/admin-api-key.txt")"
# --- one-time bootstrap of the in-memory Redis helper ---
if [ ! -d "$REDIS_MEM_DIR/node_modules" ]; then
echo "==> First run: installing an in-memory Redis for local dev (no system Redis/Docker needed)..."
mkdir -p "$REDIS_MEM_DIR"
(cd "$REDIS_MEM_DIR" && npm init -y >/dev/null 2>&1 && npm install redis-memory-server >/dev/null 2>&1)
cat > "$REDIS_MEM_DIR/start.mjs" <<'EOF'
import { RedisMemoryServer } from "redis-memory-server";
const redisServer = new RedisMemoryServer({ instance: { port: 6379 } });
const host = await redisServer.getHost();
const port = await redisServer.getPort();
console.log(`REDIS_READY redis://${host}:${port}`);
process.stdin.resume();
for (const sig of ["SIGINT", "SIGTERM"]) {
process.on(sig, async () => { await redisServer.stop(); process.exit(0); });
}
EOF
fi
PIDS=()
cleanup() {
echo ""
echo "==> Stopping stack..."
for pid in "${PIDS[@]}"; do
kill "$pid" 2>/dev/null || true
done
wait 2>/dev/null || true
}
trap cleanup EXIT INT TERM
# --- 1. Redis ---
if lsof -i :6379 -sTCP:LISTEN >/dev/null 2>&1; then
echo "==> Redis already running on :6379, reusing it."
else
echo "==> Starting in-memory Redis on :6379..."
(cd "$REDIS_MEM_DIR" && node start.mjs) > "$LOG_DIR/redis.log" 2>&1 &
PIDS+=($!)
for i in $(seq 1 30); do
grep -q "REDIS_READY" "$LOG_DIR/redis.log" 2>/dev/null && break
sleep 1
done
if ! grep -q "REDIS_READY" "$LOG_DIR/redis.log" 2>/dev/null; then
echo "error: Redis didn't start in time. See $LOG_DIR/redis.log" >&2
exit 1
fi
fi
# --- 2. Backend ---
if lsof -i :4000 -sTCP:LISTEN >/dev/null 2>&1; then
echo "==> Backend already running on :4000, reusing it."
else
echo "==> Starting smartdrop-backend on :4000..."
(
cd "$BACKEND_DIR"
PORT=4000 \
REDIS_URL=redis://localhost:6379 \
NODE_ENV=development \
ADMIN_API_KEY="$ADMIN_API_KEY" \
CORS_ALLOWED_ORIGINS="http://localhost:3000,http://localhost:3001" \
npm run dev
) > "$LOG_DIR/backend.log" 2>&1 &
PIDS+=($!)
for i in $(seq 1 60); do
grep -q "running on port" "$LOG_DIR/backend.log" 2>/dev/null && break
sleep 1
done
if ! grep -q "running on port" "$LOG_DIR/backend.log" 2>/dev/null; then
echo "error: backend didn't start in time. See $LOG_DIR/backend.log" >&2
exit 1
fi
fi
echo ""
echo "==> Redis: redis://localhost:6379"
echo "==> Backend: http://localhost:4000 (log: $LOG_DIR/backend.log)"
echo "==> Admin API key (for /alerts): $ADMIN_API_KEY"
echo "==> Frontend: http://localhost:3000 (starting now, Ctrl+C stops everything)"
echo ""
# --- 3. Frontend (foreground) ---
npm run dev