7.7 KiB
Backend Configuration Guide
This guide explains how to configure CodeSnippet to use different storage backends.
Storage Options
CodeSnippet supports two storage backends:
1. IndexedDB (Default)
- Local browser storage using SQLite compiled to WebAssembly
- No server required
- Data persists only on the current device/browser
- Best for personal use or offline scenarios
2. Flask Backend
- Remote server storage with a Flask REST API
- Requires running a Flask backend server
- Data accessible from any device
- Best for team use or multi-device access
Configuration Methods
Method 1: Automatic Configuration (Environment Variable)
The simplest way to configure Flask backend is using the VITE_FLASK_BACKEND_URL environment variable.
When to use:
- Production deployments
- Docker/containerized environments
- When you want to enforce backend usage
How it works:
- App automatically connects to Flask backend on startup
- Manual backend selection is disabled in Settings UI
- Overrides any saved user preferences
Setup:
Create .env file in project root:
VITE_FLASK_BACKEND_URL=http://localhost:5000
Or set in Docker:
# docker-compose.yml
services:
frontend:
environment:
- VITE_FLASK_BACKEND_URL=http://backend:5000
Or for Docker build:
docker build --build-arg VITE_FLASK_BACKEND_URL=http://api.example.com .
Method 2: Manual Configuration (Settings Page)
Users can manually switch backends in the Settings page.
When to use:
- Development/testing
- User preference scenarios
- When Flask backend is optional
How it works:
- Navigate to Settings → Storage Backend
- Select "Flask Backend (Remote Server)"
- Enter Flask backend URL
- Test connection
- Save settings
Note: Manual configuration is automatically disabled when VITE_FLASK_BACKEND_URL is set.
Backend Deployment Scenarios
Scenario 1: Full Docker Stack (Recommended for Production)
Both frontend and backend in Docker with automatic configuration:
docker-compose up -d
Frontend: http://localhost:3000
Backend: http://localhost:5000
The frontend automatically connects to backend via internal Docker network.
Scenario 2: Local Development
Backend in Docker, frontend in dev mode:
# Terminal 1: Start backend
docker-compose up backend
# Terminal 2: Start frontend with env var
echo "VITE_FLASK_BACKEND_URL=http://localhost:5000" > .env
npm run dev
Scenario 3: Backend Only
Run backend separately, users configure manually:
cd backend
python app.py
Users go to Settings and configure http://localhost:5000 manually.
Scenario 4: Remote Backend
Point frontend to a remote Flask API:
# .env
VITE_FLASK_BACKEND_URL=https://api.example.com
Or configure manually in Settings with the remote URL.
Data Migration
From IndexedDB to Flask
- Ensure Flask backend is running
- Go to Settings → Storage Backend
- Select "Flask Backend"
- Enter backend URL and test connection
- Click "Migrate IndexedDB Data to Flask"
- Save storage settings
From Flask to IndexedDB
- Ensure you have Flask backend URL configured
- Go to Settings → Storage Backend
- Click "Migrate Flask Data to IndexedDB"
- Select "IndexedDB (Local Browser Storage)"
- Save storage settings
Note: Migration copies data, it doesn't move it. Original data remains in the source.
Environment Variable Reference
| Variable | Description | Default | Example |
|---|---|---|---|
VITE_FLASK_BACKEND_URL |
Flask backend URL. When set, forces Flask backend usage. | (none) | http://localhost:5000 or https://backend.example.com |
CORS_ALLOWED_ORIGINS |
Comma-separated list of allowed frontend origins for CORS. | * |
https://frontend.example.com |
DATABASE_PATH |
Path to SQLite database file in backend. | /app/data/snippets.db |
/app/data/snippets.db |
Troubleshooting
"Connection failed" error
Causes:
- Backend server not running
- Incorrect URL
- CORS issues
- Network/firewall blocking
Solutions:
- Verify backend is running:
curl http://localhost:5000/health - Check URL spelling and port number
- Review backend logs for errors
- Ensure CORS is enabled in Flask app (see CORS-GUIDE.md)
- For production deployments, see DEPLOYMENT.md
Environment variable not working
Causes:
- File named incorrectly (must be
.env) - Variable not prefixed with
VITE_ - Server not restarted after change
Solutions:
- Ensure file is named
.env(not.env.localor.env.txt) - Restart dev server:
npm run dev - For production builds: rebuild with
npm run build - Verify with:
console.log(import.meta.env.VITE_FLASK_BACKEND_URL)
Settings page is read-only
This is expected when VITE_FLASK_BACKEND_URL is set. To enable manual configuration:
- Remove the environment variable from
.env - Restart the application
- Settings will become editable
Data not syncing between devices
Ensure:
- All devices are configured to use the same Flask backend URL
- Backend server is accessible from all devices (not localhost if remote)
- Backend has persistent storage (volume mounted in Docker)
Security Considerations
Production Deployment
- Use HTTPS: Always use
https://URLs in production - Authentication: Consider adding authentication to Flask backend
- CORS: Configure CORS to allow only your frontend domain
- Network: Run backend in private network, not exposed to internet
Example secure configuration:
# backend/app.py
from flask_cors import CORS
CORS(app, origins=['https://your-frontend-domain.com'])
# .env (production)
VITE_FLASK_BACKEND_URL=https://api.your-domain.com
Best Practices
- Development: Use IndexedDB or local Flask backend
- Staging: Use Flask backend with test data
- Production: Use Flask backend with environment variable
- Backup: Regularly export database from Settings page
- Migration: Test data migration with small dataset first
Architecture Diagram
┌─────────────────────────────────────┐
│ React Frontend │
│ │
│ ┌──────────────────────────────┐ │
│ │ Storage Config Layer │ │
│ │ (checks env var first) │ │
│ └──────────┬───────────────────┘ │
│ │ │
│ ┌──────┴──────┐ │
│ │ │ │
│ ┌───▼───┐ ┌───▼───────┐ │
│ │ IDB │ │ Flask │ │
│ │Adapter│ │ Adapter │ │
│ └───────┘ └─────┬─────┘ │
└────────────────────┼───────────────┘
│
│ HTTP
│
┌──────▼──────┐
│ Flask │
│ Backend │
│ + SQLite │
└─────────────┘