-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.sh
More file actions
executable file
·246 lines (200 loc) · 5.81 KB
/
dev.sh
File metadata and controls
executable file
·246 lines (200 loc) · 5.81 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
#!/bin/bash
# Rust Chat App - Development Script
# This script runs both frontend and backend in development mode with database support
set -e # Exit on any error
# Rust Chat App - Development Script
# This script runs both frontend and backend in development mode with database support
set -e # Exit on any error
echo "🚀 Starting Development Environment..."
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${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 kill background processes on exit
cleanup() {
echo ""
print_status "Shutting down development servers..."
# Kill all background jobs
if [ -n "$(jobs -p)" ]; then
kill $(jobs -p) 2>/dev/null || true
fi
# Kill any remaining processes on the ports we use
pkill -f "cargo run" 2>/dev/null || true
pkill -f "vite" 2>/dev/null || true
print_success "Development servers stopped"
exit 0
}
# Set up signal handlers
trap cleanup SIGINT SIGTERM
# Check if we're in the right directory
if [ ! -d "backend" ] || [ ! -d "frontend" ]; then
print_error "Please run this script from the project root directory"
exit 1
fi
# Parse command line arguments
BACKEND_ONLY=false
FRONTEND_ONLY=false
BACKEND_PORT=3000
FRONTEND_PORT=5173
INIT_DB=false
NO_DB=false
while [[ $# -gt 0 ]]; do
case $1 in
--backend-only)
BACKEND_ONLY=true
shift
;;
--frontend-only)
FRONTEND_ONLY=true
shift
;;
--backend-port)
BACKEND_PORT="$2"
shift 2
;;
--frontend-port)
FRONTEND_PORT="$2"
shift 2
;;
--init-db)
INIT_DB=true
shift
;;
--no-db)
NO_DB=true
shift
;;
--help|-h)
echo "Usage: $0 [OPTIONS]"
echo "Options:"
echo " --backend-only Run only the backend server"
echo " --frontend-only Run only the frontend dev server"
echo " --backend-port PORT Set backend port (default: 8080)"
echo " --frontend-port PORT Set frontend port (default: 5173)"
echo " --init-db Initialize database before starting"
echo " --no-db Run without database (memory only)"
echo " --help Show this help message"
exit 0
;;
*)
print_error "Unknown option: $1"
echo "Use --help for usage information"
exit 1
;;
esac
done
# Database initialization
if [ "$INIT_DB" = true ] && [ "$NO_DB" != true ]; then
print_status "Initializing database..."
touch backend/chatx.db
cd backend
sqlx migrate run
fi
# Set database environment variable
if [ "$NO_DB" != true ]; then
export DATABASE_URL="sqlite:chatx.db"
else
print_warning "Running in memory-only mode (no database)"
export DATABASE_URL="sqlite::memory:"
fi
# Check if frontend/dist exists, if not build frontend
if [ ! -d "frontend/dist" ]; then
print_status "frontend/dist directory not found, building frontend..."
cd frontend
npm run build
if [ $? -eq 0 ]; then
print_success "Frontend build completed"
else
print_error "Frontend build failed"
exit 1
fi
cd ..
else
print_status "frontend/dist directory found, skipping build"
fi
# Start backend if not frontend-only
if [ "$FRONTEND_ONLY" != true ]; then
print_status "Starting backend server on port $BACKEND_PORT..."
# Check if backend directory and files exist
if [ ! -f "backend/Cargo.toml" ]; then
print_error "Backend Cargo.toml not found"
exit 1
fi
cd backend
# Set the port environment variable if specified
if [ "$BACKEND_PORT" != "3000" ]; then
export PORT=$BACKEND_PORT
fi
# Set database environment variable for backend
if [ "$NO_DB" != true ]; then
export DATABASE_URL="sqlite:../chatx.db"
else
export DATABASE_URL="sqlite::memory:"
fi
# Start backend in background
cargo run &
BACKEND_PID=$!
cd ..
print_success "Backend server started (PID: $BACKEND_PID)"
# Give backend a moment to start
sleep 2
fi
# Start frontend if not backend-only
if [ "$BACKEND_ONLY" != true ]; then
print_status "Starting frontend dev server on port $FRONTEND_PORT..."
# Check if frontend directory and files exist
if [ ! -f "frontend/package.json" ]; then
print_error "Frontend package.json not found"
exit 1
fi
cd frontend
# Install dependencies if node_modules doesn't exist
if [ ! -d "node_modules" ]; then
print_warning "node_modules not found, running npm install..."
npm install
if [ $? -ne 0 ]; then
print_error "npm install failed"
exit 1
fi
fi
# Start frontend in background
if [ "$FRONTEND_PORT" != "5173" ]; then
npm run dev -- --port $FRONTEND_PORT &
else
npm run dev &
fi
FRONTEND_PID=$!
cd ..
print_success "Frontend dev server started (PID: $FRONTEND_PID)"
fi
# Show running services
echo ""
print_success "🎉 Development environment is ready!"
echo ""
if [ "$FRONTEND_ONLY" != true ]; then
print_status "Backend API: http://localhost:$BACKEND_PORT"
fi
if [ "$BACKEND_ONLY" != true ]; then
print_status "Frontend: http://localhost:$FRONTEND_PORT"
fi
echo ""
print_status "Press Ctrl+C to stop all servers"
# Wait for user to stop the servers
while true; do
sleep 1
done