Skip to main content

Nipun Bharat Backend

This is the repository for the mobile app for Nipun Bharat (SCERT) project - Backend

Description

This repository contains the backend code for the Nipun Bharat mobile application, a project by SCERT (State Council of Educational Research and Training). The backend is built using FastAPI (Python) with MongoDB for data storage. It provides APIs for managing users, students, questions, assessments, and media files, supporting the functionality of the Nipun Bharat mobile app.

Project Structure

The project is organized into two main components:

  1. Main Application: The core Nipun Bharat backend with full functionality
  2. LMS Application: A specialized Learning Management System component

Main Application

The main application provides comprehensive APIs for the Nipun Bharat mobile app, including user management, authentication, student management, assessments, and media handling.

LMS Application

The LMS application is a streamlined version focused specifically on content management functionality. It's located in the lms-app/ directory and can be deployed independently.

Key Features

  • User Management: APIs for creating and managing user accounts (teachers, headmasters, and admins).
  • Authentication: Secure authentication using OTP and JWT tokens.
  • Student Management: APIs for managing student information, including profile images.
  • Question Bank: APIs for creating, retrieving, updating, and deleting questions for various test types (maths, writing, speaking).
  • Assessment Results: APIs for recording and retrieving assessment results for students.
  • FLN Function: Integration with speech-to-text and modified LCS algorithm for evaluating speaking tests.
  • Media Management: Integration with AWS S3 for storing and serving media files (audio and images) via CloudFront CDN.
  • Caching: Redis caching to be implemented later to improve API performance.
  • Scalability: Designed for high scalability with asynchronous operations (using Motor), connection pooling, and proper indexing.
  • CI/CD Pipeline: Automated testing and deployment using GitHub Actions and AWS ECS.
  • LMS Component: Specialized Learning Management System for content management.

Technologies Used

  • FastAPI: A modern, fast (high-performance), web framework for building APIs with Python 3.7+
  • MongoDB: A NoSQL database used for storing application data.
  • Motor: An asynchronous MongoDB driver for Python.
  • Pydantic: A data validation and settings management library.
  • PyMongo: The official MongoDB driver for Python.
  • AWS S3: Used for storing media files (audio and images).
  • AWS CloudFront: A content delivery network (CDN) for serving media files.
  • Redis: An in-memory data structure store, to be implemented later for caching.
  • Boto3: The AWS SDK for Python.
  • Uvicorn: An ASGI server for running the FastAPI application.
  • Docker: For containerization of the application.
  • GitHub Actions: For CI/CD pipeline.
  • AWS ECS: For container orchestration.

File Structure

backend/
├── main.py # FastAPI application entry point - Defines the API endpoints and overall app structure.
├── app/
│ ├── core/
│ │ ├── config.py # Environment and AWS configuration - Loads settings from environment variables.
│ │ └── security.py # Authentication and authorization - Handles user authentication and authorization.
│ ├── models/
│ │ ├── database.py # MongoDB connection - Establishes connection to the MongoDB database.
│ │ └── models.py # Pydantic models - Defines data structures for request and response bodies.
│ ├── api/
│ │ ├── routes/
│ │ │ ├── auth.py # Authentication routes - API endpoints for OTP-based authentication.
│ │ │ ├── users.py # User management routes - API endpoints for managing user accounts.
│ │ │ ├── students.py # Student management routes - API endpoints for managing student information.
│ │ │ ├── questions.py # Question management routes - API endpoints for managing questions.
│ │ │ ├── assessments.py # Assessment management routes - API endpoints for managing assessments.
│ │ │ ├── media.py # S3 media handling routes - API endpoints for handling media files on S3.
│ │ │ └── init.py # Initializes the routes package - Combines all the routes into a single package.
│ │ └── dependencies.py # Shared dependencies - Defines dependencies used across multiple API endpoints.
│ ├── services/
│ │ ├── s3.py # AWS S3 integration - Handles interactions with AWS S3 for media storage.
│ │ ├── cloudfront.py # CloudFront URL generation - Generates signed URLs for accessing media files via CloudFront.
│ │ ├── cache.py # Redis caching service - To be implemented later for improving API performance.
│ │ └── fln.py # FLN service - Implements speech-to-text and modified LCS algorithm for evaluating speaking tests.
│ └── utils/
│ ├── indexes.py # MongoDB indexing - Defines indexes for MongoDB collections to optimize query performance.
│ └── validators.py # Custom validators - Defines custom data validators for Pydantic models.
├── .github/
│ └── workflows/
│ └── ci-cd.yml # CI/CD pipeline configuration - Defines the GitHub Actions workflow for CI/CD.
├── Dockerfile # Docker configuration - Defines the Docker image for the application.
├── requirements.txt # Python dependencies - Lists all the Python packages required by the application.
├── README.md # Project README file - Provides documentation and setup instructions for the project.
└── .env # Environment variables - Stores sensitive information and configuration settings.

