FastAPI Video Backend
A FastAPI backend for the multi-agent video generation system using Pydantic for data modeling, Clerk for authentication, and Redis for caching and job queuing.
Features
- FastAPI Framework: Modern, fast web framework for building APIs
- Pydantic Models: Data validation and serialization
- Clerk Authentication: Secure user authentication and management
- Redis Integration: Caching and job queue management
- Structured Logging: JSON-formatted logging with structlog
- Environment Configuration: Flexible configuration with Pydantic Settings
- CORS Support: Cross-origin resource sharing configuration
- Health Checks: Built-in health monitoring endpoints
- Testing Suite: Comprehensive test coverage with pytest
Quick Start
Prerequisites
- Python 3.11+
- Redis server
- Clerk account (for authentication)
Installation
Clone the repository
git clone <repository-url> cd fastapi-video-backendInstall dependencies
pip install -e .Set up development environment
python scripts/setup.pyConfigure environment variables
Update the
.envfile with your actual configuration:# Required: Get these from your Clerk dashboard CLERK_SECRET_KEY=your_actual_clerk_secret_key CLERK_PUBLISHABLE_KEY=your_actual_clerk_publishable_key # Required: Generate a secure secret key SECRET_KEY=your_super_secret_key_hereStart Redis server
redis-serverRun the application
# Development mode python -m src.app.main # Or using the script python -m uvicorn src.app.main:app --reloadAccess the API
- API Documentation: http://localhost:8000/docs
- Alternative Docs: http://localhost:8000/redoc
- Health Check: http://localhost:8000/health
Project Structure
src/
βββ app/
β βββ main.py # FastAPI application entry point
β βββ api/ # API layer
β β βββ dependencies.py # Shared dependencies
β β βββ v1/ # API version 1
β β βββ videos.py # Video generation endpoints
β β βββ jobs.py # Job management endpoints
β β βββ system.py # System health endpoints
β βββ core/ # Core utilities and configurations
β β βββ config.py # Application settings
β β βββ redis.py # Redis connection and utilities
β β βββ auth.py # Clerk authentication utilities
β β βββ logger.py # Logging configuration
β β βββ exceptions.py # Custom exceptions
β βββ services/ # Business logic layer
β β βββ video_service.py # Video generation business logic
β β βββ job_service.py # Job management logic
β β βββ queue_service.py # Redis queue management
β βββ models/ # Pydantic models
β β βββ job.py # Job data models
β β βββ video.py # Video metadata models
β β βββ user.py # User data models
β β βββ system.py # System status models
β βββ middleware/ # Custom middleware
β β βββ cors.py # CORS middleware
β β βββ clerk_auth.py # Clerk authentication middleware
β β βββ error_handling.py # Global error handling
β βββ utils/ # Utility functions
β βββ file_utils.py # File handling utilities
β βββ helpers.py # General helper functions
βββ tests/ # Test suite
βββ scripts/ # Utility scripts
Configuration
The application uses environment-based configuration with Pydantic Settings. All configuration options are documented in .env.example.
Key Configuration Sections
- Application Settings: Basic app configuration
- Server Settings: Host, port, and server options
- Redis Settings: Redis connection and caching configuration
- Clerk Settings: Authentication configuration
- Security Settings: JWT and security configuration
- Logging Settings: Structured logging configuration
Development
Running Tests
# Run all tests
pytest
# Run with coverage
pytest --cov=src
# Run specific test file
pytest tests/test_main.py
Code Quality
# Format code
black src/ tests/
# Sort imports
isort src/ tests/
# Lint code
flake8 src/ tests/
# Type checking
mypy src/
Development Scripts
python scripts/setup.py- Set up development environmentpython -m src.app.main- Run development serverpytest- Run test suite
API Documentation
Once the application is running, you can access:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- OpenAPI JSON: http://localhost:8000/openapi.json
Health Monitoring
The application includes built-in health check endpoints:
GET /health- Basic health statusGET /- Root endpoint with basic information
Logging
The application uses structured logging with configurable output formats:
- Development: Colorized console output
- Production: JSON-formatted logs
Log levels and formats can be configured via environment variables.
Security
- Authentication: Clerk-based JWT authentication
- CORS: Configurable cross-origin resource sharing
- Rate Limiting: Built-in rate limiting support
- Input Validation: Pydantic-based request validation
- Security Headers: Automatic security header injection
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
License
This project is licensed under the MIT License - see the LICENSE file for details.