-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdev.sh
More file actions
executable file
·230 lines (197 loc) · 6.4 KB
/
Copy pathdev.sh
File metadata and controls
executable file
·230 lines (197 loc) · 6.4 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/bin/bash
# Soniq Dev Startup Script
# Starts both API (backend) and Dashboard (frontend) + ngrok
#
# Usage:
# ./dev.sh # Start both BE + FE (recommended)
# ./dev.sh --api # Start only API
# ./dev.sh --fe # Start only Frontend
# ./dev.sh --docker # Use Docker for API
set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
API_PORT=3100
FE_PORT=3000
NGROK_PORT=4040
# Defaults
START_API=true
START_FE=true
USE_DOCKER=false
# Parse args
for arg in "$@"; do
case $arg in
--api) START_FE=false ;;
--fe) START_API=false ;;
--docker) USE_DOCKER=true ;;
esac
done
log() { echo -e "${GREEN}[DEV]${NC} $1"; }
log_api() { echo -e "${CYAN}[API]${NC} $1"; }
log_fe() { echo -e "${YELLOW}[FE]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; }
cleanup() {
echo ""
log "Shutting down..."
# Kill processes
$USE_DOCKER && docker compose down 2>/dev/null || true
lsof -ti:$API_PORT | xargs -r kill -9 2>/dev/null || true
lsof -ti:$FE_PORT | xargs -r kill -9 2>/dev/null || true
pkill -f "ngrok http" 2>/dev/null || true
pkill -f "next dev" 2>/dev/null || true
exit 0
}
trap cleanup SIGINT SIGTERM
# Header
echo ""
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE} Soniq Voice AI - Dev${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
# Check env files
if $START_API && [ ! -f "soniq-api/.env" ]; then
error "Missing soniq-api/.env file!"
exit 1
fi
# Cleanup old processes
log "Cleaning up old processes..."
pkill -f "ngrok http" 2>/dev/null || true
lsof -ti:$API_PORT | xargs -r kill -9 2>/dev/null || true
lsof -ti:$FE_PORT | xargs -r kill -9 2>/dev/null || true
lsof -ti:$NGROK_PORT | xargs -r kill -9 2>/dev/null || true
docker compose down 2>/dev/null || true
sleep 1
# ============================================
# Start API (Backend)
# ============================================
if $START_API; then
if $USE_DOCKER; then
log_api "Starting API with Docker..."
cp soniq-api/.env .env 2>/dev/null || true
docker compose up -d --build api
log_api "Waiting for container..."
for i in {1..60}; do
curl -s http://localhost:$API_PORT/health > /dev/null 2>&1 && break
sleep 2
echo -n "."
done
echo ""
else
log_api "Starting API server..."
cd soniq-api
# Load nvm and use correct node version (requires node >= 22)
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
if [ -f .nvmrc ]; then
nvm use > /dev/null 2>&1 || nvm install > /dev/null 2>&1
log_api "Using node $(node --version)"
fi
npm run dev > /tmp/soniq-api.log 2>&1 &
API_PID=$!
cd ..
log_api "Waiting for server..."
for i in {1..30}; do
curl -s http://localhost:$API_PORT/health > /dev/null 2>&1 && break
sleep 1
echo -n "."
done
echo ""
fi
# Verify API
if curl -s http://localhost:$API_PORT/health > /dev/null 2>&1; then
HEALTH=$(curl -s http://localhost:$API_PORT/health)
log_api "Running on :$API_PORT | LLM: $(echo $HEALTH | jq -r '.services.llm.model')"
else
error "API failed to start!"
[ -f /tmp/soniq-api.log ] && tail -20 /tmp/soniq-api.log
exit 1
fi
fi
# ============================================
# Start Dashboard (Frontend)
# ============================================
if $START_FE; then
log_fe "Starting Dashboard..."
cd soniq-dashboard
# Set API URL for frontend
export NEXT_PUBLIC_API_URL="http://localhost:$API_PORT"
npm run dev > /tmp/soniq-dashboard.log 2>&1 &
FE_PID=$!
cd ..
log_fe "Waiting for Next.js..."
for i in {1..30}; do
curl -s http://localhost:$FE_PORT > /dev/null 2>&1 && break
sleep 1
echo -n "."
done
echo ""
if curl -s http://localhost:$FE_PORT > /dev/null 2>&1; then
log_fe "Running on :$FE_PORT"
else
warn "Dashboard may still be compiling - check http://localhost:$FE_PORT"
fi
fi
# ============================================
# Start ngrok (for API webhooks)
# ============================================
if $START_API; then
log "Starting ngrok tunnel for API..."
npx ngrok http $API_PORT --log=stdout > /tmp/ngrok.log 2>&1 &
sleep 3
NGROK_URL=$(curl -s http://localhost:$NGROK_PORT/api/tunnels 2>/dev/null | jq -r '.tunnels[0].public_url' 2>/dev/null)
[ -z "$NGROK_URL" ] || [ "$NGROK_URL" == "null" ] && NGROK_URL="(run manually: npx ngrok http $API_PORT)"
fi
# ============================================
# Summary
# ============================================
echo ""
echo -e "${BLUE}========================================${NC}"
echo -e "${GREEN} Dev Environment Ready${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
if $START_API; then
echo -e " ${CYAN}Backend API:${NC}"
echo -e " Local: ${GREEN}http://localhost:$API_PORT${NC}"
echo -e " Public: ${GREEN}$NGROK_URL${NC}"
echo -e " Health: http://localhost:$API_PORT/health"
echo ""
fi
if $START_FE; then
echo -e " ${YELLOW}Frontend Dashboard:${NC}"
echo -e " Local: ${GREEN}http://localhost:$FE_PORT${NC}"
echo ""
fi
if $START_API; then
echo -e " ${CYAN}SignalWire Webhooks:${NC}"
echo -e " Voice: $NGROK_URL/signalwire/voice"
echo -e " Stream: ${NGROK_URL/https/wss}/signalwire/stream"
echo ""
fi
echo -e " ${YELLOW}Logs:${NC}"
$START_API && echo -e " API: tail -f /tmp/soniq-api.log"
$START_FE && echo -e " FE: tail -f /tmp/soniq-dashboard.log"
echo -e " ngrok: tail -f /tmp/ngrok.log"
echo ""
echo -e " Press ${RED}Ctrl+C${NC} to stop all services"
echo ""
# ============================================
# Stream logs (combined)
# ============================================
if $START_API && $START_FE; then
# Show both logs with prefixes
tail -f /tmp/soniq-api.log 2>/dev/null | sed 's/^/[API] /' &
tail -f /tmp/soniq-dashboard.log 2>/dev/null | sed 's/^/[FE] /' &
wait
elif $START_API; then
if $USE_DOCKER; then
docker compose logs -f api
else
tail -f /tmp/soniq-api.log
fi
elif $START_FE; then
tail -f /tmp/soniq-dashboard.log
fi