Setup Instructions

  1. Clone the repository:

    git clone [repository_url]
    cd nipun-bharat-BE
  2. Create a virtual environment:

    python3 -m venv venv
    source venv/bin/activate
  3. Install dependencies:

    pip install -r requirements.txt
  4. Configure environment variables:

    • Create a .env file in the root directory.
    • Add the necessary environment variables (e.g., MongoDB URL, AWS credentials, etc.). See app/core/config.py for the required variables.
    MONGODB_URL=mongodb://localhost:27017
    DATABASE_NAME=nipun_bharat
    AWS_ACCESS_KEY_ID=YOUR_AWS_ACCESS_KEY_ID
    AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET_ACCESS_KEY
    AWS_REGION=YOUR_AWS_REGION
    S3_BUCKET_NAME=YOUR_S3_BUCKET_NAME
    CLOUDFRONT_DOMAIN=YOUR_CLOUDFRONT_DOMAIN
    CLOUDFRONT_KEY_PAIR_ID=YOUR_CLOUDFRONT_KEY_PAIR_ID
    CLOUDFRONT_PRIVATE_KEY_PATH=/path/to/your/cloudfront_private_key.pem
    # Redis settings (to be configured later)
    # REDIS_HOST=localhost
    # REDIS_PORT=6379
    # REDIS_PASSWORD=YOUR_REDIS_PASSWORD
    SECRET_KEY=YOUR_SECRET_KEY
  5. Run the application:

    uvicorn app.main:app --reload

    This will start the FastAPI application with hot reloading enabled.

  6. Using Docker:

    docker build -t nipun-bharat-api .
    docker run -p 8000:8000 --env-file .env nipun-bharat-api

API Endpoints

The API documentation can be accessed at http://localhost:8000/docs after running the application.

Authentication

  • POST /api/auth/request-otp: Request OTP for login
  • POST /api/auth/verify-otp: Verify OTP and get access token
  • GET /api/auth/me: Get current user information

Users

  • GET /api/users/: Get all users
  • POST /api/users/: Create a new user
  • GET /api/users/{user_id}: Get a user by ID
  • PUT /api/users/{user_id}: Update a user
  • DELETE /api/users/{user_id}: Delete a user

Students

  • GET /api/students/: Get all students
  • POST /api/students/: Create a new student
  • GET /api/students/{student_id}: Get a student by ID
  • PUT /api/students/{student_id}: Update a student
  • DELETE /api/students/{student_id}: Delete a student

Questions

  • GET /api/questions/: Get all questions
  • POST /api/questions/: Create a new question
  • GET /api/questions/{question_id}: Get a question by ID
  • PUT /api/questions/{question_id}: Update a question
  • DELETE /api/questions/{question_id}: Delete a question
  • POST /api/questions/bulk-upload: Bulk upload questions

Assessments

  • GET /api/assessments/: Get all assessments
  • POST /api/assessments/: Create a new assessment
  • GET /api/assessments/{assessment_id}: Get an assessment by ID
  • PUT /api/assessments/{assessment_id}: Update an assessment
  • DELETE /api/assessments/{assessment_id}: Delete an assessment
  • POST /api/assessments/{assessment_id}/audio-response: Upload an audio response for a speaking assessment
  • POST /api/assessments/{assessment_id}/image-response: Upload an image response for a math or writing assessment
  • PUT /api/assessments/{assessment_id}/level: Update the level of a student for an assessment

Media

  • POST /api/media/upload: Upload a file to S3
  • GET /api/media/{file_key}: Get a signed URL for a file

Testing

The project includes a comprehensive test suite built with pytest. The tests ensure that the API endpoints and services function correctly, particularly the critical FLN (Foundational Literacy & Numeracy) evaluation service.

Test Structure

  • Fixtures: Located in tests/conftest.py, providing mock implementations of MongoDB and AWS services.
  • API Tests: Tests for API endpoints, located in tests/api/.
  • Service Tests: Tests for internal services, located in tests/services/.
  • Integration Tests: End-to-end tests that verify the interaction between components, located in tests/integration/.

Running Tests

To run the test suite:

# Run all tests
pytest

# Run specific test file
pytest tests/api/test_auth.py

# Run tests with verbosity
pytest -v

# Run tests and generate coverage report
pytest --cov=app tests/

Key Test Areas

  1. Authentication: Tests for OTP generation, verification, and JWT token handling.
  2. Student Management: Tests for creating, retrieving, updating, and deleting student records.
  3. Assessment Evaluation: Tests for the FLN service that performs speech-to-text conversion and accuracy calculation.
  4. Media Handling: Tests for file uploads to S3 and signed URL generation via CloudFront.
  5. Database Operations: Tests for MongoDB operations with proper error handling.

CI/CD Integration

The tests are integrated into the CI/CD pipeline (GitHub Actions) to ensure that all changes pass the test suite before being deployed to production.

Code Quality

The codebase follows strict code quality guidelines to ensure maintainability and reliability:

Code Structure and Organization

  • Modular Design: The codebase is organized into modules with clear responsibilities (routes, services, models, utilities).
  • Function Decomposition: Complex functions are broken down into smaller, focused helper functions to improve readability and maintainability.
  • Single Responsibility Principle: Each module and function has a clear, single responsibility.

Linting and Style

  • PEP 8 Compliance: The code follows PEP 8 style guidelines for Python code.
  • Line Length: Code lines are limited to 79 characters for better readability across different environments.
  • Consistent Spacing: Proper spacing around operators and between functions improves code readability.
  • Documentation: All functions have docstrings explaining their purpose, parameters, and return values.

Performance Optimization

  • Caching: Redis caching will be implemented later for frequently accessed data to reduce database load.
  • Asynchronous Processing: FastAPI's async capabilities are leveraged for I/O-bound operations.
  • Query Optimization: MongoDB queries are optimized with proper indexing defined in utils/indexes.py.

Error Handling

  • Comprehensive Error Handling: All API endpoints include proper error handling with meaningful error messages.
  • Input Validation: Pydantic models are used for input validation to catch errors early in the request pipeline.
  • Logging: Detailed logging is implemented to aid in debugging and monitoring.

To maintain code quality, the CI/CD pipeline includes automated linting and testing to catch issues early in the development process.

Contributing

Please follow these guidelines when contributing to the project:

  1. Fork the repository.
  2. Create a new branch for your feature or bug fix.
  3. Write tests for your code.
  4. Submit a pull request.

License

MIT License