Skip to content

jagapathi20/FASTAPI-Social-Media-API

Repository files navigation

FastAPI Social Media API

Python FastAPI PostgreSQL License: MIT Tests

A full-featured social media backend API built with FastAPI, PostgreSQL, SQLAlchemy, and JWT authentication.
It demonstrates clean architecture, modular routing, Alembic migrations, password hashing, and robust testing using Pytest.


Project Preview

Add API demo screenshot or OpenAPI docs preview here.

FastAPI Swagger UI


Features

User Authentication

  • JWT-based login system
  • Password hashing with passlib
  • Token verification & protected routes

Post Management

  • Create, Read, Update, Delete (CRUD) operations
  • Ownership checks (only authors can modify/delete)
  • Pagination & search support

Voting System

  • Upvote / downvote functionality
  • Prevents duplicate votes

Database & ORM

  • PostgreSQL + SQLAlchemy ORM
  • Alembic for migrations
  • Pydantic models for validation

Testing

  • Pytest with isolated test database
  • Fixtures for posts, users, and auth
  • CI-ready test configuration

Project Structure

app/
│
├── __init__.py
├── main.py                # FastAPI entry point
├── models.py              # SQLAlchemy models
├── schemas.py             # Pydantic schemas
├── utils.py               # Password hashing utilities
├── oauth2.py              # JWT creation & validation
├── config.py              # Environment configuration
├── database.py            # SQLAlchemy engine & session
│
├── routers/               # API routes
│   ├── post.py
│   ├── user.py
│   ├── auth.py
│   └── vote.py
│
├── tests/                 # Pytest suite
│   ├── conftest.py
│   ├── test_users.py
│   ├── test_posts.py
│   ├── test_votes.py
│   └── ...
│
├── alembic/               # Migration scripts
├── alembic.ini            # Alembic config
├── requirements.txt
└── README.md

Installation

Clone the repository

git clone https://github.com/<your-username>/fastapi-social-media.git
cd fastapi-social-media

Create a virtual environment

python -m venv venv
source venv/bin/activate     # Linux / macOS
venv\Scripts\activate        # Windows

Install dependencies

pip install -r requirements.txt

Set up environment variables

Create a .env file in the project root:

database_host_name=localhost
database_port=5432
database_password=yourpassword
database_name=fastapi
database_username=postgres
secret_key=your_secret_key
algorithm=HS256
access_token_expire_minutes=30

Run Alembic migrations

alembic upgrade head

Start the development server

uvicorn app.main:app --reload

Server runs at http://127.0.0.1:8000

Swagger docs at http://127.0.0.1:8000/docs


Running Tests

pytest -v

API Endpoints Overview

Authentication

Method Endpoint Description
POST /login User login, returns access token

Users

Method Endpoint Description
POST /users/ Create a new user
GET /users/{id} Retrieve a user by ID

Posts

Method Endpoint Description
GET /posts/ List all posts
GET /posts/{id} Retrieve a single post
POST /posts/ Create a new post
PUT /posts/{id} Update an existing post
DELETE /posts/{id} Delete a post

Votes

Method Endpoint Description
POST /vote/ Cast or remove a vote on a post

Docker (Optional)

Development mode

docker compose -f docker-compose-dev.yml up --build

Production mode

docker compose -f docker-compose-prod.yml up --build -d

Tech Stack

Layer Technology
Framework FastAPI
Database PostgreSQL
ORM SQLAlchemy
Migrations Alembic
Authentication JWT via python-jose
Testing Pytest
Containerization Docker, Docker Compose

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors