primer commit

This commit is contained in:
2026-01-17 21:43:47 -06:00
commit c8450ed5a8
40 changed files with 11170 additions and 0 deletions

308
deploy.sh Executable file
View File

@@ -0,0 +1,308 @@
#!/bin/bash
# WhatsApp Gateway - Deployment Script
# Para VPS Ubuntu/Debian
set -e
echo "🚀 WhatsApp Gateway Deployment Script"
echo "======================================"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Functions
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Check if running as root
if [[ $EUID -eq 0 ]]; then
log_error "This script should not be run as root!"
exit 1
fi
# Update system
log_info "Updating system packages..."
sudo apt update && sudo apt upgrade -y
# Install Node.js
log_info "Installing Node.js 18+..."
if ! command -v node &> /dev/null; then
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
else
log_warn "Node.js already installed: $(node --version)"
fi
# Install PM2
log_info "Installing PM2 process manager..."
if ! command -v pm2 &> /dev/null; then
sudo npm install -g pm2
else
log_warn "PM2 already installed: $(pm2 --version)"
fi
# Install Nginx (optional)
log_info "Installing Nginx..."
if ! command -v nginx &> /dev/null; then
sudo apt install -y nginx
else
log_warn "Nginx already installed: $(nginx -v 2>&1)"
fi
# Create deployment directory
DEPLOY_DIR="/opt/whatsapp-gateway"
log_info "Creating deployment directory: $DEPLOY_DIR"
sudo mkdir -p $DEPLOY_DIR
sudo chown $USER:$USER $DEPLOY_DIR
# Copy files (assuming script is run from project root)
log_info "Copying project files..."
cp -r gateway $DEPLOY_DIR/
cp -r manager $DEPLOY_DIR/
cp README.md $DEPLOY_DIR/
# Install dependencies
log_info "Installing Gateway dependencies..."
cd $DEPLOY_DIR/gateway
npm install
log_info "Installing Manager dependencies..."
cd $DEPLOY_DIR/manager
npm install
# Build Manager
log_info "Building Manager Web interface..."
npm run build
# Create PM2 ecosystem file
log_info "Creating PM2 ecosystem configuration..."
cat > $DEPLOY_DIR/ecosystem.config.js << 'EOF'
module.exports = {
apps: [
{
name: 'whatsapp-gateway',
script: './gateway/dist/index.js',
cwd: '/opt/whatsapp-gateway',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
NODE_ENV: 'production',
PORT: 3001
},
error_file: '/var/log/whatsapp-gateway-error.log',
out_file: '/var/log/whatsapp-gateway-out.log',
log_file: '/var/log/whatsapp-gateway-combined.log',
time: true
},
{
name: 'whatsapp-manager',
script: 'serve',
cwd: '/opt/whatsapp-gateway/manager',
args: '-s dist -l 3002',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '500M',
error_file: '/var/log/whatsapp-manager-error.log',
out_file: '/var/log/whatsapp-manager-out.log',
log_file: '/var/log/whatsapp-manager-combined.log',
time: true
}
]
};
EOF
# Install serve for Manager
cd $DEPLOY_DIR/manager
npm install serve
# Build Gateway
log_info "Building Gateway..."
cd $DEPLOY_DIR/gateway
npm run build
# Create auth_info directory
mkdir -p $DEPLOY_DIR/gateway/auth_info
# Set permissions
chmod +x $DEPLOY_DIR/gateway/auth_info
# Create log directory
sudo mkdir -p /var/log/whatsapp-gateway
sudo chown $USER:$USER /var/log/whatsapp-gateway
# Start services with PM2
log_info "Starting services with PM2..."
cd $DEPLOY_DIR
pm2 start ecosystem.config.js
# Save PM2 configuration
pm2 save
pm2 startup
# Configure Nginx
log_info "Configuring Nginx..."
sudo tee /etc/nginx/sites-available/whatsapp-gateway << 'EOF'
server {
listen 80;
server_name _;
# Manager Web
location / {
proxy_pass http://localhost:3002;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# API Gateway
location /api/ {
proxy_pass http://localhost:3001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# WebSocket
location /ws {
proxy_pass http://localhost:3003;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
EOF
# Enable Nginx site
sudo ln -sf /etc/nginx/sites-available/whatsapp-gateway /etc/nginx/sites-enabled/
sudo rm -f /etc/nginx/sites-enabled/default
# Test Nginx configuration
sudo nginx -t
# Restart Nginx
sudo systemctl restart nginx
sudo systemctl enable nginx
# Configure firewall
log_info "Configuring firewall..."
sudo ufw --force reset
sudo ufw allow 22 # SSH
sudo ufw allow 80 # HTTP
sudo ufw allow 443 # HTTPS
sudo ufw --force enable
# Create status script
log_info "Creating status monitoring script..."
cat > $DEPLOY_DIR/status.sh << 'EOF'
#!/bin/bash
echo "🔍 WhatsApp Gateway Status"
echo "=========================="
# Check PM2 processes
echo -e "\n📊 PM2 Processes:"
pm2 status
# Check service health
echo -e "\n🏥 Service Health:"
curl -s http://localhost:3001/api/health | jq '.' 2>/dev/null || echo "❌ Gateway API not responding"
# Check logs
echo -e "\n📝 Recent Logs:"
pm2 logs whatsapp-gateway --lines 10 --nostream
# Check system resources
echo -e "\n💻 System Resources:"
echo "Memory: $(free -h | grep '^Mem:' | awk '{print $3 "/" $2}')"
echo "Disk: $(df -h / | tail -1 | awk '{print $3 "/" $2}')"
echo "Uptime: $(uptime -p)"
EOF
chmod +x $DEPLOY_DIR/status.sh
# Create update script
log_info "Creating update script..."
cat > $DEPLOY_DIR/update.sh << 'EOF'
#!/bin/bash
echo "🔄 Updating WhatsApp Gateway..."
echo "=============================="
# Backup current version
BACKUP_DIR="/opt/whatsapp-gateway-backup-$(date +%Y%m%d-%H%M%S)"
sudo mkdir -p $BACKUP_DIR
sudo cp -r /opt/whatsapp-gateway/* $BACKUP_DIR/
# Pull latest changes (if git repo)
cd /opt/whatsapp-gateway
if [ -d ".git" ]; then
git pull origin main
fi
# Rebuild and restart
cd gateway
npm install
npm run build
cd ../manager
npm install
npm run build
# Restart services
pm2 restart whatsapp-gateway whatsapp-manager
echo "✅ Update completed!"
echo "📁 Backup available at: $BACKUP_DIR"
EOF
chmod +x $DEPLOY_DIR/update.sh
# Final status
log_info "Deployment completed successfully!"
echo ""
echo -e "${GREEN}🎉 WhatsApp Gateway is now running!${NC}"
echo ""
echo "📍 Access URLs:"
echo " Manager Web: http://$(curl -s ifconfig.me)/"
echo " API: http://$(curl -s ifconfig.me)/api/"
echo " WebSocket: ws://$(curl -s ifconfig.me)/ws"
echo ""
echo "🔧 Management Commands:"
echo " Status: $DEPLOY_DIR/status.sh"
echo " Update: $DEPLOY_DIR/update.sh"
echo " PM2: pm2 status | pm2 logs | pm2 restart"
echo ""
echo "📁 Important Paths:"
echo " Project: $DEPLOY_DIR"
echo " Logs: /var/log/whatsapp-gateway/"
echo " Auth: $DEPLOY_DIR/gateway/auth_info/"
echo ""
echo -e "${YELLOW}⚠️ Next Steps:${NC}"
echo " 1. Open Manager Web in browser"
echo " 2. Scan QR code with WhatsApp"
echo " 3. Test API endpoints"
echo " 4. Configure n8n integration"
echo ""
echo -e "${GREEN}✅ All services are running and ready!${NC}"