-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·461 lines (398 loc) · 13.9 KB
/
setup.sh
File metadata and controls
executable file
·461 lines (398 loc) · 13.9 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#!/bin/bash
# Tournament Hub Setup and Run Script
# This script helps you set up and run the tournament hub project
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
# 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"
}
# Environment presets
# local -> Frontend local (Vite), Backend local (uvicorn on 8000)
# devnet -> Frontend local (Vite), Backend remote (https://devnet-tools.multiversx.com/tournament-hub)
# cloudflare -> Frontend built for Cloudflare, Backend remote (https://devnet-tools.multiversx.com/tournament-hub)
ENV_PRESET="${ENV_PRESET:-local}"
# Load env config if present
load_env_config() {
if [ -f "config.sh" ]; then
print_status "Loading environment from config.sh"
# shellcheck disable=SC1091
source ./config.sh
if [ -n "${ENV_PRESET:-}" ]; then
print_status "Using ENV_PRESET='${ENV_PRESET}' from environment/config"
fi
else
print_warning "config.sh not found. Using current shell environment."
fi
}
# Show notifier configuration summary
show_notifier_config() {
if [ -n "${MX_AMQP_USER:-}" ] || [ -n "${MX_AMQP_URL:-}" ]; then
print_status "Event Notifier subscriber: RabbitMQ (AMQP)"
echo " Host: ${MX_AMQP_HOST:-devnet-external-k8s-proxy.multiversx.com}"
echo " Port: ${MX_AMQP_PORT:-30006}"
echo " VHost: ${MX_AMQP_VHOST:-devnet2}"
echo " Exchange: ${MX_AMQP_EXCHANGE:-all_events}"
echo " Queue: ${MX_AMQP_QUEUE:-costin_queue_temporary}"
else
print_status "Event Notifier subscriber: WebSocket"
echo " WS URL: ${MX_NOTIFIER_WS_URL:-ws://localhost:5000/hub/ws}"
fi
if [ -n "${MX_TOURNAMENT_CONTRACT:-}" ]; then
echo " Contract filter: ${MX_TOURNAMENT_CONTRACT}"
else
echo " Contract filter: (none)"
fi
}
# Configure frontend environment for a given preset
configure_frontend_env() {
local preset="$1"
print_status "Configuring frontend env for preset: ${preset}"
pushd tournament-hub-frontend >/dev/null
case "$preset" in
local)
# Use local backend via Vite proxy
echo "VITE_BACKEND_URL=/api" > .env.local
;;
devnet|remote)
# Use remote devnet backend
echo "VITE_BACKEND_URL=https://devnet-tools.multiversx.com/tournament-hub" > .env.local
;;
cloudflare)
# Cloudflare build targets remote backend
echo "VITE_BACKEND_URL=https://devnet-tools.multiversx.com/tournament-hub" > .env.local
;;
*)
print_warning "Unknown preset '$preset'. Falling back to local."
echo "VITE_BACKEND_URL=/api" > .env.local
;;
esac
print_success "Wrote tournament-hub-frontend/.env.local"
popd >/dev/null
}
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to check if a port is in use
port_in_use() {
lsof -i :$1 >/dev/null 2>&1
}
# Function to kill process on port
kill_port() {
if port_in_use $1; then
print_warning "Port $1 is in use. Killing existing process..."
lsof -ti:$1 | xargs kill -9
sleep 2
fi
}
# Function to cleanup processes on exit
cleanup_processes() {
print_status "Cleaning up processes..."
if [ -n "$BACKEND_PID" ] && kill -0 $BACKEND_PID 2>/dev/null; then
print_status "Stopping backend server (PID: $BACKEND_PID)..."
kill $BACKEND_PID 2>/dev/null
fi
if [ -n "$FRONTEND_PID" ] && kill -0 $FRONTEND_PID 2>/dev/null; then
print_status "Stopping frontend server (PID: $FRONTEND_PID)..."
kill $FRONTEND_PID 2>/dev/null
fi
# Also kill by port as backup
kill_port 8000
kill_port 3000
print_success "Cleanup complete!"
exit 0
}
# Function to setup backend
setup_backend() {
print_status "Setting up backend..."
cd tournament-hub-game-server
# Check if Python virtual environment exists
if [ ! -d "venv" ]; then
print_status "Creating Python virtual environment..."
python3 -m venv venv
fi
# Activate virtual environment
print_status "Activating virtual environment..."
source venv/bin/activate
# Install dependencies
print_status "Installing Python dependencies..."
pip install -r requirements.txt
print_success "Backend setup complete!"
cd ..
}
# Function to setup frontend
setup_frontend() {
print_status "Setting up frontend..."
cd tournament-hub-frontend
# Check if node_modules exists
if [ ! -d "node_modules" ]; then
print_status "Installing Node.js dependencies..."
npm install
fi
print_success "Frontend setup complete!"
cd ..
}
# Function to build smart contract
build_contract() {
print_status "Building smart contract..."
cd tournament-hub-sc
# Check if sc-meta is available
if ! command_exists sc-meta; then
print_error "sc-meta is not installed. Please install MultiversX SDK first."
print_status "Visit: https://docs.multiversx.com/developers/setup/overview/"
exit 1
fi
# Build the contract
print_status "Building contract..."
sc-meta all build
# Generate proxy
print_status "Generating proxy..."
sc-meta all proxy
print_success "Smart contract build complete!"
cd ..
}
# Function to run backend
run_backend() {
print_status "Starting backend server..."
cd tournament-hub-game-server
# Kill existing process on port 8000
kill_port 8000
# Ensure virtual environment exists
if [ ! -d "venv" ]; then
print_status "Creating Python virtual environment..."
python3 -m venv venv
fi
# Activate virtual environment
source venv/bin/activate
# Load environment variables from env.local if it exists
if [ -f "env.local" ]; then
print_status "Loading environment variables from env.local..."
source env.local
# Export all variables to make them available to the Python process
export $(cat env.local | grep -v '^#' | grep -v '^$' | xargs)
else
print_warning "env.local not found. Using default environment variables."
fi
# Ensure dependencies (update in case requirements changed)
print_status "Installing/updating Python dependencies..."
pip install -r requirements.txt
# Start the server
print_status "Starting FastAPI server on port 8000..."
python main.py &
BACKEND_PID=$!
export BACKEND_PID
# Wait for server to start with multiple attempts
print_status "Waiting for backend server to start..."
local attempts=0
local max_attempts=30 # 30 seconds total
while [ $attempts -lt $max_attempts ]; do
if curl -s http://localhost:8000/ >/dev/null 2>&1; then
print_success "Backend server started successfully!"
print_status "Backend API docs: http://localhost:8000/docs"
cd ..
return 0
fi
sleep 1
attempts=$((attempts + 1))
if [ $((attempts % 5)) -eq 0 ]; then
print_status "Still waiting for backend server... (${attempts}s)"
fi
done
print_error "Failed to start backend server after ${max_attempts} seconds"
print_error "Please check the server logs for errors"
cd ..
exit 1
}
# Function to run frontend
run_frontend() {
print_status "Starting frontend development server..."
cd tournament-hub-frontend
# Kill existing process on port 3000
kill_port 3000
# Export VITE_BACKEND_URL based on preset to ensure dev server picks it up
case "${ENV_PRESET}" in
devnet|remote)
export VITE_BACKEND_URL="https://devnet-tools.multiversx.com/tournament-hub"
;;
cloudflare)
export VITE_BACKEND_URL="https://devnet-tools.multiversx.com/tournament-hub"
;;
*)
export VITE_BACKEND_URL="/api"
;;
esac
print_status "VITE_BACKEND_URL=${VITE_BACKEND_URL}"
# Start the development server
print_status "Starting Vite dev server on port 3000 (HTTPS)..."
PORT=3000 npm run dev &
FRONTEND_PID=$!
export FRONTEND_PID
# Wait for server to become ready (retry for up to 30 seconds)
READY=false
for i in {1..30}; do
# Vite is configured with HTTPS; -k ignores self-signed cert
if curl -k -s https://localhost:3000 >/dev/null 2>&1; then
READY=true
break
fi
sleep 1
done
if [ "$READY" = true ]; then
print_success "Frontend server started successfully!"
print_status "Frontend URL: https://localhost:3000"
else
print_error "Failed to start frontend server"
print_status "Tip: If you just changed ports or certificates, try running: npm run dev inside tournament-hub-frontend to see logs."
exit 1
fi
cd ..
}
# Function to stop all services
stop_all() {
print_status "Stopping all services..."
# Kill processes on common ports
kill_port 8000
kill_port 3000
print_success "All services stopped!"
}
# Function to show status
show_status() {
print_status "Checking service status..."
if port_in_use 8000; then
print_success "Backend server is running on port 8000"
else
print_warning "Backend server is not running"
fi
if port_in_use 3000; then
print_success "Frontend server is running on port 3000"
else
print_warning "Frontend server is not running"
fi
}
# Function to show help
show_help() {
echo "Tournament Hub Setup and Run Script"
echo ""
echo "Usage: $0 [COMMAND]"
echo ""
echo "Commands:"
echo " setup - Set up the entire project (backend, frontend, contract)"
echo " run - Run backend (with notifier subscriber) and frontend servers"
echo " backend - Set up and run only the backend server"
echo " frontend - Set up and run only the frontend server"
echo " contract - Build the smart contract"
echo " stop - Stop all running services"
echo " status - Show status of running services"
echo " help - Show this help message"
echo ""
echo "Environment presets (set ENV_PRESET before command):"
echo " local - Frontend local (Vite), Backend local (default)"
echo " devnet - Frontend local (Vite), Backend remote (devnet tools)"
echo " cloudflare - Frontend build for Cloudflare, Backend remote (devnet tools)"
echo ""
echo "Examples:"
echo " $0 setup # Set up everything"
echo " $0 run # Run local frontend + backend"
echo " ENV_PRESET=devnet $0 run # Run local frontend against devnet backend"
echo " ENV_PRESET=cloudflare $0 setup # Prepare env for Cloudflare build"
echo " $0 backend # Run only backend"
echo " $0 stop # Stop all services"
}
# Main script logic
case "${1:-help}" in
"setup")
print_status "Setting up Tournament Hub project..."
load_env_config
configure_frontend_env "${ENV_PRESET}"
setup_backend
setup_frontend
build_contract
print_success "Setup complete! Run '$0 run' to start the application."
;;
"run")
print_status "Starting Tournament Hub application..."
load_env_config
configure_frontend_env "${ENV_PRESET}"
show_notifier_config
# Initialize PIDs
BACKEND_PID=""
FRONTEND_PID=""
if [ "${ENV_PRESET}" = "local" ]; then
run_backend
run_frontend
elif [ "${ENV_PRESET}" = "devnet" ] || [ "${ENV_PRESET}" = "remote" ]; then
print_status "Preset '${ENV_PRESET}': using remote backend. Running only frontend."
run_frontend
elif [ "${ENV_PRESET}" = "cloudflare" ]; then
print_status "Preset 'cloudflare': configure env for build. Not starting local servers."
print_status "To build for Cloudflare: cd tournament-hub-frontend && npm run build"
exit 0
else
print_warning "Unknown preset '${ENV_PRESET}', defaulting to local."
run_backend
run_frontend
fi
print_success "Tournament Hub is now running!"
print_status "Frontend: https://localhost:3000"
if [ "${ENV_PRESET}" = "local" ]; then
print_status "Backend API: http://localhost:8000"
print_status "Backend Docs: http://localhost:8000/docs"
else
print_status "Backend API: https://devnet-tools.multiversx.com/tournament-hub"
print_status "Backend Docs: https://devnet-tools.multiversx.com/tournament-hub/openapi.json"
fi
print_status "Press Ctrl+C to stop all services"
# Set up signal handler for cleanup
trap 'cleanup_processes' INT TERM
# Wait for processes
if [ -n "$BACKEND_PID" ] && [ -n "$FRONTEND_PID" ]; then
wait $BACKEND_PID $FRONTEND_PID
elif [ -n "$BACKEND_PID" ]; then
wait $BACKEND_PID
elif [ -n "$FRONTEND_PID" ]; then
wait $FRONTEND_PID
else
wait
fi
;;
"backend")
load_env_config
setup_backend
run_backend
print_status "Backend is running. Press Ctrl+C to stop."
wait $BACKEND_PID
;;
"frontend")
setup_frontend
run_frontend
print_status "Frontend is running. Press Ctrl+C to stop."
wait $FRONTEND_PID
;;
"contract")
build_contract
;;
"stop")
stop_all
;;
"status")
show_status
;;
"help"|*)
show_help
;;
esac