#!/bin/bash # Cloudflare Tunnel Setup Script for PMFlow set -e echo "🚀 Starting Cloudflare Tunnel Setup..." # Check if cloudflared is installed if ! command -v cloudflared &> /dev/null; then echo "❌ cloudflared is not installed. Please install it first:" echo " brew install cloudflared" exit 1 fi # Ask for configuration read -p "Enter your Frontend domain (e.g., pmflow.com): " FRONTEND_DOMAIN read -p "Enter your Backend domain (e.g., api.pmflow.com): " BACKEND_DOMAIN read -p "Enter a name for your tunnel (e.g., pmflow-tunnel): " TUNNEL_NAME if [ -z "$FRONTEND_DOMAIN" ] || [ -z "$BACKEND_DOMAIN" ] || [ -z "$TUNNEL_NAME" ]; then echo "❌ Frontend domain, Backend domain, and Tunnel name are required." exit 1 fi echo "Checking authentication..." # Check if cert.pem exists in default location if [ ! -f "$HOME/.cloudflared/cert.pem" ]; then echo "âš ī¸ You need to login to Cloudflare first." cloudflared tunnel login fi echo "Creating tunnel '$TUNNEL_NAME'..." # Check if tunnel exists, if not create it if ! cloudflared tunnel list | grep -q "$TUNNEL_NAME"; then cloudflared tunnel create "$TUNNEL_NAME" else echo "â„šī¸ Tunnel '$TUNNEL_NAME' already exists." fi echo "Routing DNS..." # Route DNS for both domains cloudflared tunnel route dns "$TUNNEL_NAME" "$FRONTEND_DOMAIN" cloudflared tunnel route dns "$TUNNEL_NAME" "$BACKEND_DOMAIN" # Get Tunnel ID TUNNEL_ID=$(cloudflared tunnel list | grep "$TUNNEL_NAME" | awk '{print $1}') if [ -z "$TUNNEL_ID" ]; then echo "❌ Could not find Tunnel ID for '$TUNNEL_NAME'" exit 1 fi echo "Generating config file..." # Create config.yml mkdir -p devops/tunnel cat > devops/tunnel/config.yml <> "$ENV_FILE" fi else sed -i "s|VITE_APP_DOMAIN=.*|VITE_APP_DOMAIN=$FRONTEND_DOMAIN|" "$ENV_FILE" if grep -q "VITE_API_BASE_URL=" "$ENV_FILE"; then sed -i "s|VITE_API_BASE_URL=.*|VITE_API_BASE_URL=https://$BACKEND_DOMAIN|" "$ENV_FILE" else echo "VITE_API_BASE_URL=https://$BACKEND_DOMAIN" >> "$ENV_FILE" fi fi if ! grep -q "VITE_APP_DOMAIN=" "$ENV_FILE"; then echo "VITE_APP_DOMAIN=$FRONTEND_DOMAIN" >> "$ENV_FILE" fi echo "✅ Updated $ENV_FILE with:" echo " VITE_APP_DOMAIN=$FRONTEND_DOMAIN" echo " VITE_API_BASE_URL=https://$BACKEND_DOMAIN" echo "🎉 Setup complete! You can now run the tunnel with:" echo " ./devops/tunnel/start.sh"