Skip to content

Latest commit

Β 

History

45 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌐 Social Media API πŸš€πŸ’¬

Social Media API is a powerful RESTful API for managing user profiles, posts, likes, comments, and subscriptions on a social media platform. This API allows the integration of core social platform features such as registration, logout, creating and editing posts, likes, comments, blocking users, and more.

πŸ“Œ Key Features πŸ”₯πŸ’₯

  • JWT Authentication πŸ”‘πŸ”’ β€” secure access via tokens.
  • Role-Based Access Control (RBAC) πŸ‘₯πŸ›‘οΈβš™οΈ β€” detailed access control for resources.
  • Filtration & Search πŸ”πŸ”ŽπŸ’¬ β€” search users by username and filter posts by hashtags.
  • Profile Management πŸ§‘β€πŸ’»πŸ“‹βœ¨ β€” view and edit profiles, set privacy preferences.
  • Post Management πŸ“„βœοΈπŸ“Έ β€” create, edit, view posts, like and comment.
  • Subscriptions & Blocking πŸš«πŸ”’πŸ‘€ β€” follow, unfollow, and block users.
  • Celery for Background Tasks β³βš™οΈπŸ”§ β€” process background tasks to optimize API performance.
  • Redis πŸ§ βš‘πŸ’¨ β€” caching and task queue management using Redis in Celery.
  • Flower 🌸🌺🌼 β€” monitor Celery tasks through the Flower web interface.

πŸ“¦ Installation & Setup πŸ› οΈ

1. Clone the repository

πŸ“‚πŸ’» Clone the repository to your local machine.

git clone https://github.com/your-username/social-media-api.git
cd social-media-api

2. Create a virtual environment

πŸŒ±πŸ’» Set up a virtual environment for dependency management:

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

3. Install dependencies

πŸ“₯πŸ“¦ Install all required dependencies from the requirements.txt file:

pip install -r requirements.txt

4. Set up environment variables

πŸŒπŸ”‘ Create and configure the .env file for environment settings:

# Django
DJANGO_SECRET_KEY=<django-insecure-6_e1bm0dh#zcn2m9@@*_z@9r-*m0h2i+)&oxh!^9m3bt3w2=ha>
DJANGO_SETTINGS_MODULE=<config.settings.dev>

# DB
POSTGRES_PASSWORD=<password>
POSTGRES_USER=<user>
POSTGRES_DB=<the_best_db>
POSTGRES_HOST=<localhost>
POSTGRES_PORT=<5432>
PGDATA=</var/lib/postgresql/data>

# Celery settings
CELERY_BROKER_URL = "redis://localhost:6379/0"
CELERY_RESULT_BACKEND = "redis://localhost:6379/0"
CELERY_TIMEZONE = "Europe/Kyiv"
CELERY_TASK_TRACK_STARTED = True
CELERY_TASK_TIME_LIMIT = 30 * 60

5. Run the server

πŸš€πŸŒ Start the development server to run the API.

python manage.py runserver

6. Access the API

Visit http://127.0.0.1:8000 to test the API.

🐳 Deployment via Docker πŸ“¦

1. Start using Docker Compose

πŸ³πŸ”§ Run the application using Docker Compose to spin up all services in containers.

docker-compose up --build

πŸ“‚πŸ’‘ Define all necessary services (PostgreSQL, Redis, Celery, etc.) in the docker-compose.yaml.

2. Dockerfile

FROM python:3.10.8

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

WORKDIR /code

COPY requirements.txt /code/

RUN pip install -r requirements.txt

COPY . /code/

3. docker-compose.yaml

