Skip to content

Suk022/Python-based-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Based Backend

A Python-based REST API backend that demonstrates modern web development practices. It's a paragraph management system with user authentication, text processing, and search functionality. The core features include JWT-based authentication, paragraph submission, automatic word indexing using background tasks, and frequency-based search."

High-Level Architecture

Client (Browser/Postman) 
    ↓ HTTP Requests
FastAPI Application
    ↓ Database Operations
SQLAlchemy ORM
    ↓ Data Storage
SQLite Database
    ↓ Background Processing
FastAPI BackgroundTasks (Word Indexing)

Features

  • User Authentication: JWT-based auth with register/login/logout
  • Paragraph Management: Submit multiple paragraphs in a single request
  • Word Indexing: Automatic background indexing with word frequency tracking
  • Smart Search: Find top 10 paragraphs ranked by word frequency
  • Containerized: Docker setup with lightweight images
  • Interactive Docs: Auto-generated Swagger UI documentation

Tech Stack

  • Backend: FastAPI (Python 3.11+)
  • Database: SQLAlchemy ORM with SQLite
  • Authentication: JWT tokens with bcrypt password hashing
  • Background Tasks: FastAPI BackgroundTasks for word indexing
  • Testing: pytest with comprehensive test coverage
  • Deployment: Docker, Render-ready configuration

Quick Start

Prerequisites

  • Python 3.11+
  • pip (or Docker for containerized setup)

Installation

  1. Clone the repository:

    git clone https://github.com/Suk022/Python-based-backend.git
    cd backend-assignment
  2. Install dependencies:

    pip install -r requirements.txt
  3. Run the application:

    uvicorn app.main:app --reload --port 8000
  4. Access the API:

    • API Documentation: http://localhost:8000/docs
    • Health Check: http://localhost:8000/health
    • Root Endpoint: http://localhost:8000/

API Endpoints

Authentication

  • POST /auth/register - Register new user
  • POST /auth/login - User login (returns JWT token)
  • POST /auth/logout - Logout and invalidate refresh token

Paragraphs

  • POST /paragraphs - Submit multiple paragraphs
  • GET /paragraphs - List user paragraphs (paginated)
  • GET /paragraphs/search?word=<word> - Search paragraphs by word frequency

Documentation

  • GET /docs - Interactive Swagger UI
  • GET / - API information
  • GET /health - Health check endpoint

Testing Guide

Below are the instructions for testing APIs using different methods:

  1. Start the application:

    # Using Python directly
    uvicorn app.main:app --reload --port 8000
    
    # Using Docker
    docker-compose -f docker-compose.dev.yml up --build
  2. Open Swagger UI: http://localhost:8000/docs

  3. Test User Registration:

    • Find POST /auth/register
    • Click "Try it out"
    • Enter test data:
      {
        "email": "test@example.com",
        "password": "password123"
      }
    • Click "Execute"
    • Expected: Status 200, user_id returned
  4. Test User Login:

    • Find POST /auth/login
    • Use same credentials as registration
    • Click "Execute"
    • Copy the access_token from response
  5. Authorize for Protected Endpoints:

    • Click "Authorize" button (top right)
    • Enter: YOUR_ACCESS_TOKEN
    • Click "Authorize"
  6. Test Paragraph Submission:

    • Find POST /paragraphs/
    • Enter test paragraphs:
      {
        "paragraphs": [
          "Python is a great programming language. Python is easy to learn.",
          "I love coding in Python. Python has many libraries.",
          "JavaScript is popular, but Python is my favorite programming language."
        ]
      }
    • Click "Execute"
    • Expected: Status 200, success message
  7. Test Word Search:

    • Find GET /paragraphs/search
    • Enter search word: python
    • Click "Execute"
    • Expected: Paragraphs ranked by word frequency
  8. Test Paragraph Listing:

    • Find GET /paragraphs/
    • Click "Execute"
    • Expected: Paginated list of your paragraphs

Method 2: Using Postman

  1. Import Collection: Create requests for each endpoint
  2. Set Environment Variable: {{token}} for authorization
  3. Test Flow: Register - Login - Save token - Test protected endpoints

Method 3: Automated Testing

# Register user
curl -X POST "http://localhost:8000/auth/register" \
  -H "Content-Type: application/json" \
  -d '{"email": "user@example.com", "password": "password123"}'

# Login
curl -X POST "http://localhost:8000/auth/login" \
  -H "Content-Type: application/json" \
  -d '{"email": "user@example.com", "password": "password123"}'

# Submit paragraphs (use token from login)
curl -X POST "http://localhost:8000/paragraphs/" \
  -H "Authorization: Bearer <your-token>" \
  -H "Content-Type: application/json" \
  -d '{"paragraphs": ["I love apple pie. Apple is sweet.", "The apple tree grows apples."]}'

# Search for word
curl -X GET "http://localhost:8000/paragraphs/search?word=apple" \
  -H "Authorization: Bearer <your-token>"

Testing

# Run tests (uses SQLite automatically)
make test

# Or directly with pytest
USE_SQLITE=true python -m pytest tests/ -v

Make Commands

make build      # Build Docker images
make up         # Start production mode (all services)
make dev-up     # Start development mode (web only)
make test       # Run tests
make clean      # Clean Docker resources
make clean-all  # Clean everything including images
make dev-local  # Run locally without Docker

Architecture

Models

  • User - User accounts with email/password
  • Paragraph - User-submitted text content
  • WordCount - Global word frequency per user
  • ParagraphWordCount - Word frequency per paragraph
  • RefreshToken - JWT refresh token storage

Indexing Process

  1. User submits paragraphs via POST /paragraphs
  2. Paragraphs are stored immediately
  3. Background task indexes words:
    • Tokenizes text (lowercase, remove punctuation)
    • Updates ParagraphWordCount (per paragraph)
    • Updates WordCount (global user totals)
    • Uses SQL upserts for concurrency safety

Search Algorithm

  1. Query ParagraphWordCount for user + word
  2. Join with Paragraph for content
  3. Order by word count descending
  4. Return top 10 results

Database Indexes

Optimized for fast search:

  • (user_id, word) - Word lookup
  • (user_id, word, count) - Frequency sorting
  • (paragraph_id) - Paragraph joins

Live Demo

The project is deployed on Render and can be accessed via https://python-based-backend.onrender.com/. Test the APIs directly through the link.

About

Python Backend demonstrating auth, session management, and text-analysis APIs

Resources

Stars

Watchers

Forks