Skip to content

DevAnabKhan/socialMediaProjectBackend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Backend API – FastAPI

This repository contains the backend service for the application, built using Python FastAPI. It provides a clean, scalable REST API for user authentication, profiles, posts, likes, follows, and related social features.

The backend is designed following professional backend practices, focusing on modular architecture, clear separation of concerns, and best practices


🚀 Tech Stack

  • Python 3.10+
  • FastAPI – High‑performance web framework
  • Pydantic – Data validation and serialization
  • SQLAlchemy / SQLModel – ORM (depending on implementation)
  • PostgreSQL / SQLite – Database (configurable)
  • JWT Authentication – Secure token‑based auth
  • Uvicorn – ASGI server

📁 Project Structure

app/
├── core/
│   ├── config.py        # Environment & app settings
│   ├── security.py      # JWT, password hashing
│   └── dependencies.py # Shared dependencies
│
├── db/
│   ├── base.py          # Base ORM models
│   ├── session.py       # DB session handling
│   └── init_db.py       # DB initialization
│
├── models/
│   ├── user.py          # User ORM model
│   ├── post.py          # Post ORM model
│   └── follow.py        # Follow relationship model
│
├── schemas/
│   ├── user.py          # User request/response schemas
│   ├── post.py          # Post schemas
│   └── auth.py          # Auth schemas
│
├── repositories/
│   ├── user_repo.py     # User DB operations
│   ├── post_repo.py     # Post DB operations
│   └── follow_repo.py   # Follow/unfollow logic
│
├── api/
│   ├── routes/
│   │   ├── auth.py      # Login & auth routes
│   │   ├── users.py     # User & profile routes
│   │   ├── posts.py     # Create, like, fetch posts
│   │   └── follows.py  # Follow/unfollow users
│   └── router.py       # Main API router
│
├── main.py              # App entry point
└── __init__.py

🔐 Authentication

  • Uses JWT (JSON Web Tokens)
  • Tokens are issued on successful login
  • Protected routes require a valid Authorization: Bearer <token> header

🧑 User Features

  • User registration & login
  • Fetch user profile
  • Follow & unfollow users
  • Get followers and following lists
  • is_following flag returned where applicable

📝 Post Features

  • Create posts

  • Fetch posts feed

  • Like & unlike posts

  • Post response includes:

    • likes_count
    • is_liked
    • created_at

Example Post Response:

{
  "id": 18,
  "content": "Hello world",
  "author": {
    "id": 3,
    "username": "anab20"
  },
  "likes_count": 5,
  "is_liked": true,
  "created_at": "2026-01-02T00:17:35"
}

🗄️ Database

  • ORM‑based schema design
  • Relationships handled via foreign keys
  • Supports easy migration to PostgreSQL for production

⚙️ Environment Variables

Create a .env file in the root directory:

DATABASE_URL=sqlite:///./app.db
SECRET_KEY=your_secret_key
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=60

▶️ Running the Project

# Install dependencies
pip install -r requirements.txt

# Run the server
uvicorn app.main:app --reload

Server will be available at:

http://127.0.0.1:8000

📚 API Documentation

FastAPI provides interactive API docs out of the box:

  • Swagger UI: http://127.0.0.1:8000/docs
  • ReDoc: http://127.0.0.1:8000/redoc

🧪 Testing (Optional)

  • Pytest compatible structure
  • Repository‑level testing encouraged
  • Dependency overrides supported by FastAPI

🛠️ Best Practices Followed

  • Clean architecture
  • Repository pattern
  • Proper error handling
  • Typed schemas
  • Scalable & maintainable structure

About

Backend API – FastAPI This repository contains the backend service for the application, built using Python FastAPI. It provides a clean, scalable REST API for user authentication, profiles, posts, likes, follows, and related social features. The backend is designed following professional backend practices, focusing on modular architecture, clear s

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages