-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.sh
More file actions
executable file
·379 lines (316 loc) · 11.4 KB
/
dev.sh
File metadata and controls
executable file
·379 lines (316 loc) · 11.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/bin/bash
# Layercake Development Script
# Runs both frontend and backend in development mode
# Use --tauri flag to run in Tauri desktop mode
set -e
# Parse command line arguments
TAURI_MODE=false
while [[ $# -gt 0 ]]; do
case $1 in
--tauri)
TAURI_MODE=true
shift
;;
*)
echo "Unknown option: $1"
echo "Usage: $0 [--tauri]"
exit 1
;;
esac
done
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
BACKEND_PORT=3001
FRONTEND_PORT=1422
BACKEND_DIR="."
FRONTEND_DIR="frontend"
PROJECTIONS_DIR="projections-frontend"
TAURI_DIR="src-tauri"
LOG_LEVEL="${LOG_LEVEL:-debug}"
LOCAL_AUTH_BYPASS="${LAYERCAKE_LOCAL_AUTH_BYPASS:-1}"
# Function to print colored output
print_status() {
echo -e "${BLUE}[DEV]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to cleanup background processes
cleanup() {
print_status "Shutting down development servers..."
# Kill background jobs
if [[ -n $TAURI_PID ]]; then
kill $TAURI_PID 2>/dev/null || true
fi
if [[ -n $BACKEND_PID ]]; then
kill $BACKEND_PID 2>/dev/null || true
fi
if [[ -n $FRONTEND_PID ]]; then
kill $FRONTEND_PID 2>/dev/null || true
fi
# Kill any remaining processes on our ports
if [[ "$TAURI_MODE" == "false" ]]; then
lsof -ti:$BACKEND_PORT | xargs kill -9 2>/dev/null || true
lsof -ti:$FRONTEND_PORT | xargs kill -9 2>/dev/null || true
fi
print_success "Development servers stopped"
exit 0
}
# Set up signal handlers
trap cleanup SIGINT SIGTERM EXIT
# Check if required directories exist
if [[ ! -d "$BACKEND_DIR" ]]; then
print_error "Backend directory '$BACKEND_DIR' not found"
exit 1
fi
if [[ "$TAURI_MODE" == "false" ]]; then
if [[ ! -d "$FRONTEND_DIR" ]]; then
print_error "Frontend directory '$FRONTEND_DIR' not found"
exit 1
fi
else
if [[ ! -d "$TAURI_DIR" ]]; then
print_error "Tauri directory '$TAURI_DIR' not found"
exit 1
fi
if [[ ! -d "$FRONTEND_DIR" ]]; then
print_error "Frontend directory '$FRONTEND_DIR' not found (required for Tauri)"
exit 1
fi
fi
# Check if ports are available (only in web mode)
if [[ "$TAURI_MODE" == "false" ]]; then
if lsof -i:$BACKEND_PORT >/dev/null 2>&1; then
print_warning "Port $BACKEND_PORT is already in use. Attempting to free it..."
lsof -ti:$BACKEND_PORT | xargs kill -9 2>/dev/null || true
sleep 2
fi
if lsof -i:$FRONTEND_PORT >/dev/null 2>&1; then
print_warning "Port $FRONTEND_PORT is already in use. Attempting to free it..."
lsof -ti:$FRONTEND_PORT | xargs kill -9 2>/dev/null || true
sleep 2
fi
fi
# Build projection viewer assets so backend can serve /projections/viewer/*
ensure_projection_viewer_build() {
if [[ ! -d "$PROJECTIONS_DIR" ]]; then
print_warning "Projection viewer directory '$PROJECTIONS_DIR' not found; skipping build"
return
fi
print_status "Building projection viewer assets..."
pushd "$PROJECTIONS_DIR" > /dev/null
if [[ ! -d "node_modules" ]]; then
print_status "Installing projection viewer dependencies..."
npm install
fi
echo "VITE_API_BASE_URL=http://localhost:$BACKEND_PORT" > .env.local
npm run build
popd > /dev/null
print_success "Projection viewer build ready"
}
# Build main frontend assets
ensure_frontend_build() {
if [[ ! -d "$FRONTEND_DIR" ]]; then
print_warning "Frontend directory '$FRONTEND_DIR' not found; skipping build"
return
fi
print_status "Building main frontend assets..."
pushd "$FRONTEND_DIR" > /dev/null
if [[ ! -d "node_modules" ]]; then
print_status "Installing frontend dependencies..."
npm install
fi
echo "VITE_API_BASE_URL=http://localhost:$BACKEND_PORT" > .env.development.local
npm run build
popd > /dev/null
print_success "Main frontend build ready"
}
if [[ "$TAURI_MODE" == "true" ]]; then
print_status "Starting Layercake in Tauri desktop mode..."
else
print_status "Starting Layercake development environment..."
print_status "Backend port: $BACKEND_PORT"
print_status "Frontend port: $FRONTEND_PORT"
fi
print_status "Log level: $LOG_LEVEL"
print_status "Local auth bypass: $LOCAL_AUTH_BYPASS"
# Initialize database if it doesn't exist
if [[ ! -f "layercake.db" ]]; then
print_status "Initializing database..."
cargo run --bin layercake -- db init
print_success "Database initialized"
fi
# Branch based on mode
if [[ "$TAURI_MODE" == "true" ]]; then
# ============================================
# TAURI MODE: Run desktop application
# ============================================
print_status "Starting Tauri desktop application (this may take a moment to compile)..."
# Tauri dev command handles both backend and frontend
LAYERCAKE_LOCAL_AUTH_BYPASS="$LOCAL_AUTH_BYPASS" cargo tauri dev > tauri.log 2>&1 &
TAURI_PID=$!
# Wait for Tauri to start
print_status "Waiting for Tauri to compile and start..."
TAURI_WAIT=0
TAURI_MAX_WAIT=360
while [ $TAURI_WAIT -lt $TAURI_MAX_WAIT ]; do
# Check if process is still alive
if ! kill -0 $TAURI_PID 2>/dev/null; then
print_error "Tauri process died. Check tauri.log for details:"
tail -50 tauri.log
exit 1
fi
# Check log for ready indicators
if grep -q "Finished" tauri.log 2>/dev/null; then
print_success "Tauri application started (PID: $TAURI_PID)"
break
fi
sleep 3
TAURI_WAIT=$((TAURI_WAIT + 3))
if [ $TAURI_WAIT -ge $TAURI_MAX_WAIT ]; then
print_error "Tauri failed to start within ${TAURI_MAX_WAIT}s. Check tauri.log for details:"
tail -50 tauri.log
exit 1
fi
done
# Display connection info
echo ""
print_success "🚀 Layercake Tauri desktop application is ready!"
echo ""
echo -e "${YELLOW}📝 Log:${NC} tail -f tauri.log"
echo ""
echo -e "${GREEN}Press Ctrl+C to stop the application${NC}"
# Monitor process
while true; do
sleep 30
if ! kill -0 $TAURI_PID 2>/dev/null; then
print_error "Tauri process died unexpectedly"
exit 1
fi
print_status "Tauri application running (PID: $TAURI_PID)"
done
else
# ============================================
# WEB MODE: Run separate backend and frontend
# ============================================
# Build frontend assets before starting servers
ensure_frontend_build
ensure_projection_viewer_build
# Start backend server
print_status "Starting backend server (this may take a moment to compile)..."
cd "$BACKEND_DIR"
LAYERCAKE_LOCAL_AUTH_BYPASS="$LOCAL_AUTH_BYPASS" \
cargo run --bin layercake -- serve --port $BACKEND_PORT --log-level $LOG_LEVEL --cors-origin "http://localhost:$FRONTEND_PORT" > backend.log 2>&1 &
BACKEND_PID=$!
cd - > /dev/null
# Wait for backend to compile and start (with progress indicator)
print_status "Waiting for backend to compile and start..."
BACKEND_WAIT=0
BACKEND_MAX_WAIT=360
while [ $BACKEND_WAIT -lt $BACKEND_MAX_WAIT ]; do
# Check if process is still alive
if ! kill -0 $BACKEND_PID 2>/dev/null; then
print_error "Backend process died. Check backend.log for details:"
tail -30 backend.log
exit 1
fi
# Check if server is responding
if curl -s -f http://localhost:$BACKEND_PORT/health > /dev/null 2>&1; then
print_success "Backend server started and responding (PID: $BACKEND_PID)"
break
fi
sleep 2
BACKEND_WAIT=$((BACKEND_WAIT + 2))
if [ $BACKEND_WAIT -ge $BACKEND_MAX_WAIT ]; then
print_error "Backend failed to start within ${BACKEND_MAX_WAIT}s. Check backend.log for details:"
tail -30 backend.log
exit 1
fi
done
# Start frontend server
print_status "Starting frontend server..."
cd "$FRONTEND_DIR"
# Check if node_modules exists, install if not
if [[ ! -d "node_modules" ]]; then
print_status "Installing frontend dependencies..."
npm install
fi
# Update environment file for backend connection
echo "VITE_API_BASE_URL=http://localhost:$BACKEND_PORT" > .env.development.local
npm run dev -- --port $FRONTEND_PORT > ../frontend.log 2>&1 &
FRONTEND_PID=$!
cd - > /dev/null
# Wait for frontend to start (with progress indicator)
print_status "Waiting for frontend to start..."
FRONTEND_WAIT=0
FRONTEND_MAX_WAIT=30
while [ $FRONTEND_WAIT -lt $FRONTEND_MAX_WAIT ]; do
# Check if process is still alive
if ! kill -0 $FRONTEND_PID 2>/dev/null; then
print_error "Frontend process died. Check frontend.log for details:"
tail -30 frontend.log
exit 1
fi
# Check if server is responding (look for Vite's ready message in log)
if grep -q "Local:.*localhost:$FRONTEND_PORT" frontend.log 2>/dev/null; then
print_success "Frontend server started and responding (PID: $FRONTEND_PID)"
break
fi
sleep 2
FRONTEND_WAIT=$((FRONTEND_WAIT + 2))
if [ $FRONTEND_WAIT -ge $FRONTEND_MAX_WAIT ]; then
print_error "Frontend failed to start within ${FRONTEND_MAX_WAIT}s. Check frontend.log for details:"
tail -30 frontend.log
exit 1
fi
done
# Display connection info
echo ""
print_success "🚀 Layercake development environment is ready!"
echo ""
echo -e "${BLUE}📊 Backend API:${NC} http://localhost:$BACKEND_PORT"
echo -e "${BLUE}🌐 Frontend App:${NC} http://localhost:$FRONTEND_PORT"
echo -e "${BLUE}📚 API Docs:${NC} http://localhost:$BACKEND_PORT/swagger-ui/"
echo -e "${BLUE}🔍 GraphQL:${NC} http://localhost:$BACKEND_PORT/graphql"
echo ""
echo -e "${YELLOW}📝 Logs:${NC}"
echo -e " Backend: tail -f backend.log"
echo -e " Frontend: tail -f frontend.log"
echo ""
echo -e "${GREEN}Press Ctrl+C to stop all servers${NC}"
# Function to check server health
check_health() {
backend_health=$(curl -s http://localhost:$BACKEND_PORT/health || echo "DOWN")
frontend_health=$(curl -s http://localhost:$FRONTEND_PORT || echo "DOWN")
if [[ "$backend_health" == "DOWN" ]] || [[ "$frontend_health" == "DOWN" ]]; then
print_warning "Health check failed - one or more servers may be down"
fi
}
# Monitor processes and show periodic status
while true; do
sleep 30
# Check if processes are still running
if ! kill -0 $BACKEND_PID 2>/dev/null; then
print_error "Backend process died unexpectedly"
exit 1
fi
if ! kill -0 $FRONTEND_PID 2>/dev/null; then
print_error "Frontend process died unexpectedly"
exit 1
fi
# Optional health check (uncomment to enable)
# check_health
print_status "Services running (Backend: $BACKEND_PID, Frontend: $FRONTEND_PID)"
done
fi