-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmanage.sh
More file actions
executable file
·287 lines (242 loc) · 7.48 KB
/
Copy pathmanage.sh
File metadata and controls
executable file
·287 lines (242 loc) · 7.48 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
#!/bin/bash
# Douyin Analytics Platform Management Script
# Uses xvfb virtual display for server-side QR code scanning
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Project directories
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BACKEND_DIR="$SCRIPT_DIR/backend"
FRONTEND_DIR="$SCRIPT_DIR/frontend"
# Print colored message
print_msg() {
local color=$1
local msg=$2
echo -e "${color}${msg}${NC}"
}
# Check if command exists
check_command() {
if ! command -v "$1" &> /dev/null; then
print_msg "$RED" "Error: $1 is not installed"
return 1
fi
return 0
}
# Check prerequisites
check_prerequisites() {
print_msg "$BLUE" "Checking prerequisites..."
local missing=0
# Check Python
if ! check_command python3; then
missing=1
fi
# Check Node.js
if ! check_command node; then
missing=1
fi
# Check xvfb-run (for server-side QR scanning)
if ! check_command xvfb-run; then
print_msg "$YELLOW" "Warning: xvfb-run not found. Server-side QR scanning may not work."
print_msg "$YELLOW" "Install with: sudo apt-get install xvfb"
fi
if [ $missing -eq 1 ]; then
print_msg "$RED" "Please install missing dependencies first"
exit 1
fi
print_msg "$GREEN" "Prerequisites check passed"
}
# Start backend with xvfb (for QR code scanning support)
start_backend() {
print_msg "$BLUE" "Starting backend with xvfb virtual display..."
cd "$BACKEND_DIR"
# Check if xvfb-run is available
if command -v xvfb-run &> /dev/null; then
print_msg "$GREEN" "Using xvfb for virtual display support"
# Start with xvfb-run for non-headless browser support
xvfb-run -a --server-args="-screen 0 1920x1080x24" python3 run.py &
else
print_msg "$YELLOW" "xvfb not available, starting without virtual display"
print_msg "$YELLOW" "Server-side QR code scanning may not work correctly"
python3 run.py &
fi
BACKEND_PID=$!
echo $BACKEND_PID > "$SCRIPT_DIR/.backend.pid"
print_msg "$GREEN" "Backend started (PID: $BACKEND_PID)"
}
# Start backend without xvfb (headless mode)
start_backend_headless() {
print_msg "$BLUE" "Starting backend in headless mode..."
cd "$BACKEND_DIR"
python3 run.py &
BACKEND_PID=$!
echo $BACKEND_PID > "$SCRIPT_DIR/.backend.pid"
print_msg "$GREEN" "Backend started in headless mode (PID: $BACKEND_PID)"
}
# Start frontend
start_frontend() {
print_msg "$BLUE" "Starting frontend..."
cd "$FRONTEND_DIR"
# Install dependencies if node_modules doesn't exist
if [ ! -d "node_modules" ]; then
print_msg "$YELLOW" "Installing frontend dependencies..."
npm install
fi
npm run dev &
FRONTEND_PID=$!
echo $FRONTEND_PID > "$SCRIPT_DIR/.frontend.pid"
print_msg "$GREEN" "Frontend started (PID: $FRONTEND_PID)"
}
# Stop all services
stop_all() {
print_msg "$BLUE" "Stopping all services..."
# Stop backend
if [ -f "$SCRIPT_DIR/.backend.pid" ]; then
BACKEND_PID=$(cat "$SCRIPT_DIR/.backend.pid")
if kill -0 $BACKEND_PID 2>/dev/null; then
kill $BACKEND_PID
print_msg "$GREEN" "Backend stopped"
fi
rm -f "$SCRIPT_DIR/.backend.pid"
fi
# Stop frontend
if [ -f "$SCRIPT_DIR/.frontend.pid" ]; then
FRONTEND_PID=$(cat "$SCRIPT_DIR/.frontend.pid")
if kill -0 $FRONTEND_PID 2>/dev/null; then
kill $FRONTEND_PID
print_msg "$GREEN" "Frontend stopped"
fi
rm -f "$SCRIPT_DIR/.frontend.pid"
fi
# Also try to kill any remaining processes
pkill -f "python3 run.py" 2>/dev/null || true
pkill -f "vite" 2>/dev/null || true
print_msg "$GREEN" "All services stopped"
}
# Start all services
start_all() {
check_prerequisites
stop_all
print_msg "$BLUE" "Starting all services..."
start_backend
sleep 2 # Wait for backend to initialize
start_frontend
print_msg "$GREEN" "All services started!"
print_msg "$BLUE" "Backend: http://localhost:8000"
print_msg "$BLUE" "Frontend: http://localhost:5173"
print_msg "$YELLOW" ""
print_msg "$YELLOW" "QR Code Scanning: The backend now uses xvfb for virtual display,"
print_msg "$YELLOW" "which enables server-side QR code scanning with anti-detection."
print_msg "$YELLOW" ""
print_msg "$YELLOW" "Press Ctrl+C to stop all services"
# Wait for interrupt
wait
}
# Install dependencies
install_deps() {
print_msg "$BLUE" "Installing dependencies..."
# Backend dependencies
print_msg "$YELLOW" "Installing backend dependencies..."
cd "$BACKEND_DIR"
pip3 install -r requirements.txt
# Install Playwright browsers
print_msg "$YELLOW" "Installing Playwright browsers..."
playwright install chromium
# Frontend dependencies
print_msg "$YELLOW" "Installing frontend dependencies..."
cd "$FRONTEND_DIR"
npm install
print_msg "$GREEN" "All dependencies installed!"
}
# Show status
show_status() {
print_msg "$BLUE" "Service Status:"
# Check backend
if [ -f "$SCRIPT_DIR/.backend.pid" ]; then
BACKEND_PID=$(cat "$SCRIPT_DIR/.backend.pid")
if kill -0 $BACKEND_PID 2>/dev/null; then
print_msg "$GREEN" "Backend: Running (PID: $BACKEND_PID)"
else
print_msg "$RED" "Backend: Not running (stale PID file)"
fi
else
print_msg "$RED" "Backend: Not running"
fi
# Check frontend
if [ -f "$SCRIPT_DIR/.frontend.pid" ]; then
FRONTEND_PID=$(cat "$SCRIPT_DIR/.frontend.pid")
if kill -0 $FRONTEND_PID 2>/dev/null; then
print_msg "$GREEN" "Frontend: Running (PID: $FRONTEND_PID)"
else
print_msg "$RED" "Frontend: Not running (stale PID file)"
fi
else
print_msg "$RED" "Frontend: Not running"
fi
}
# Show help
show_help() {
echo "Douyin Analytics Platform Management Script"
echo ""
echo "Usage: $0 <command>"
echo ""
echo "Commands:"
echo " start Start all services (backend with xvfb + frontend)"
echo " start-backend Start backend only (with xvfb virtual display)"
echo " start-headless Start backend in headless mode (no xvfb)"
echo " start-frontend Start frontend only"
echo " stop Stop all services"
echo " restart Restart all services"
echo " status Show service status"
echo " install Install all dependencies"
echo " help Show this help message"
echo ""
echo "Notes:"
echo " - Backend uses xvfb for virtual display to support server-side QR scanning"
echo " - Make sure xvfb is installed: sudo apt-get install xvfb"
echo " - Playwright browsers must be installed: playwright install chromium"
}
# Main command handler
case "$1" in
start)
start_all
;;
start-backend)
check_prerequisites
start_backend
;;
start-headless)
check_prerequisites
start_backend_headless
;;
start-frontend)
check_prerequisites
start_frontend
;;
stop)
stop_all
;;
restart)
stop_all
sleep 1
start_all
;;
status)
show_status
;;
install)
install_deps
;;
help|--help|-h)
show_help
;;
*)
print_msg "$RED" "Unknown command: $1"
echo ""
show_help
exit 1
;;
esac