chitfund/README/UPDATE_API_URL.md

3.7 KiB

🔧 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:

static const String baseUrl = 'http://localhost:3000/api';

To:

static const String baseUrl = 'http://192.168.8.148:3000/api';

🚀 Three Ways to Deploy

Upload deploy-frontend.sh to your server and run:

# 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:

# On your server (as root)
chmod +x deploy-frontend-simple.sh
./deploy-frontend-simple.sh

Option 3: Manual Steps

# 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:


🔄 Update Process

When you need to update the frontend:

# 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

# 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

# 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

# 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:

    sudo apt install certbot python3-certbot-nginx
    sudo certbot --nginx -d yourdomain.com
    
  4. Update API URL to domain:

    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! 🚀