t2m / README.md
thanhkt's picture
implement core api
50a7bf0
# 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
1. **Clone the repository**
```bash
git clone <repository-url>
cd fastapi-video-backend
```
2. **Install dependencies**
```bash
pip install -e .
```
3. **Set up development environment**
```bash
python scripts/setup.py
```
4. **Configure environment variables**
Update the `.env` file with your actual configuration:
```bash
# 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_here
```
5. **Start Redis server**
```bash
redis-server
```
6. **Run the application**
```bash
# Development mode
python -m src.app.main
# Or using the script
python -m uvicorn src.app.main:app --reload
```
7. **Access 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
```bash
# Run all tests
pytest
# Run with coverage
pytest --cov=src
# Run specific test file
pytest tests/test_main.py
```
### Code Quality
```bash
# 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 environment
- `python -m src.app.main` - Run development server
- `pytest` - 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 status
- `GET /` - 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
1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Add tests for new functionality
5. Ensure all tests pass
6. Submit a pull request
## License
This project is licensed under the MIT License - see the LICENSE file for details.