diff --git a/UPDATE_API_URL.md b/UPDATE_API_URL.md new file mode 100644 index 0000000..b4af701 --- /dev/null +++ b/UPDATE_API_URL.md @@ -0,0 +1,193 @@ +# 🔧 Update API URL for Production + +Your Flutter app is currently configured to use `localhost:3000` for API calls. +For production, you need to change it to your server IP: `192.168.8.148` + +## 📝 File to Update + +**File:** `luckychit/lib/core/services/api_service.dart` + +**Line 5:** Change from: +```dart +static const String baseUrl = 'http://localhost:3000/api'; +``` + +To: +```dart +static const String baseUrl = 'http://192.168.8.148:3000/api'; +``` + +--- + +## 🚀 Three Ways to Deploy + +### Option 1: Automated Script (Recommended) + +Upload `deploy-frontend.sh` to your server and run: + +```bash +# On your server (as root) +cd /home/luckychit/apps/chitfund +chmod +x deploy-frontend.sh +./deploy-frontend.sh +``` + +This script will: +- ✅ Backup current files +- ✅ Update API URL automatically +- ✅ Build Flutter web app +- ✅ Deploy to Nginx +- ✅ Restore original file for future edits + +--- + +### Option 2: Quick One-Liner + +Upload `deploy-frontend-simple.sh` and run: + +```bash +# On your server (as root) +chmod +x deploy-frontend-simple.sh +./deploy-frontend-simple.sh +``` + +--- + +### Option 3: Manual Steps + +```bash +# 1. Update API URL +cd /home/luckychit/apps/chitfund/luckychit +nano lib/core/services/api_service.dart +# Change line 5 to: static const String baseUrl = 'http://192.168.8.148:3000/api'; + +# 2. Build +flutter pub get +flutter build web --release + +# 3. Deploy +cp -r build/web/* /var/www/luckychit/ +chown -R www-data:www-data /var/www/luckychit +``` + +--- + +## 🌐 Access Your App + +After deployment: +- **Frontend:** http://192.168.8.148 +- **API Health:** http://192.168.8.148:3000/health +- **API Base:** http://192.168.8.148:3000/api + +--- + +## 🔄 Update Process + +When you need to update the frontend: + +```bash +# Pull latest code +cd /home/luckychit/apps/chitfund +git pull + +# Run deployment script +cd luckychit +./deploy-frontend.sh +``` + +--- + +## âš ī¸ Important Notes + +1. **Development vs Production:** + - Keep `localhost:3000` in your local code + - Only change to server IP when deploying to production + - The automated scripts handle this for you + +2. **CORS Settings:** + - Your backend `.env` should already allow this IP + - Check: `ALLOWED_ORIGINS=http://192.168.8.148` + +3. **Firewall:** + - Make sure port 80 (HTTP) is open + - Make sure port 3000 (API) is open + - Check: `sudo ufw status` + +--- + +## 🐛 Troubleshooting + +### Can't connect to API +```bash +# Check backend is running +pm2 status + +# Test API directly +curl http://192.168.8.148:3000/health + +# Check firewall +sudo ufw status | grep 3000 +``` + +### Frontend shows blank page +```bash +# Check Nginx logs +sudo tail -f /var/log/nginx/error.log + +# Check files deployed +ls -la /var/www/luckychit/ + +# Restart Nginx +sudo systemctl restart nginx +``` + +### API URL not updated +```bash +# Verify the change in built files +grep -r "192.168.8.148" /var/www/luckychit/ + +# If not found, rebuild and redeploy +cd /home/luckychit/apps/chitfund/luckychit +./deploy-frontend.sh +``` + +--- + +## 🔐 Production Best Practices + +For production, consider: + +1. **Use Environment Variables:** + Create a config file instead of hardcoding IPs + +2. **Use Domain Name:** + Setup a domain and use it instead of IP + +3. **Enable HTTPS:** + ```bash + sudo apt install certbot python3-certbot-nginx + sudo certbot --nginx -d yourdomain.com + ``` + +4. **Update API URL to domain:** + ```dart + static const String baseUrl = 'https://api.yourdomain.com'; + ``` + +--- + +## 📋 Quick Reference + +| Task | Command | +|------|---------| +| Deploy frontend | `./deploy-frontend.sh` | +| Quick deploy | `./deploy-frontend-simple.sh` | +| Check backend | `pm2 status` | +| View API logs | `pm2 logs luckychit-api` | +| Test API | `curl http://192.168.8.148:3000/health` | +| View web logs | `sudo tail -f /var/log/nginx/access.log` | + +--- + +Good luck! 🚀 + diff --git a/deploy-frontend-simple.sh b/deploy-frontend-simple.sh new file mode 100644 index 0000000..94a2284 --- /dev/null +++ b/deploy-frontend-simple.sh @@ -0,0 +1,27 @@ +#!/bin/bash +############################################################################### +# Simple Frontend Deployment - Quick Version +############################################################################### + +SERVER_IP="192.168.8.148" +cd /home/luckychit/apps/chitfund/luckychit + +echo "🔧 Updating API URL..." +sed -i "s|http://localhost:3000/api|http://${SERVER_IP}:3000/api|g" lib/core/services/api_service.dart + +echo "đŸ“Ļ Getting dependencies..." +flutter pub get + +echo "đŸ—ī¸ Building web app..." +flutter build web --release + +echo "🚀 Deploying..." +mkdir -p /var/www/luckychit +cp -r build/web/* /var/www/luckychit/ +chown -R www-data:www-data /var/www/luckychit + +echo "🔄 Restoring original file..." +git checkout lib/core/services/api_service.dart 2>/dev/null || sed -i "s|http://${SERVER_IP}:3000/api|http://localhost:3000/api|g" lib/core/services/api_service.dart + +echo "✅ Done! Visit: http://${SERVER_IP}" + diff --git a/deploy-frontend.sh b/deploy-frontend.sh new file mode 100644 index 0000000..f5ff1e6 --- /dev/null +++ b/deploy-frontend.sh @@ -0,0 +1,199 @@ +#!/bin/bash + +############################################################################### +# LuckyChit Frontend Deployment Script +# Deploys Flutter Web App to Nginx +############################################################################### + +set -e # Exit on error + +# Colors +GREEN='\033[0;32m' +BLUE='\033[0;34m' +RED='\033[0;31m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# Configuration +SERVER_IP="192.168.8.148" +API_URL="http://${SERVER_IP}:3000/api" +PROJECT_DIR="/home/luckychit/apps/chitfund/luckychit" +WEB_ROOT="/var/www/luckychit" +API_SERVICE_FILE="lib/core/services/api_service.dart" + +print_info() { + echo -e "${BLUE}â„šī¸ $1${NC}" +} + +print_success() { + echo -e "${GREEN}✅ $1${NC}" +} + +print_error() { + echo -e "${RED}❌ $1${NC}" +} + +print_header() { + echo "" + echo -e "${GREEN}╔════════════════════════════════════════════════════════╗${NC}" + echo -e "${GREEN}║ LuckyChit Frontend Deployment Script ║${NC}" + echo -e "${GREEN}╚════════════════════════════════════════════════════════╝${NC}" + echo "" +} + +############################################################################### +# Main Deployment +############################################################################### + +print_header + +# Check if running as root +if [ "$EUID" -ne 0 ]; then + print_error "Please run as root (sudo ./deploy-frontend.sh)" + exit 1 +fi + +print_info "Deployment Configuration:" +echo " Server IP: $SERVER_IP" +echo " API URL: $API_URL" +echo " Project Dir: $PROJECT_DIR" +echo " Web Root: $WEB_ROOT" +echo "" +read -p "Continue with deployment? (y/n) " -n 1 -r +echo +if [[ ! $REPLY =~ ^[Yy]$ ]]; then + print_error "Deployment cancelled" + exit 1 +fi + +############################################################################### +# Step 1: Navigate to project directory +############################################################################### +print_info "Step 1/8: Navigating to project directory..." +cd $PROJECT_DIR +print_success "In directory: $(pwd)" + +############################################################################### +# Step 2: Create backup of current API service +############################################################################### +print_info "Step 2/8: Creating backup of API service..." +if [ -f "$API_SERVICE_FILE" ]; then + cp $API_SERVICE_FILE ${API_SERVICE_FILE}.backup + print_success "Backup created: ${API_SERVICE_FILE}.backup" +else + print_error "API service file not found: $API_SERVICE_FILE" + exit 1 +fi + +############################################################################### +# Step 3: Update API URL +############################################################################### +print_info "Step 3/8: Updating API URL to $API_URL..." + +# Replace localhost URL with server IP +sed -i "s|http://localhost:3000/api|$API_URL|g" $API_SERVICE_FILE + +# Verify the change +if grep -q "$API_URL" $API_SERVICE_FILE; then + print_success "API URL updated successfully" +else + print_error "Failed to update API URL" + exit 1 +fi + +############################################################################### +# Step 4: Update Flutter +############################################################################### +print_info "Step 4/8: Updating Flutter..." +cd /opt/flutter +git pull origin stable +flutter upgrade +print_success "Flutter updated: $(flutter --version | head -n 1)" + +############################################################################### +# Step 5: Get dependencies +############################################################################### +print_info "Step 5/8: Getting Flutter dependencies..." +cd $PROJECT_DIR +flutter pub get +print_success "Dependencies installed" + +############################################################################### +# Step 6: Build Flutter web app +############################################################################### +print_info "Step 6/8: Building Flutter web app..." +flutter build web --release --web-renderer html +print_success "Flutter web app built successfully" + +############################################################################### +# Step 7: Deploy to web server +############################################################################### +print_info "Step 7/8: Deploying to web server..." + +# Create web root if it doesn't exist +mkdir -p $WEB_ROOT + +# Backup existing deployment +if [ -d "$WEB_ROOT" ] && [ "$(ls -A $WEB_ROOT)" ]; then + BACKUP_DIR="${WEB_ROOT}_backup_$(date +%Y%m%d_%H%M%S)" + print_info "Backing up existing deployment to $BACKUP_DIR" + cp -r $WEB_ROOT $BACKUP_DIR +fi + +# Copy new build +cp -r build/web/* $WEB_ROOT/ + +# Set proper permissions +chown -R www-data:www-data $WEB_ROOT +chmod -R 755 $WEB_ROOT + +print_success "Deployed to $WEB_ROOT" + +############################################################################### +# Step 8: Verify deployment +############################################################################### +print_info "Step 8/8: Verifying deployment..." + +if [ -f "$WEB_ROOT/index.html" ]; then + print_success "Deployment verified - index.html exists" +else + print_error "Deployment failed - index.html not found" + exit 1 +fi + +############################################################################### +# Cleanup +############################################################################### +print_info "Restoring original API service file for future builds..." +if [ -f "${API_SERVICE_FILE}.backup" ]; then + mv ${API_SERVICE_FILE}.backup $API_SERVICE_FILE + print_success "Original API service restored" +fi + +############################################################################### +# Complete +############################################################################### +echo "" +echo -e "${GREEN}╔════════════════════════════════════════════════════════╗${NC}" +echo -e "${GREEN}║ Deployment Complete! 🎉 ║${NC}" +echo -e "${GREEN}╚════════════════════════════════════════════════════════╝${NC}" +echo "" + +print_success "Frontend deployed successfully!" +echo "" +print_info "Access your application at:" +echo " 🌐 http://$SERVER_IP" +echo "" +print_info "Backend API is at:" +echo " 🔌 http://$SERVER_IP:3000/api" +echo " 💚 Health: http://$SERVER_IP:3000/health" +echo "" +print_info "Next steps:" +echo " 1. Test the application in your browser" +echo " 2. Check backend is running: pm2 status" +echo " 3. View logs: pm2 logs luckychit-api" +echo "" +print_info "To rollback, use the backup at:" +echo " $(ls -td ${WEB_ROOT}_backup_* 2>/dev/null | head -1)" +echo "" +