mirror of
https://github.com/johndoe6345789/snippet-pastebin.git
synced 2026-04-24 13:34:55 +00:00
8.9 KiB
8.9 KiB
CapRover/Cloudflare CORS Configuration Summary
✅ What's Been Configured
Backend CORS Implementation
The Flask backend (backend/app.py) now includes comprehensive CORS support:
-
Environment-based CORS configuration:
CORS_ALLOWED_ORIGINSenvironment variable- Supports wildcard (
*) for development - Supports comma-separated list for multiple origins in production
-
Proper CORS headers:
Access-Control-Allow-OriginAccess-Control-Allow-MethodsAccess-Control-Allow-HeadersAccess-Control-Allow-Credentials
-
Security features:
- Specific origins in production
- Wildcard only in development
- Credentials support for specific origins
Frontend Configuration
The frontend automatically detects and uses the backend via:
- Environment variable:
VITE_FLASK_BACKEND_URL - Automatic configuration: When set, forces Flask backend usage
- Manual configuration: Settings page (if env var not set)
Docker Configuration
-
Backend Dockerfile:
- Environment variables support
- Persistent volume at
/app/data - Health check endpoint
-
Frontend Dockerfile:
- Build-time argument for backend URL
- Nginx with proxy support
- Static file serving
-
Nginx Configuration:
- Proper proxy headers
- Cache control for SPA
- API proxying
CapRover Support
-
captain-definition files:
- Frontend: Root directory
- Backend: Backend directory
-
Deployment ready:
- Separate app deployments
- Environment variable configuration
- Persistent storage support
📚 Documentation Created
Primary Guides
-
- Complete CapRover/Cloudflare deployment walkthrough
- Step-by-step instructions
- DNS configuration
- SSL setup
- Testing procedures
- Troubleshooting
-
- CORS concepts and configuration
- Testing procedures
- Common errors and solutions
- Automated testing script
- Security best practices
- Debugging tips
-
- Quick reference checklist
- All deployment steps
- Testing verification
- Security checks
- Quick commands
-
- Environment variable examples
- Different deployment scenarios
- Common mistakes
- Troubleshooting
Updated Documentation
-
- Added CORS environment variable
- Updated with deployment links
-
- Added CORS configuration
- Production deployment section
- Environment variables table
-
- Added deployment documentation links
- Organized documentation section
Configuration Files
-
- Frontend variables
- Backend variables
- Comments and examples
-
- Updated with new environment variables
- Proper volume paths
-
- Production configuration example
- Network configuration
-
- Enhanced proxy configuration
- Security headers
- Cache control
Testing Tools
- test-cors.sh
- Automated CORS testing script
- 5 comprehensive tests
- Clear pass/fail indicators
- Usage instructions
🚀 Quick Start Guide
Local Development
# Backend
cd backend
pip install -r requirements.txt
CORS_ALLOWED_ORIGINS=http://localhost:3000 python app.py
# Frontend
echo "VITE_FLASK_BACKEND_URL=http://localhost:5000" > .env
npm install
npm run dev
Docker Compose
# Starts both frontend and backend
docker-compose up -d
# Access at http://localhost:3000
CapRover Deployment
# Deploy backend
cd backend
caprover deploy -a codesnippet-backend
# Configure in CapRover dashboard:
# - CORS_ALLOWED_ORIGINS=https://frontend.example.com
# - DATABASE_PATH=/app/data/snippets.db
# Deploy frontend
cd ..
caprover deploy -a codesnippet-frontend
# Configure in CapRover dashboard:
# - VITE_FLASK_BACKEND_URL=https://backend.example.com
🔧 Configuration Examples
Separate Domains (Recommended)
Frontend: https://frontend.example.com
Backend: https://backend.example.com
Frontend:
VITE_FLASK_BACKEND_URL=https://backend.example.com
Backend:
CORS_ALLOWED_ORIGINS=https://frontend.example.com
DATABASE_PATH=/app/data/snippets.db
Single Domain (Proxied)
Frontend: https://app.example.com
Backend: https://app.example.com/api (proxied)
Frontend:
VITE_FLASK_BACKEND_URL=/api
Backend:
CORS_ALLOWED_ORIGINS=*
DATABASE_PATH=/app/data/snippets.db
Multiple Frontends
Frontend 1: https://app.example.com
Frontend 2: https://staging.example.com
Backend: https://api.example.com
Backend:
CORS_ALLOWED_ORIGINS=https://app.example.com,https://staging.example.com
DATABASE_PATH=/app/data/snippets.db
✅ Testing CORS
Quick Test
curl -H "Origin: https://frontend.example.com" \
-H "Access-Control-Request-Method: GET" \
-X OPTIONS \
https://backend.example.com/api/snippets
Should return:
Access-Control-Allow-Origin: https://frontend.example.com
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Automated Test
chmod +x test-cors.sh
./test-cors.sh https://backend.example.com https://frontend.example.com
🔒 Security Checklist
- HTTPS enabled for both frontend and backend
CORS_ALLOWED_ORIGINSset to specific domains (not*)- Cloudflare proxy enabled (orange cloud)
- SSL/TLS mode set to "Full (strict)"
- Flask debug mode disabled
- Database backed up regularly
- Rate limiting configured
- Monitoring enabled
📋 Environment Variables
Frontend
| Variable | Required | Example |
|---|---|---|
VITE_FLASK_BACKEND_URL |
Yes (for Flask) | https://backend.example.com |
Backend
| Variable | Required | Example |
|---|---|---|
CORS_ALLOWED_ORIGINS |
Yes | https://frontend.example.com |
DATABASE_PATH |
No | /app/data/snippets.db (default) |
🐛 Troubleshooting
CORS Errors
Problem: Browser shows CORS policy error
Solution:
- Check
CORS_ALLOWED_ORIGINSincludes frontend URL - Verify both use HTTPS (not mixed)
- Restart backend after env changes
- See CORS-GUIDE.md
Connection Failed
Problem: Frontend can't connect to backend
Solution:
- Test backend:
curl https://backend.example.com/health - Check
VITE_FLASK_BACKEND_URLis correct - Verify backend is running
- Check firewall/network settings
SSL Issues
Problem: SSL certificate not valid
Solution:
- Wait 5-10 minutes for Let's Encrypt
- Verify DNS records are correct
- Check Cloudflare SSL mode: "Full (strict)"
- Disable and re-enable HTTPS in CapRover
📖 Documentation Index
For Developers
- Backend Configuration - Configure storage backends
- Environment Configuration - Environment variable examples
- Backend API - API documentation
For DevOps
- Deployment Guide - Complete deployment walkthrough
- CORS Guide - CORS configuration and testing
- Deployment Checklist - Quick reference
For Everyone
- Quick Start - Get started quickly
- Application Guide - Using the application
- Main README - Overview and links
🎯 Key Benefits
-
Flexible Deployment:
- Single domain or separate domains
- CapRover, Docker, or standalone
- Local development or cloud production
-
Secure by Default:
- CORS properly configured
- HTTPS enforced
- Specific origins in production
-
Easy to Configure:
- Environment variables
- Clear documentation
- Testing tools included
-
Production Ready:
- Cloudflare CDN support
- CapRover deployment ready
- Monitoring and logging
-
Well Documented:
- Step-by-step guides
- Configuration examples
- Troubleshooting help
🤝 Support
For issues or questions:
- Check the relevant guide in Documentation Index
- Review Troubleshooting section
- Run the test script:
./test-cors.sh - Check application logs
📝 Next Steps
After deployment:
- ✅ Test all functionality
- ✅ Configure backups
- ✅ Set up monitoring
- ✅ Review security settings
- ✅ Configure rate limiting
- ✅ Test disaster recovery
- ✅ Document your specific configuration
- ✅ Share with your team
Ready to deploy? Start with the Deployment Checklist!