Deploy: Migrate to Gitea Actions, update paths for turbotrades.dev
Some checks failed
Deploy to Production / Deploy to turbotrades.dev (push) Has been cancelled

This commit is contained in:
2026-01-11 00:15:14 +00:00
parent 53d0c89d17
commit f9156897ce
6 changed files with 1567 additions and 140 deletions

174
.gitea/workflows/deploy.yml Normal file
View File

@@ -0,0 +1,174 @@
name: Deploy to Production
on:
push:
branches:
- main
workflow_dispatch:
jobs:
deploy:
name: Deploy to turbotrades.dev
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
cache: "npm"
- name: Install Backend Dependencies
run: npm ci --production
- name: Install Frontend Dependencies
run: |
cd frontend
npm ci
- name: Build Frontend
run: |
cd frontend
npm run build
env:
NODE_ENV: production
VITE_API_URL: https://api.turbotrades.dev
VITE_WS_URL: https://ws.turbotrades.dev
- name: Deploy to Production
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: ${{ secrets.SERVER_PORT || 22 }}
script: |
echo "🚀 Starting deployment to turbotrades.dev..."
# Create directories if they don't exist
mkdir -p /root/ttbackend
mkdir -p /root/ttbackend/logs
mkdir -p /var/www/html/turbotrades
# Navigate to backend directory
cd /root/ttbackend
# Stop the application
echo "⏸️ Stopping backend..."
pm2 stop turbotrades-backend || echo "Backend not running"
# Backup current version
echo "💾 Creating backup..."
if [ -d "/root/ttbackend-backup" ]; then
rm -rf /root/ttbackend-backup-old
mv /root/ttbackend-backup /root/ttbackend-backup-old
fi
mkdir -p /root/ttbackend-backup
cp -r /root/ttbackend/* /root/ttbackend-backup/ 2>/dev/null || true
# Clone or pull repository
if [ -d ".git" ]; then
echo "📥 Pulling latest code..."
git fetch origin
git reset --hard origin/main
git clean -fd
else
echo "📥 Cloning repository..."
cd /root
rm -rf ttbackend/*
git clone https://git.turbotrades.dev/iDefineHD/TurboTrades.git ttbackend
cd ttbackend
fi
# Install backend dependencies
echo "📦 Installing backend dependencies..."
npm ci --production
# Build frontend
echo "🎨 Building frontend..."
cd frontend
npm ci
npm run build
# Deploy frontend to nginx directory
echo "🚀 Deploying frontend to /var/www/html/turbotrades..."
rm -rf /var/www/html/turbotrades/*
cp -r dist/* /var/www/html/turbotrades/
# Set proper permissions
chown -R www-data:www-data /var/www/html/turbotrades
chmod -R 755 /var/www/html/turbotrades
# Back to backend directory
cd /root/ttbackend
# Start backend with PM2
echo "▶️ Starting backend..."
pm2 start ecosystem.config.js --env production
pm2 save
# Verify deployment
echo "✅ Deployment complete!"
echo ""
echo "📊 PM2 Status:"
pm2 list
echo ""
echo "🌐 Frontend: https://turbotrades.dev"
echo "🔧 Backend: https://api.turbotrades.dev"
echo "💬 WebSocket: https://ws.turbotrades.dev"
- name: Health Check
run: |
echo "⏳ Waiting for services to start..."
sleep 15
echo "🏥 Checking backend health..."
curl -f https://api.turbotrades.dev/api/health || echo "⚠️ Backend health check failed"
echo "🏥 Checking frontend..."
curl -f https://turbotrades.dev || echo "⚠️ Frontend check failed"
- name: Notify Success
if: success()
run: |
echo "✅ Deployment successful!"
echo "🌐 Frontend: https://turbotrades.dev"
echo "🔧 Backend: https://api.turbotrades.dev"
echo "💬 WebSocket: https://ws.turbotrades.dev"
echo "📅 Time: $(date)"
- name: Notify Failure
if: failure()
run: |
echo "❌ Deployment failed!"
echo "Check the logs above for details"
- name: Rollback on Failure
if: failure()
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: ${{ secrets.SERVER_PORT || 22 }}
script: |
echo "🔄 Rolling back to previous version..."
if [ -d "/root/ttbackend-backup" ]; then
echo "📦 Restoring backend from backup..."
cd /root
rm -rf ttbackend
cp -r ttbackend-backup ttbackend
cd ttbackend
echo "▶️ Restarting backend..."
pm2 restart turbotrades-backend
pm2 save
echo "✅ Rollback complete"
else
echo "❌ No backup found!"
fi

View File

@@ -1,138 +0,0 @@
name: Deploy to Production Server
on:
push:
branches:
- main
workflow_dispatch: # Allow manual trigger
jobs:
deploy:
name: Deploy to 178.63.127.19
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
cache: "npm"
- name: Install dependencies (Backend)
run: |
npm ci
- name: Install dependencies (Frontend)
run: |
cd frontend
npm ci
- name: Build Frontend
run: |
cd frontend
npm run build
env:
NODE_ENV: production
VITE_API_URL: http://178.63.127.19:3000
- name: Run Tests (if any)
run: |
npm test || echo "No tests configured"
continue-on-error: true
- name: Deploy to Production Server
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: ${{ secrets.SERVER_PORT || 22 }}
script: |
echo "🚀 Starting deployment..."
# Navigate to project directory
cd ${{ secrets.DEPLOY_PATH || '/var/www/turbotrades' }}
# Stop the application
echo "⏸️ Stopping application..."
pm2 stop turbotrades || echo "App not running"
# Backup current version
echo "💾 Creating backup..."
cp -r . ../turbotrades-backup-$(date +%Y%m%d-%H%M%S) || true
# Pull latest code
echo "📥 Pulling latest code..."
git fetch origin
git reset --hard origin/main
# Install backend dependencies
echo "📦 Installing backend dependencies..."
npm ci --production
# Build and install frontend
echo "🎨 Building frontend..."
cd frontend
npm ci
npm run build
cd ..
# Run database migrations (if any)
echo "🗄️ Running migrations..."
npm run migrate || echo "No migrations to run"
# Start the application
echo "▶️ Starting application..."
pm2 start ecosystem.config.js || pm2 start index.js --name turbotrades
pm2 save
# Verify deployment
echo "✅ Deployment complete!"
pm2 list
- name: Health Check
run: |
sleep 10
curl -f http://178.63.127.19:3000/api/health || echo "⚠️ Health check failed"
- name: Notify Success
if: success()
run: |
echo "✅ Deployment successful!"
echo "🌐 Server: 178.63.127.19"
echo "👤 Repository: git.turbotrades.dev/iDefineHD/TurboTrades"
echo "📅 Time: $(date)"
- name: Notify Failure
if: failure()
run: |
echo "❌ Deployment failed!"
echo "Check the logs above for details"
- name: Rollback on Failure
if: failure()
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: ${{ secrets.SERVER_PORT || 22 }}
script: |
echo "🔄 Rolling back to previous version..."
cd ${{ secrets.DEPLOY_PATH || '/var/www/turbotrades' }}
# Find latest backup
BACKUP=$(ls -t ../turbotrades-backup-* | head -1)
if [ -n "$BACKUP" ]; then
echo "Found backup: $BACKUP"
rm -rf ./*
cp -r $BACKUP/* .
pm2 restart turbotrades
echo "✅ Rollback complete"
else
echo "❌ No backup found!"
fi

317
DEPLOY.md Normal file
View File

@@ -0,0 +1,317 @@
# 🚀 TurboTrades - Simple Deployment
Quick deployment guide for TurboTrades on turbotrades.dev.
---
## 📋 Server Setup (Already Done)
- **Frontend:** https://turbotrades.dev → `/var/www/html/turbotrades`
- **Backend:** https://api.turbotrades.dev → Node.js on port 3000
- **WebSocket:** https://ws.turbotrades.dev → Node.js on port 3000
- **Backend Code:** `/root/ttbackend`
- **Nginx:** Already configured
- **SSL:** Already configured
---
## 🎯 Quick Deploy
### SSH to Server and Run Deploy Script
```bash
# SSH into server
ssh root@turbotrades.dev
# Navigate to backend directory
cd /root/ttbackend
# Pull latest code
git pull origin main
# Run deploy script
./deploy.sh
```
That's it! ✅
---
## 🔄 What the Deploy Script Does
1. ⏸️ Stops the backend (PM2)
2. 💾 Creates a backup
3. 📥 Pulls latest code from Gitea
4. 📦 Installs backend dependencies
5. 🎨 Builds frontend
6. 🚀 Deploys frontend to `/var/www/html/turbotrades`
7. ▶️ Restarts backend with PM2
8. ✅ Runs health checks
---
## 🛠️ Manual Deploy (Without Script)
```bash
# SSH to server
ssh root@turbotrades.dev
# Stop backend
pm2 stop turbotrades-backend
# Navigate to backend
cd /root/ttbackend
# Pull latest code
git pull origin main
# Install backend dependencies
npm ci --production
# Build frontend
cd frontend
npm ci
npm run build
# Deploy frontend
rm -rf /var/www/html/turbotrades/*
cp -r dist/* /var/www/html/turbotrades/
chown -R www-data:www-data /var/www/html/turbotrades
# Back to backend
cd /root/ttbackend
# Restart backend
pm2 restart turbotrades-backend
pm2 save
# Check status
pm2 logs turbotrades-backend
```
---
## 📊 Monitoring
### Check Backend Status
```bash
pm2 list
pm2 logs turbotrades-backend
pm2 monit
```
### Check Nginx Logs
```bash
tail -f /var/log/nginx/turbotrades-error.log
tail -f /var/log/nginx/api-turbotrades-error.log
tail -f /var/log/nginx/ws-turbotrades-error.log
```
### Health Checks
```bash
# Backend API
curl https://api.turbotrades.dev/api/health
# Frontend
curl https://turbotrades.dev
# WebSocket (requires wscat)
wscat -c wss://ws.turbotrades.dev
```
---
## 🐛 Quick Troubleshooting
### Backend Won't Start
```bash
# Check logs
pm2 logs turbotrades-backend
# Check if port 3000 is free
netstat -tulpn | grep 3000
# Check MongoDB
systemctl status mongod
# Restart everything
pm2 restart turbotrades-backend
```
### Frontend Not Loading
```bash
# Check if files exist
ls -la /var/www/html/turbotrades/
# Check permissions
chown -R www-data:www-data /var/www/html/turbotrades
chmod -R 755 /var/www/html/turbotrades
# Check nginx
nginx -t
systemctl reload nginx
```
### 502 Bad Gateway
```bash
# Backend is probably down
pm2 restart turbotrades-backend
# Check backend is listening
netstat -tulpn | grep 3000
# Check nginx logs
tail -f /var/log/nginx/api-turbotrades-error.log
```
---
## 🔄 Rollback
```bash
# SSH to server
ssh root@turbotrades.dev
# Stop current version
pm2 stop turbotrades-backend
# List backups
ls -lt /root/ | grep ttbackend-backup
# Restore from backup
cd /root
rm -rf ttbackend
cp -r ttbackend-backup-YYYYMMDD-HHMMSS ttbackend
# Restart
cd ttbackend
pm2 restart turbotrades-backend
```
---
## 📝 Update .env
If you need to update environment variables:
```bash
ssh root@turbotrades.dev
cd /root/ttbackend
nano .env
# After editing, restart
pm2 restart turbotrades-backend
```
---
## 🎯 Deployment Workflow
### For Code Changes
```bash
# On your local machine
git add .
git commit -m "Your changes"
git push origin main
# On the server
ssh root@turbotrades.dev
cd /root/ttbackend
./deploy.sh
```
### For Environment Changes Only
```bash
ssh root@turbotrades.dev
cd /root/ttbackend
nano .env
pm2 restart turbotrades-backend
```
### For Frontend Only
```bash
ssh root@turbotrades.dev
cd /root/ttbackend
git pull origin main
cd frontend
npm ci
npm run build
rm -rf /var/www/html/turbotrades/*
cp -r dist/* /var/www/html/turbotrades/
chown -R www-data:www-data /var/www/html/turbotrades
```
### For Backend Only
```bash
ssh root@turbotrades.dev
cd /root/ttbackend
git pull origin main
npm ci --production
pm2 restart turbotrades-backend
```
---
## 🔐 Required Environment Variables
Make sure these are set in `/root/ttbackend/.env`:
```env
NODE_ENV=production
PORT=3000
FRONTEND_URL=https://turbotrades.dev
BACKEND_URL=https://api.turbotrades.dev
WS_URL=https://ws.turbotrades.dev
MONGODB_URI=mongodb://localhost:27017/turbotrades
JWT_SECRET=your-secret
REFRESH_TOKEN_SECRET=your-secret
SESSION_SECRET=your-secret
STEAM_API_KEY=your-key
STEAM_REALM=https://turbotrades.dev
STEAM_RETURN_URL=https://api.turbotrades.dev/auth/steam/return
```
---
## ✅ Post-Deployment Checklist
- [ ] Backend is running (`pm2 list`)
- [ ] Frontend loads (visit https://turbotrades.dev)
- [ ] API responds (`curl https://api.turbotrades.dev/api/health`)
- [ ] Steam login works
- [ ] WebSocket connects
- [ ] No errors in PM2 logs
- [ ] No errors in Nginx logs
---
## 🆘 Emergency Contacts
- **PM2 Documentation:** https://pm2.keymetrics.io/docs/usage/quick-start/
- **Nginx Documentation:** https://nginx.org/en/docs/
---
## 🎉 Done!
Your TurboTrades instance is now deployed and running!
- 🌐 **Frontend:** https://turbotrades.dev
- 🔧 **Backend:** https://api.turbotrades.dev
- 💬 **WebSocket:** https://ws.turbotrades.dev
**Happy Trading! 🚀**

371
DEPLOY_SUMMARY.md Normal file
View File

@@ -0,0 +1,371 @@
# 🚀 TurboTrades - Deployment Summary
**Date:** January 10, 2026
**Status:** ✅ Ready for Deployment
**Repository:** https://git.turbotrades.dev/iDefineHD/TurboTrades.git
---
## 📋 Configuration Overview
### Domain Setup
- **Frontend:** https://turbotrades.dev
- **Backend API:** https://api.turbotrades.dev
- **WebSocket:** https://ws.turbotrades.dev
### Server Paths
- **Backend Code:** `/root/ttbackend`
- **Frontend Build:** `/var/www/html/turbotrades`
- **Nginx Config:** Already configured ✅
- **SSL Certificates:** Already configured ✅
### PM2 Configuration
- **App Name:** `turbotrades-backend`
- **Port:** 3000
- **Mode:** Cluster
- **Auto-restart:** Enabled
---
## 🎯 Quick Start
### Deploy to Production
```bash
# 1. SSH to server
ssh root@turbotrades.dev
# 2. Initial setup (first time only)
cd /root/ttbackend
git clone https://git.turbotrades.dev/iDefineHD/TurboTrades.git .
# 3. Deploy
./deploy.sh
```
### Push Changes from Local
```bash
# Option 1: Use helper script
./push.sh
# Option 2: Manual
git add .
git commit -m "Your changes"
git push origin main
# Then SSH and deploy
ssh root@turbotrades.dev
cd /root/ttbackend
./deploy.sh
```
---
## 📁 Files Created for Deployment
### Deployment Scripts
-`deploy.sh` - Main deployment script
-`push.sh` - Quick commit and push helper
-`ecosystem.config.js` - PM2 configuration (updated)
### Configuration
-`nginx-config/turbotrades.conf` - Nginx configuration example
-`.github/workflows/deploy.yml` - Gitea Actions workflow
### Documentation
-`DEPLOY.md` - Simple deployment guide
-`GITEA_DEPLOY.md` - Complete deployment documentation
-`DEPLOY_SUMMARY.md` - This file
---
## 🔧 What Was Updated
### Repository Configuration
- ✅ Git remote updated to Gitea: `https://git.turbotrades.dev/iDefineHD/TurboTrades.git`
- ✅ All documentation updated with Gitea URLs
- ✅ Removed all tunnel-related files and scripts
### Backend Configuration (`ecosystem.config.js`)
```javascript
{
name: "turbotrades-backend",
cwd: "/root/ttbackend",
env: {
FRONTEND_URL: "https://turbotrades.dev",
BACKEND_URL: "https://api.turbotrades.dev",
WS_URL: "https://ws.turbotrades.dev"
}
}
```
### Frontend Configuration
- ✅ WebSocket connects to `wss://ws.turbotrades.dev` in production
- ✅ API calls go to `https://api.turbotrades.dev`
- ✅ Vite proxy configured for local development
### Deployment Workflow
```
Local Changes → Git Push → SSH to Server → Run deploy.sh → Live! 🎉
```
---
## 📦 deploy.sh Script Actions
The deployment script automatically:
1. ⏸️ **Stops** backend (PM2)
2. 💾 **Creates** backup of current version
3. 📥 **Pulls** latest code from Gitea
4. 📦 **Installs** backend dependencies (`npm ci --production`)
5. 🎨 **Builds** frontend (`npm run build`)
6. 🚀 **Deploys** frontend to `/var/www/html/turbotrades`
7. 🔒 **Sets** proper permissions (www-data:www-data)
8. ▶️ **Restarts** backend with PM2
9. 💾 **Saves** PM2 configuration
10.**Runs** health checks
**Deployment Time:** ~2-3 minutes
---
## 🌐 Environment Variables Required
Create `/root/ttbackend/.env` with:
```env
# Application
NODE_ENV=production
PORT=3000
# URLs
FRONTEND_URL=https://turbotrades.dev
BACKEND_URL=https://api.turbotrades.dev
WS_URL=https://ws.turbotrades.dev
# Database
MONGODB_URI=mongodb://localhost:27017/turbotrades
# Security
JWT_SECRET=your-super-secret-jwt-key
REFRESH_TOKEN_SECRET=your-refresh-token-secret
SESSION_SECRET=your-session-secret
# Steam OAuth
STEAM_API_KEY=your-steam-api-key
STEAM_REALM=https://turbotrades.dev
STEAM_RETURN_URL=https://api.turbotrades.dev/auth/steam/return
# Email (optional)
SMTP_HOST=smtp.example.com
SMTP_PORT=587
SMTP_USER=your-email@example.com
SMTP_PASS=your-password
SMTP_FROM=noreply@turbotrades.dev
```
---
## 📊 Monitoring Commands
### PM2 Status
```bash
pm2 list # List all processes
pm2 logs turbotrades-backend # View logs
pm2 monit # Monitor resources
pm2 restart turbotrades-backend # Restart backend
```
### Health Checks
```bash
curl https://api.turbotrades.dev/api/health # Backend API
curl https://turbotrades.dev # Frontend
```
### Nginx Logs
```bash
tail -f /var/log/nginx/turbotrades-error.log
tail -f /var/log/nginx/api-turbotrades-error.log
tail -f /var/log/nginx/ws-turbotrades-error.log
```
---
## 🐛 Common Issues & Fixes
### Backend Won't Start
```bash
pm2 logs turbotrades-backend # Check logs
systemctl status mongod # Check MongoDB
pm2 restart turbotrades-backend # Force restart
```
### Frontend 404
```bash
ls -la /var/www/html/turbotrades/ # Check files exist
chown -R www-data:www-data /var/www/html/turbotrades # Fix permissions
```
### 502 Bad Gateway
```bash
pm2 restart turbotrades-backend # Backend probably down
netstat -tulpn | grep 3000 # Check if port 3000 is listening
```
### WebSocket Won't Connect
```bash
# Check WebSocket domain is correct in frontend
# Should be: wss://ws.turbotrades.dev
pm2 logs turbotrades-backend # Check backend logs
```
---
## 🔄 Deployment Workflow Examples
### Regular Update
```bash
# Local machine
git add .
git commit -m "Update feature X"
git push origin main
# Server
ssh root@turbotrades.dev
cd /root/ttbackend
./deploy.sh
```
### Hotfix
```bash
# Local machine
git add .
git commit -m "Hotfix: Critical bug"
git push origin main
# Server
ssh root@turbotrades.dev
cd /root/ttbackend
./deploy.sh
```
### Frontend Only Update
```bash
# Server
ssh root@turbotrades.dev
cd /root/ttbackend
git pull origin main
cd frontend && npm ci && npm run build
rm -rf /var/www/html/turbotrades/*
cp -r dist/* /var/www/html/turbotrades/
chown -R www-data:www-data /var/www/html/turbotrades
```
### Backend Only Update
```bash
# Server
ssh root@turbotrades.dev
cd /root/ttbackend
git pull origin main
npm ci --production
pm2 restart turbotrades-backend
```
---
## 🚨 Emergency Rollback
```bash
ssh root@turbotrades.dev
pm2 stop turbotrades-backend
# List backups
ls -lt /root/ | grep ttbackend-backup
# Restore
cd /root
rm -rf ttbackend
cp -r ttbackend-backup-YYYYMMDD-HHMMSS ttbackend
cd ttbackend
pm2 restart turbotrades-backend
```
---
## ✅ Pre-Deployment Checklist
- [x] Git remote configured to Gitea
- [x] Tunnel infrastructure removed
- [x] Deployment scripts created and executable
- [x] PM2 ecosystem config updated
- [x] Frontend WebSocket configured for production
- [x] Nginx configuration ready
- [x] Domain structure documented
- [x] Server paths configured
### Before First Deployment
- [ ] Server prerequisites installed (Node.js, PM2, MongoDB, Nginx)
- [ ] Directories created (`/root/ttbackend`, `/var/www/html/turbotrades`)
- [ ] Nginx configuration deployed
- [ ] SSL certificates configured
- [ ] `.env` file created with proper secrets
- [ ] MongoDB running and accessible
- [ ] Repository cloned to `/root/ttbackend`
### Before Each Deployment
- [ ] Code tested locally
- [ ] Environment variables updated if needed
- [ ] Database migrations prepared (if any)
- [ ] Team notified of deployment
- [ ] Backup created (deploy.sh does this automatically)
---
## 🎉 Ready to Deploy!
Everything is configured and ready. To deploy:
```bash
# 1. Push your changes
git add .
git commit -m "Ready for production"
git push origin main
# 2. Deploy to server
ssh root@turbotrades.dev
cd /root/ttbackend
git pull origin main
./deploy.sh
```
**Your site will be live at:**
- 🌐 https://turbotrades.dev
- 🔧 https://api.turbotrades.dev
- 💬 https://ws.turbotrades.dev
---
## 📚 Documentation
- **Quick Deploy:** `DEPLOY.md`
- **Complete Guide:** `GITEA_DEPLOY.md`
- **Nginx Config:** `nginx-config/turbotrades.conf`
- **PM2 Config:** `ecosystem.config.js`
---
## 🆘 Support
If you encounter issues:
1. Check the logs: `pm2 logs turbotrades-backend`
2. Check nginx logs: `tail -f /var/log/nginx/api-turbotrades-error.log`
3. Review documentation in `DEPLOY.md` or `GITEA_DEPLOY.md`
4. Check health endpoints with curl
---
**Last Updated:** January 10, 2026
**Status:** ✅ Ready for Production Deployment
**Next Step:** SSH to server and run `./deploy.sh`

694
GITEA_DEPLOY.md Normal file
View File

@@ -0,0 +1,694 @@
# 🚀 TurboTrades - Gitea Deployment Guide
Complete deployment guide for TurboTrades using Gitea self-hosted repository.
---
## 📋 Overview
**Repository:** https://git.turbotrades.dev/iDefineHD/TurboTrades.git
**Server:** turbotrades.dev
**Deployment Method:** Gitea Actions + SSH
### Domain Structure
- **Frontend:** https://turbotrades.dev (Nginx serves static files)
- **Backend API:** https://api.turbotrades.dev (Nginx → Node.js)
- **WebSocket:** https://ws.turbotrades.dev (Nginx → Node.js)
### Server Paths
- **Backend Code:** `/root/ttbackend`
- **Frontend Build:** `/var/www/html/turbotrades`
- **Nginx Config:** `/etc/nginx/sites-available/turbotrades.conf`
- **Logs:** `/root/ttbackend/logs/`
---
## 🎯 Quick Deployment
### For First-Time Setup
```bash
# 1. SSH into server
ssh root@turbotrades.dev
# 2. Clone repository
mkdir -p /root/ttbackend
cd /root/ttbackend
git clone https://git.turbotrades.dev/iDefineHD/TurboTrades.git .
# 3. Install dependencies
npm ci --production
# 4. Build frontend
cd frontend
npm ci
npm run build
# 5. Deploy frontend to nginx
mkdir -p /var/www/html/turbotrades
cp -r dist/* /var/www/html/turbotrades/
chown -R www-data:www-data /var/www/html/turbotrades
# 6. Configure environment
cd /root/ttbackend
cp .env.example .env
nano .env # Edit with your settings
# 7. Start backend with PM2
pm2 start ecosystem.config.js --env production
pm2 save
pm2 startup # Follow instructions
```
### For Updates (Automatic via Gitea Actions)
Just push to `main` branch:
```bash
git add .
git commit -m "Your update"
git push origin main
```
Gitea Actions will automatically deploy!
---
## 🔧 Detailed Setup
### 1. Server Prerequisites
#### Install Required Software
```bash
# Update system
apt update && apt upgrade -y
# Install Node.js 20
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt install -y nodejs
# Install PM2
npm install -g pm2
# Install Nginx
apt install -y nginx
# Install MongoDB (if not installed)
wget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/7.0 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-7.0.list
apt update
apt install -y mongodb-org
systemctl enable mongod
systemctl start mongod
# Install Git
apt install -y git
```
#### Create Directories
```bash
# Backend directory
mkdir -p /root/ttbackend
mkdir -p /root/ttbackend/logs
# Frontend directory
mkdir -p /var/www/html/turbotrades
# Set permissions
chown -R www-data:www-data /var/www/html/turbotrades
chmod -R 755 /var/www/html/turbotrades
```
### 2. Configure Nginx
#### Copy Nginx Config
```bash
# Copy config file
cp /root/ttbackend/nginx-config/turbotrades.conf /etc/nginx/sites-available/
# Enable site
ln -s /etc/nginx/sites-available/turbotrades.conf /etc/nginx/sites-enabled/
# Test configuration
nginx -t
# Reload Nginx
systemctl reload nginx
```
#### Setup SSL with Let's Encrypt
```bash
# Install Certbot
apt install -y certbot python3-certbot-nginx
# Get SSL certificate (will configure nginx automatically)
certbot --nginx -d turbotrades.dev -d www.turbotrades.dev -d api.turbotrades.dev -d ws.turbotrades.dev
# Auto-renewal
certbot renew --dry-run
```
### 3. Configure Backend
#### Create .env File
```bash
cd /root/ttbackend
nano .env
```
**Required Environment Variables:**
```env
# Application
NODE_ENV=production
PORT=3000
# URLs
FRONTEND_URL=https://turbotrades.dev
BACKEND_URL=https://api.turbotrades.dev
WS_URL=https://ws.turbotrades.dev
# Database
MONGODB_URI=mongodb://localhost:27017/turbotrades
# JWT Secrets
JWT_SECRET=your-super-secret-jwt-key-change-this
REFRESH_TOKEN_SECRET=your-refresh-token-secret-change-this
# Steam OAuth
STEAM_API_KEY=your-steam-api-key
STEAM_REALM=https://turbotrades.dev
STEAM_RETURN_URL=https://api.turbotrades.dev/auth/steam/return
# Session
SESSION_SECRET=your-session-secret-change-this
# Email (Optional)
SMTP_HOST=smtp.example.com
SMTP_PORT=587
SMTP_USER=your-email@example.com
SMTP_PASS=your-email-password
SMTP_FROM=noreply@turbotrades.dev
# Admin
ADMIN_EMAIL=admin@turbotrades.dev
# Rate Limiting
RATE_LIMIT_WINDOW=15
RATE_LIMIT_MAX=100
```
### 4. Deploy Backend with PM2
```bash
cd /root/ttbackend
# Start with ecosystem config
pm2 start ecosystem.config.js --env production
# Save PM2 configuration
pm2 save
# Setup PM2 to start on boot
pm2 startup
# Follow the command it outputs
# Monitor
pm2 list
pm2 logs turbotrades-backend
```
---
## 🤖 Gitea Actions Setup
### 1. Configure Repository Secrets
Go to: `https://git.turbotrades.dev/iDefineHD/TurboTrades/settings/secrets`
Add these secrets:
| Secret Name | Value | Description |
|-------------|-------|-------------|
| `SERVER_HOST` | `turbotrades.dev` | Your server hostname |
| `SERVER_USER` | `root` | SSH user |
| `SERVER_PORT` | `22` | SSH port (default 22) |
| `SSH_PRIVATE_KEY` | `-----BEGIN OPENSSH PRIVATE KEY-----...` | SSH private key for deployment |
### 2. Generate SSH Key for Deployment
```bash
# On your server
ssh-keygen -t ed25519 -C "gitea-deploy" -f ~/.ssh/gitea_deploy
# Add public key to authorized_keys
cat ~/.ssh/gitea_deploy.pub >> ~/.ssh/authorized_keys
# Copy PRIVATE key for Gitea secret
cat ~/.ssh/gitea_deploy
# Copy entire output including BEGIN/END lines
```
### 3. Enable Gitea Actions
1. Go to repository settings
2. Enable "Actions" if not already enabled
3. Workflow file is at `.github/workflows/deploy.yml`
### 4. Test Deployment
```bash
# Make a change
echo "# Test" >> README.md
git add README.md
git commit -m "Test deployment"
git push origin main
# Watch in Gitea Actions tab
# Go to: https://git.turbotrades.dev/iDefineHD/TurboTrades/actions
```
---
## 📊 Monitoring & Management
### PM2 Commands
```bash
# View status
pm2 list
# View logs
pm2 logs turbotrades-backend
# Restart
pm2 restart turbotrades-backend
# Stop
pm2 stop turbotrades-backend
# Start
pm2 start turbotrades-backend
# Monitor resources
pm2 monit
# Clear logs
pm2 flush
```
### Nginx Commands
```bash
# Test configuration
nginx -t
# Reload (without downtime)
systemctl reload nginx
# Restart
systemctl restart nginx
# View access logs
tail -f /var/log/nginx/turbotrades-access.log
tail -f /var/log/nginx/api-turbotrades-access.log
tail -f /var/log/nginx/ws-turbotrades-access.log
# View error logs
tail -f /var/log/nginx/turbotrades-error.log
tail -f /var/log/nginx/api-turbotrades-error.log
tail -f /var/log/nginx/ws-turbotrades-error.log
```
### Health Checks
```bash
# Frontend
curl https://turbotrades.dev
# Backend API
curl https://api.turbotrades.dev/api/health
# WebSocket (needs wscat)
npm install -g wscat
wscat -c wss://ws.turbotrades.dev
```
---
## 🔄 Manual Deployment
If Gitea Actions fail or you prefer manual deployment:
```bash
# SSH to server
ssh root@turbotrades.dev
# Navigate to backend
cd /root/ttbackend
# Stop application
pm2 stop turbotrades-backend
# Backup current version
cp -r /root/ttbackend /root/ttbackend-backup-$(date +%Y%m%d-%H%M%S)
# Pull latest code
git fetch origin
git reset --hard origin/main
git clean -fd
# Install backend dependencies
npm ci --production
# Build frontend
cd frontend
npm ci
npm run build
# Deploy frontend
rm -rf /var/www/html/turbotrades/*
cp -r dist/* /var/www/html/turbotrades/
chown -R www-data:www-data /var/www/html/turbotrades
chmod -R 755 /var/www/html/turbotrades
# Back to root
cd /root/ttbackend
# Restart backend
pm2 restart turbotrades-backend
pm2 save
# Verify
pm2 list
pm2 logs turbotrades-backend --lines 50
```
---
## 🐛 Troubleshooting
### Backend Not Starting
```bash
# Check logs
pm2 logs turbotrades-backend
# Check if port 3000 is in use
netstat -tulpn | grep 3000
# Check MongoDB connection
mongo --eval "db.runCommand({ ping: 1 })"
# Check environment variables
cd /root/ttbackend
cat .env | grep -v "SECRET\|PASSWORD\|KEY"
```
### Frontend Not Loading
```bash
# Check Nginx config
nginx -t
# Check if files exist
ls -la /var/www/html/turbotrades/
# Check permissions
ls -ld /var/www/html/turbotrades/
# Check Nginx logs
tail -f /var/log/nginx/turbotrades-error.log
```
### SSL Certificate Issues
```bash
# Renew certificate
certbot renew
# Check certificate status
certbot certificates
# Test SSL
curl -I https://turbotrades.dev
```
### WebSocket Connection Failed
```bash
# Check if backend is listening
netstat -tulpn | grep 3000
# Check Nginx WebSocket proxy
grep -A 20 "ws.turbotrades.dev" /etc/nginx/sites-available/turbotrades.conf
# Test WebSocket upgrade
curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" https://ws.turbotrades.dev
```
### Database Connection Issues
```bash
# Check MongoDB status
systemctl status mongod
# Restart MongoDB
systemctl restart mongod
# Check connection
mongo turbotrades --eval "db.stats()"
```
### Gitea Actions Failed
1. Check Actions tab: `https://git.turbotrades.dev/iDefineHD/TurboTrades/actions`
2. Click failed workflow to view logs
3. Common issues:
- SSH key not configured
- Secrets not set
- Server unreachable
- Permission issues
### 502 Bad Gateway
```bash
# Backend not running
pm2 status turbotrades-backend
pm2 start turbotrades-backend
# Port mismatch - check backend port
grep "PORT" /root/ttbackend/.env
# Nginx proxy config
grep "proxy_pass" /etc/nginx/sites-available/turbotrades.conf
```
---
## 🔒 Security Checklist
- [x] SSL certificates installed for all domains
- [ ] Firewall configured (UFW or iptables)
- [ ] SSH key authentication only (disable password auth)
- [ ] MongoDB secured (authentication enabled)
- [ ] Secrets properly set in .env (not hardcoded)
- [ ] Rate limiting enabled (Nginx + Backend)
- [ ] CORS properly configured
- [ ] Security headers in Nginx
- [ ] Regular backups configured
- [ ] Log rotation enabled
- [ ] Fail2ban installed (optional)
- [ ] Server updates automated
### Firewall Setup (UFW)
```bash
# Install UFW
apt install -y ufw
# Allow SSH
ufw allow 22/tcp
# Allow HTTP/HTTPS
ufw allow 80/tcp
ufw allow 443/tcp
# Enable firewall
ufw enable
# Check status
ufw status
```
---
## 📦 Backup Strategy
### Automated Backup Script
```bash
# Create backup script
nano /root/backup-turbotrades.sh
```
**backup-turbotrades.sh:**
```bash
#!/bin/bash
BACKUP_DIR="/root/backups"
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
# Create backup directory
mkdir -p $BACKUP_DIR
# Backup MongoDB
mongodump --db turbotrades --out $BACKUP_DIR/mongodb-$TIMESTAMP
# Backup backend code
tar -czf $BACKUP_DIR/backend-$TIMESTAMP.tar.gz /root/ttbackend
# Backup frontend
tar -czf $BACKUP_DIR/frontend-$TIMESTAMP.tar.gz /var/www/html/turbotrades
# Backup nginx config
tar -czf $BACKUP_DIR/nginx-$TIMESTAMP.tar.gz /etc/nginx/sites-available/turbotrades.conf
# Keep only last 7 days of backups
find $BACKUP_DIR -name "*.tar.gz" -mtime +7 -delete
find $BACKUP_DIR -name "mongodb-*" -mtime +7 -delete
echo "Backup completed: $TIMESTAMP"
```
```bash
# Make executable
chmod +x /root/backup-turbotrades.sh
# Test backup
/root/backup-turbotrades.sh
# Schedule daily backups (3 AM)
crontab -e
# Add this line:
0 3 * * * /root/backup-turbotrades.sh >> /var/log/turbotrades-backup.log 2>&1
```
---
## 📈 Performance Optimization
### Nginx Caching
Add to server block in nginx config:
```nginx
# Cache static assets
location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff|woff2|ttf|eot)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
# API response caching (optional)
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=api_cache:10m max_size=1g inactive=60m;
```
### PM2 Cluster Mode
Update ecosystem.config.js:
```javascript
instances: 4, // Use multiple CPU cores
exec_mode: "cluster",
```
### MongoDB Indexes
```bash
mongo turbotrades
```
```javascript
// Add indexes for better performance
db.users.createIndex({ email: 1 }, { unique: true })
db.users.createIndex({ steamId: 1 }, { unique: true })
db.items.createIndex({ listed: 1, price: 1 })
db.trades.createIndex({ userId: 1, createdAt: -1 })
```
---
## 📝 Deployment Checklist
### Pre-Deployment
- [ ] Code reviewed and tested locally
- [ ] All tests passing
- [ ] Environment variables updated if needed
- [ ] Database migrations prepared (if any)
- [ ] Backup created
- [ ] Team notified
### Deployment
- [ ] Push to main branch
- [ ] Monitor Gitea Actions workflow
- [ ] Check deployment logs
- [ ] Verify PM2 status
### Post-Deployment
- [ ] Frontend loads correctly
- [ ] API health check passes
- [ ] WebSocket connects
- [ ] Test critical user flows
- [ ] Monitor error logs
- [ ] Check PM2 resource usage
- [ ] Verify database connections
---
## 🆘 Emergency Rollback
```bash
# SSH to server
ssh root@turbotrades.dev
# Stop current backend
pm2 stop turbotrades-backend
# Restore from backup
cd /root
rm -rf ttbackend
cp -r ttbackend-backup ttbackend # Use latest backup
# Restart
cd ttbackend
pm2 restart turbotrades-backend
pm2 save
# Verify
pm2 logs turbotrades-backend
curl https://api.turbotrades.dev/api/health
```
---
## 📚 Useful Links
- **Frontend:** https://turbotrades.dev
- **Backend API:** https://api.turbotrades.dev
- **WebSocket:** https://ws.turbotrades.dev
- **Gitea Repo:** https://git.turbotrades.dev/iDefineHD/TurboTrades
- **Gitea Actions:** https://git.turbotrades.dev/iDefineHD/TurboTrades/actions
---
## 🎉 Success!
Your TurboTrades deployment is now live and automatically updating via Gitea Actions!
**Questions?** Check the logs and troubleshooting section above.
**Happy Trading! 🚀**

View File

@@ -31,10 +31,19 @@ export const useWebSocketStore = defineStore("websocket", () => {
// Helper functions
const getWebSocketUrl = () => {
// Use environment variable or fallback to ws subdomain
if (import.meta.env.VITE_WS_URL) {
return import.meta.env.VITE_WS_URL;
}
// In production, use dedicated WebSocket domain
if (import.meta.env.PROD) {
return "wss://ws.turbotrades.dev";
}
// In development, use current host with ws path (for proxy)
const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
const host = window.location.host;
// Always use the current host (works with VS Code tunnels and proxies)
return `${protocol}//${host}/ws`;
};