services:
  social_media_api:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    env_file:
      - .env

  web:
    build: .
    command: >
      sh -c "python manage.py wait_for_db &&
           python manage.py migrate && 
           python manage.py runserver 0.0.0.0:8000"
    volumes:
      - ./:/code
      - media:/code/media
    ports:
      - "8000:8000"
    env_file:
      - .env
    depends_on:
      - social_media_api

  redis:
    image: "redis:alpine"

  celery:
    build:
      context: .
      dockerfile: Dockerfile
    command: "celery -A social_media_api worker -l info"
    depends_on:
      - web
      - redis
      - social_media_api
    restart: on-failure
    env_file:
      - .env

  celery-beat:
    build:
      context: .
      dockerfile: Dockerfile
    command: >
      sh -c "python manage.py wait_for_db &&
             celery -A social_media_api beat -l INFO --scheduler django_celery_beat.schedulers:DatabaseScheduler"
    depends_on:
      - web
      - redis
      - social_media_api
    restart: on-failure
    env_file:
      - .env

  flower:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "5555:5555"
    command: "celery -A social_media_api flower --address=0.0.0.0"
    depends_on:
      - celery
    env_file:
      - .env

volumes:
  media:

πŸ“œπŸ”₯ API Documentation (Swagger)

πŸ“Œ Swagger UI: http://127.0.0.1:8000/api/v1/schema/swagger/
swagger.png

πŸ“ŠπŸ—ƒοΈ Database Schema

db.png

🌸 Flower & Celery

flower.png

🌍 Main API Routes πŸŒπŸš€

User URLs - πŸ§‘β€πŸ’»πŸ‘€ User Management

  • POST /api/user/: Create a new user.
  • POST /api/user/token/: Get access token (JWT).
  • POST /api/user/token/refresh/: Refresh the access token.
  • POST /api/user/token/verify/: Verify the access token.
  • GET /api/user/me/: View your own profile.

Post & Interaction URLs - βœοΈπŸ“„ Manage Posts and Interactions

  • GET /api/posts/: List all posts.
  • POST /api/posts/: Create a new post.
  • GET /api/posts/<id>/: View details of a specific post.
  • POST /api/posts/<id>/like/: Like a post.
  • POST /api/posts/<id>/comment/: Comment on a post.

Profile URLs - πŸ§‘β€πŸ’»πŸ‘₯ Profile & Privacy Management

  • GET /api/profile/: View and edit your profile details.
  • GET /api/profile/<username>/: View another user's profile.

πŸ§‘β€πŸ’» Core Functionalities of the API βš™οΈπŸ”§

1. Authentication & Security πŸ”’πŸ”‘

  • JWT Authentication: Secure token-based authentication for API access.
  • Logout: Token invalidation for logging out users.

2. User Profile Management πŸ§‘β€πŸ’»πŸ–ΌοΈ

  • View Profile: Access your own or other users' profiles.
  • Edit Profile: Modify your personal information.
  • Profile Privacy: Set your account as public or private.

3. Post Management πŸ“„πŸ“Έ

  • Create Posts: Users can create posts with text and images.
  • View Posts: Posts are publicly viewable, with private access control for editing.
  • Like & Comment: Engage with posts by liking or commenting.

4. Subscriptions & Blocking πŸš«πŸ”’

  • Follow/Unfollow: Manage who you follow/unfollow.
  • Block Users: Block other users and prevent them from interacting with you.

5. Likes & Comments β€οΈπŸ’¬

  • Add Likes: Like posts to express approval.
  • Remove Likes: Remove your like from a post.
  • Write Comments: Leave comments on posts to engage in conversations.

6. Background Tasks & Optimization ⏳⚑

  • Celery: Manage background tasks (e.g., notifications, media processing).
  • Redis: Cache data and manage task queues with Redis for efficiency.
  • Flower: Use Flower for monitoring background task progress.

πŸ‘€ Author

Vladyslav Rymarchuk
GitHub | LinkedIn

About

This project is a Social Media API built using Django Rest Framework (DRF). It provides core features like user authentication, profiles, posts, followers, likes, comments, and user blocking. The API is designed to handle user interactions and content management, offering a robust foundation for building scalable social networking platforms

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages