A comprehensive Flask API server with SQLAlchemy ORM, auto-migration system, and complete web interface. Optimized for Vercel serverless deployment with PostgreSQL database.
- Automatic database migrations on app startup
- Flask-Migrate integration with comprehensive toolkit
- Zero-downtime deployments with fallback mechanisms
- Complete migration management (create, status, rollback, backup)
- User authentication (register, login, logout)
- Post management (create, read, update, delete)
- Responsive design with Tailwind CSS
- Interactive dashboard and user profiles
- Vercel serverless optimized deployment
- PostgreSQL database with connection pooling
- Session management with security features
- Comprehensive error handling and logging
- SQLAlchemy ORM for clean database operations
- Pagination support for large datasets
- API documentation with health checks
- Testing scripts for development and production
- Vercel account for deployment
- PostgreSQL database (Neon, Supabase, or other cloud provider)
- Python 3.8+ for local development
- Git for version control
Choose a cloud PostgreSQL provider:
```bash
```
```bash
```
```bash
git clone cd flask-vercel-app
pip install -r requirements.txt
export DATABASE_URL="postgresql://user:password@host:port/database" export SECRET_KEY="your-secret-key"
python scripts/init_migrations.py
python scripts/setup_database.py
python api/index.py ```
```bash
npm install -g vercel
vercel login
vercel env add DATABASE_URL vercel env add SECRET_KEY
vercel --prod ```
- Startup Check: App automatically checks for pending migrations on cold start
- Auto-Upgrade: Runs migrations automatically within the serverless function
- Fallback Safety: Creates tables if migrations fail
- Zero Downtime: Compatible with Vercel serverless cold starts
```bash
python scripts/create_migration.py "Add user avatar field"
python scripts/migration_status.py python scripts/manual_upgrade.py
git add . git commit -m "Add user avatar field migration" git push origin main
vercel --prod ```
```bash
python scripts/migration_status.py
python scripts/manual_upgrade.py
python scripts/rollback_migration.py prev
python scripts/backup_database.py backup
python scripts/backup_database.py restore backup_file.sql ```
```bash
vercel --prod
vercel logs
vercel --prod --force
vercel logs --follow ```
- Cold Start: When Vercel starts your function,
run_auto_migrations()executes - Migration Check: Function checks for pending migrations automatically
- Execution: Migrations run within the serverless function context
- Fallback: If migrations fail, app falls back to
db.create_all() - Logging: Migration status appears in Vercel function logs
```bash
vercel --prod
vercel logs --follow
vercel ls
vercel env add DATABASE_URL vercel env add SECRET_KEY
vercel --prod --force ```
- Migration files must be committed to git
- Vercel deploys the entire repository including
migrations/directory - Auto-migration runs during function cold start
- No manual
flask db upgradeneeded in production
All endpoints are prefixed with /api/:
POST /api/register- Register new userPOST /api/login- User loginPOST /api/logout- User logout
GET /api/profile- Get user profile (protected)PUT /api/profile- Update user profile (protected)GET /api/users- Get all users (paginated)
POST /api/posts- Create new post (protected)GET /api/posts- Get all posts (paginated)GET /api/posts/<id>- Get specific postPUT /api/posts/<id>- Update post (protected)DELETE /api/posts/<id>- Delete post (protected)
GET /api- API documentationGET /api/health- Health check with migration status
/web- Homepage with features overview/web/register- User registration/web/login- User login (demo: username=demo, password=demo123)/web/dashboard- User dashboard (protected)/web/posts- Browse all posts/web/posts/<id>- View specific post/web/create-post- Create new post (protected)/web/profile- User profile management (protected)
- Responsive Design: Works on desktop and mobile
- Real-time Updates: Dynamic content loading
- User Authentication: Session-based with auto-detection
- Rich Editor: Post creation with preview
- Interactive UI: Modern interface with Tailwind CSS
```bash
python test_api.py
python scripts/seed_data.py ```
```bash
export VERCEL_URL="your-app.vercel.app"
python test_vercel_api.py ```
Set these in Vercel dashboard or CLI:
```bash
DATABASE_URL=postgresql://user:password@host:port/database SECRET_KEY=your-super-secret-key-here
FLASK_ENV=production ```
The vercel.json file is pre-configured for optimal performance:
```json { "version": 2, "builds": [{"src": "api/index.py", "use": "@vercel/python"}], "routes": [ {"src": "/api/(.)", "dest": "api/index.py"}, {"src": "/(.)", "dest": "api/index.py"} ], "functions": { "api/index.py": {"maxDuration": 30} } } ```
```bash
echo $DATABASE_URL
python scripts/migration_status.py ```
```bash
python scripts/migration_status.py python scripts/manual_upgrade.py
vercel logs
vercel ls
vercel --prod --force
python scripts/migration_status.py # (with production DATABASE_URL) ```
```bash
"functions": { "api/index.py": { "maxDuration": 30 } } ```
- Ensure all imports are in
api/index.py - Vercel serverless functions need everything in one file
- Check Python path in scripts
```bash vercel logs your-deployment-url ```
```bash vercel dev ```
```bash
python scripts/backup_database.py backup
python scripts/migration_status.py
python scripts/rollback_migration.py base python scripts/manual_upgrade.py ```
- Execution Time: Max 30 seconds per request
- Memory: Limited memory per function
- File System: Read-only, no persistent storage
- Database: Must use external database
- Background Tasks: No long-running processes
- Connection Pooling: Optimized for serverless
- Auto-Migration: Handles cold starts gracefully
- Session Storage: Cookie-based (consider Redis for scale)
- Error Handling: Comprehensive rollback mechanisms
```python
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = { 'pool_size': 5, 'pool_recycle': 300, 'pool_pre_ping': True, 'connect_args': { 'connect_timeout': 10, 'application_name': 'flask_vercel_app' } } ```
- Health Checks:
/api/healthendpoint - Migration Status: Automatic logging
- Error Tracking: Comprehensive error handlers
- Performance: Vercel Analytics integration
- Password Hashing: Werkzeug security
- Session Management: Secure cookie configuration
- Input Validation: Comprehensive data validation
- SQL Injection: SQLAlchemy ORM protection
- v1.0: Basic Flask server with SQLite
- v1.5: PostgreSQL integration with raw SQL
- v2.0: SQLAlchemy ORM with relationships
- v2.1: Auto-migration system with Flask-Migrate
- v2.2: Flask CLI integration with standard commands
- SQLAlchemy ORM: Chosen for type safety and relationship management
- Flask-Migrate: Selected for robust schema versioning
- Vercel Serverless: Optimized for automatic scaling
- PostgreSQL: Production-grade database with JSON support
- Cookie Sessions: Simple authentication for MVP
- JWT Authentication: Replace cookie sessions
- Input Validation: Add comprehensive validation
- API Rate Limiting: Prevent abuse
- Redis Caching: Improve performance
- File Uploads: Add image support
- User Roles: Admin/user permissions
- Post Categories: Organize content
- Search Functionality: Full-text search
- Email Notifications: User engagement
- API Documentation: Swagger/OpenAPI
- CI/CD Pipeline: Automated testing
- Monitoring: Application performance
- Logging: Centralized log management
- Backup Automation: Scheduled backups
- Load Testing: Performance validation
- Neon Database - Recommended
- Supabase Database
- Railway PostgreSQL
- ElephantSQL
- Fork the repository
- Create feature branch
- Make changes with tests
- Update documentation
- Submit pull request
- Follow PEP 8 for Python code
- Use descriptive commit messages
- Include migration descriptions
- Update README for new features
- β Database: PostgreSQL with SQLAlchemy ORM
- β Migrations: Auto-migration system active
- β Deployment: Vercel serverless optimized
- β Interface: Complete web UI with authentication
- β API: RESTful endpoints with documentation
- β Testing: Comprehensive test scripts
- β Documentation: Complete setup and usage guides
Version: 2.2.0-flask-cli Last Updated: December 2024 Status: Production Ready π