File size: 1,918 Bytes
6d20eab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash

# Set paths
BACKEND_DIR="/workspaces/arf-api"
FRONTEND_DIR="/workspaces/arf-frontend"
VENV_ACTIVATE="$BACKEND_DIR/venv/bin/activate"
CLOUDFLARED=$(which cloudflared 2>/dev/null || echo "/usr/local/bin/cloudflared")

# Kill any existing processes
echo "πŸ›‘ Stopping existing uvicorn and cloudflared..."
pkill -f uvicorn
pkill -f cloudflared
sleep 2

# Start uvicorn
echo "πŸš€ Starting uvicorn..."
cd "$BACKEND_DIR"
source "$VENV_ACTIVATE"
uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload &
sleep 3

# Verify uvicorn is running
if ! curl -s http://localhost:8000/health >/dev/null; then
    echo "❌ uvicorn failed to start. Exiting."
    exit 1
fi
echo "βœ… uvicorn is running."

# Start cloudflared and capture URL
echo "🌐 Starting cloudflared tunnel..."
TEMP_FILE=$(mktemp)
$CLOUDFLARED tunnel --url http://localhost:8000 2>&1 | tee "$TEMP_FILE" &

# Wait for URL to appear
echo "⏳ Waiting for tunnel URL..."
URL=""
for i in {1..30}; do
    URL=$(grep -oP 'https://[a-z0-9-]+\.trycloudflare\.com' "$TEMP_FILE" | head -1)
    if [ -n "$URL" ]; then
        break
    fi
    sleep 1
done

if [ -z "$URL" ]; then
    echo "❌ Failed to get tunnel URL."
    exit 1
fi
echo "βœ… Tunnel URL: $URL"

# Save URL for monitoring (used by monitor.sh)
echo "$URL" > /workspaces/arf-api/current_url.txt

# Update Vercel environment variable
echo "πŸ”§ Updating Vercel environment variable..."
cd "$FRONTEND_DIR"
if command -v vercel &>/dev/null; then
    vercel env rm NEXT_PUBLIC_API_URL production -y
    echo "$URL" | vercel env add NEXT_PUBLIC_API_URL production
    echo "πŸ”„ Redeploying frontend..."
    vercel --prod
else
    echo "⚠️  Vercel CLI not installed. Please install it with: npm i -g vercel"
    echo "Then manually update the env var to: $URL"
fi

echo "πŸŽ‰ All done! Your new URL is: $URL"
echo "Frontend will be updated shortly. Check https://arf-frontend-sandy.vercel.app"