A comprehensive learning platform backend built with FastAPI, featuring AI-powered content generation, advanced analytics, and scalable architecture.
This backend follows a modern, scalable architecture with clear separation of concerns:
- Configuration Layer: Environment-based settings and database management
- Core Layer: Security, middleware, exceptions, and common utilities
- Models Layer: SQLAlchemy models with comprehensive relationships
- Services Layer: Business logic with AI/LLM and vector database integrations
- API Layer: RESTful endpoints with comprehensive validation
- CRUD Layer: Database operations with caching strategies
backend/
βββ app/
β βββ main.py # FastAPI application entry point
β βββ config/ # Configuration management
β β βββ settings.py # Environment-specific configurations
β β βββ database.py # Database connection and session management
β β βββ redis.py # Redis connection configuration
β β βββ logging.py # Logging configuration
β βββ core/ # Core application modules
β β βββ security.py # JWT, password hashing, auth utilities
β β βββ dependencies.py # FastAPI dependency injection
β β βββ exceptions.py # Custom exception classes
β β βββ middleware.py # Custom middleware (CORS, logging, etc.)
β β βββ events.py # Startup/shutdown event handlers
β βββ models/ # SQLAlchemy models
β β βββ base.py # Base model with common fields
β β βββ user.py # User model with preferences
β β βββ topic.py # Learning topics and categories
β β βββ content.py # Learning cards and content
β β βββ session.py # Learning sessions and progress
β β βββ analytics.py # User interaction analytics
β β βββ associations.py # Many-to-many relationship tables
β βββ schemas/ # Pydantic schemas
β β βββ base.py # Base Pydantic schemas
β β βββ user.py # User request/response schemas
β β βββ topic.py # Topic schemas
β β βββ content.py # Content schemas
β β βββ session.py # Session schemas
β β βββ analytics.py # Analytics schemas
β βββ api/ # API endpoints
β β βββ deps.py # API dependencies
β β βββ v1/
β β βββ router.py # Main API router
β β βββ endpoints/
β β βββ auth.py # Authentication endpoints
β β βββ users.py # User management
β β βββ topics.py # Topic management
β β βββ content.py # Content CRUD operations
β β βββ learning.py # Learning session endpoints
β β βββ analytics.py # Analytics and progress tracking
β β βββ admin.py # Admin-only endpoints
β βββ services/ # Business logic services
β β βββ auth_service.py # Authentication business logic
β β βββ user_service.py # User management service
β β βββ content_service.py # Content management service
β β βββ learning_service.py # Learning session management
β β βββ analytics_service.py # Analytics computation
β β βββ llm/ # AI/LLM integration
β β β βββ base.py # Abstract LLM interface
β β β βββ openai_client.py # OpenAI GPT integration
β β β βββ anthropic_client.py # Claude integration
β β β βββ content_generator.py # Content generation orchestrator
β β β βββ prompt_templates.py # LLM prompt templates
β β β βββ content_validator.py # Generated content validation
β β βββ vector/ # Vector database integration
β β β βββ base.py # Abstract vector database interface
β β β βββ pinecone_client.py # Pinecone integration
β β β βββ chroma_client.py # Chroma integration
β β β βββ semantic_search.py # Semantic content search
β β βββ cache/ # Caching services
β β β βββ redis_client.py # Redis caching service
β β β βββ cache_strategies.py # Caching strategies and TTL management
β β βββ external/ # External service integrations
β β βββ s3_client.py # AWS S3 media storage
β β βββ cloudinary_client.py # Cloudinary integration
β β βββ moderation_client.py # Content moderation service
β βββ tasks/ # Background tasks (Celery)
β β βββ celery_app.py # Celery configuration
β β βββ content_generation.py # Background content generation
β β βββ analytics_processing.py # Analytics computation tasks
β β βββ notifications.py # Push notification tasks
β βββ crud/ # Database operations
β β βββ base.py # Base CRUD operations
β β βββ user.py # User CRUD operations
β β βββ topic.py # Topic CRUD operations
β β βββ content.py # Content CRUD operations
β β βββ session.py # Session CRUD operations
β βββ utils/ # Utility functions
β β βββ validators.py # Custom validation functions
β β βββ formatters.py # Data formatting utilities
β β βββ constants.py # Application constants
β β βββ helpers.py # General utility functions
β βββ tests/ # Test suite
β βββ conftest.py # Pytest configuration and fixtures
β βββ test_auth.py # Authentication tests
β βββ test_content.py # Content generation tests
β βββ test_learning.py # Learning flow tests
β βββ test_analytics.py # Analytics tests
βββ migrations/ # Alembic database migrations
βββ scripts/ # Utility scripts
βββ docker/ # Docker configuration
βββ requirements/ # Python dependencies
βββ .env.example # Environment variables template
βββ pyproject.toml # Python project configuration
βββ README.md
- User Management: Complete user authentication, authorization, and profile management
- Learning Content: Flexible content system supporting various types (cards, videos, articles, quizzes)
- Learning Sessions: Track user progress and learning sessions with detailed analytics
- Spaced Repetition: Built-in spaced repetition system for optimal learning
- Analytics: Comprehensive user analytics and learning progress tracking
- Content Generation: AI-powered learning content generation using OpenAI/Anthropic
- Semantic Search: Vector database integration for intelligent content discovery
- Content Moderation: Automated content filtering and safety checks
- Personalization: AI-driven learning path recommendations
- Async/Await: Full async support for high performance
- Caching: Redis integration for high-performance caching
- Background Tasks: Celery integration for async task processing
- Security: JWT authentication, rate limiting, security headers
- Monitoring: Comprehensive logging and request tracking
- Scalability: Microservices-ready architecture
- Python 3.12+
- Poetry
- Redis (optional, for caching)
- PostgreSQL (optional, SQLite works for development)
-
Navigate to backend directory:
cd backend -
Install dependencies:
poetry install
-
Set up environment variables:
cp .env.example .env # Edit .env with your actual values -
Run the application:
poetry run python app/main.py
- Interactive API Docs: http://localhost:8000/docs
- Alternative API Docs: http://localhost:8000/redoc
# Initialize Alembic (if not done)
poetry run alembic init migrations
# Create migration
poetry run alembic revision --autogenerate -m "Description"
# Apply migration
poetry run alembic upgrade head# Format code
poetry run black .
# Sort imports
poetry run isort .
# Type checking
poetry run mypy .# Run tests
poetry run pytest
# Run with coverage
poetry run pytest --cov=appSee .env.example for all required environment variables.
# Build image
docker build -f docker/Dockerfile -t infinity-backend .
# Run with docker-compose
docker-compose -f docker/docker-compose.prod.yml up -d# Install production dependencies
poetry install --no-dev
# Run with Gunicorn
poetry run gunicorn app.main:app -w 4 -k uvicorn.workers.UvicornWorkerPOST /api/v1/auth/login- User loginPOST /api/v1/auth/register- User registrationPOST /api/v1/auth/refresh- Refresh tokenPOST /api/v1/auth/logout- User logout
GET /api/v1/users/me- Get current userPUT /api/v1/users/me- Update profileGET /api/v1/users/{id}- Get user by ID
GET /api/v1/topics/- List topicsPOST /api/v1/topics/- Create topicGET /api/v1/topics/{id}- Get topic details
GET /api/v1/content/- List contentPOST /api/v1/content/- Create contentPOST /api/v1/content/generate- AI-generate content
POST /api/v1/learning/sessions- Start learning sessionPUT /api/v1/learning/sessions/{id}- Update session progressGET /api/v1/learning/progress- Get learning progress
GET /api/v1/analytics/dashboard- User dashboard dataGET /api/v1/analytics/progress- Learning progress analyticsGET /api/v1/analytics/reports- Detailed reports
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
This project is licensed under the MIT License.