6.4 KiB
Flask Backend Integration - Quick Start
This guide explains how to use the Flask backend for persistent storage with CodeForge.
Overview
CodeForge now supports multiple storage backends:
- IndexedDB (default) - Browser storage, works offline
- Flask Backend (optional) - Server storage, persistent across devices
- SQLite (optional) - Browser storage with SQL support
- Spark KV (fallback) - Cloud storage
Setup Flask Backend
Option 1: Docker (Recommended)
-
Start the backend with Docker Compose:
docker-compose up -d backend -
Verify it's running:
curl http://localhost:5001/health -
Configure in the UI:
- Open CodeForge settings
- Find "Storage Backend" section
- Enter backend URL:
http://localhost:5001 - Click "Use Flask"
Option 2: Run Locally
-
Install dependencies:
cd backend pip install -r requirements.txt -
Start the server:
python app.pyOr with gunicorn:
gunicorn --bind 0.0.0.0:5001 --workers 4 app:app -
Configure in the UI (same as Docker option)
Option 3: Docker Only Backend
cd backend
docker build -t codeforge-backend .
docker run -d -p 5001:5001 -v codeforge-data:/data --name codeforge-backend codeforge-backend
Using the Backend
In the UI
- Open Settings (or wherever StorageSettings component is added)
- Find "Storage Backend" section
- Enter Flask URL:
http://localhost:5001(or your server URL) - Click "Use Flask"
- All data will be migrated automatically
Programmatically
import { unifiedStorage } from '@/lib/unified-storage'
// Switch to Flask backend
await unifiedStorage.switchToFlask('http://localhost:5001')
// Check current backend
const backend = await unifiedStorage.getBackend()
console.log(backend) // 'flask'
// Use storage as normal
await unifiedStorage.set('my-key', { foo: 'bar' })
const value = await unifiedStorage.get('my-key')
Configuration
Environment Variables
Create a .env file in the backend directory:
PORT=5001
DEBUG=false
DATABASE_PATH=/data/codeforge.db
Custom Port
# Docker
docker run -e PORT=8080 -p 8080:8080 ...
# Python
PORT=8080 python app.py
Data Persistence
Data is stored in SQLite at /data/codeforge.db. Make sure to mount a volume:
docker run -v $(pwd)/data:/data ...
Production Deployment
Docker Compose (Full Stack)
# Start both frontend and backend
docker-compose up -d
# View logs
docker-compose logs -f
# Stop all
docker-compose down
Separate Deployment
-
Deploy backend:
docker-compose up -d backend -
Deploy frontend with backend URL:
docker build -t codeforge-frontend . docker run -d -p 80:80 \ -e VITE_BACKEND_URL=https://api.yourdomain.com \ codeforge-frontend -
Configure CORS if frontend and backend are on different domains
Switching Backends
From IndexedDB to Flask
- Click "Use Flask" in settings
- Enter backend URL
- All data migrates automatically
From Flask to IndexedDB
- Click "Use IndexedDB" in settings
- All data downloads to browser
- Can work offline
Export/Import
Always available regardless of backend:
// Export backup
const data = await unifiedStorage.exportData()
const json = JSON.stringify(data, null, 2)
// Save to file
// Import backup
await unifiedStorage.importData(parsedData)
Troubleshooting
Backend not connecting
-
Check backend is running:
curl http://localhost:5001/health # Should return: {"status":"ok","timestamp":"..."} -
Check CORS: Backend has CORS enabled by default
-
Check URL: Make sure URL in settings matches backend
-
Check network: Browser console will show connection errors
Data not persisting
-
Check volume mount:
docker inspect codeforge-backend | grep Mounts -A 10 -
Check permissions:
ls -la ./data -
Check database:
sqlite3 ./data/codeforge.db ".tables"
Port conflicts
# Use different port
docker run -p 8080:5001 ...
# Update URL in settings to match
http://localhost:8080
Security Considerations
⚠️ The default Flask backend has no authentication!
For production:
- Add authentication (JWT, API keys, etc.)
- Use HTTPS/TLS
- Restrict CORS origins
- Add rate limiting
- Use environment-specific configs
API Endpoints
The Flask backend exposes these endpoints:
GET /health- Health checkGET /api/storage/keys- List all keysGET /api/storage/<key>- Get valuePUT /api/storage/<key>- Set/update valueDELETE /api/storage/<key>- Delete valuePOST /api/storage/clear- Clear all dataGET /api/storage/export- Export all dataPOST /api/storage/import- Import dataGET /api/storage/stats- Get statistics
See backend/README.md for detailed API documentation.
Benefits of Flask Backend
✅ Persistent across devices - Access data from any device ✅ Team collaboration - Share data with team members ✅ Backup/restore - Centralized backup location ✅ No size limits - Limited only by server disk space ✅ SQL queries - Server-side SQLite for complex queries ✅ Scalable - Add more storage as needed
Comparison
| Feature | IndexedDB | Flask Backend | SQLite | Spark KV |
|---|---|---|---|---|
| Offline | ✅ Yes | ❌ No | ✅ Yes | ❌ No |
| Cross-device | ❌ No | ✅ Yes | ❌ No | ✅ Yes |
| Size limit | ~50MB+ | Unlimited | ~5MB | Unlimited |
| Speed | Fast | Moderate | Fast | Moderate |
| Setup | None | Docker/Server | npm install | Spark only |
| SQL queries | ❌ No | ✅ Yes | ✅ Yes | ❌ No |
Next Steps
-
Add to settings page:
import { StorageSettings } from '@/components/molecules' function SettingsPage() { return <StorageSettings /> } -
Customize backend - Modify
backend/app.pyas needed -
Add authentication - Secure your backend for production
-
Deploy to cloud - Use AWS, Azure, DigitalOcean, etc.
-
Monitor usage - Use
/api/storage/statsendpoint
Support
- Full documentation:
STORAGE.md - Backend docs:
backend/README.md - Issues: Open a GitHub issue