# chandi977/youtube-backend architecture

> Architecture diagram of the GitHub repository chandi977/youtube-backend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-10-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/chandi977/youtube-backend
- Repository: https://github.com/chandi977/youtube-backend

## Overview

##### Project type and purpose
- This is a Node.js/Express backend for a video-streaming platform (“VideoTube”), using MongoDB for storage, Redis (Upstash) for caching/queues, Cloudinary for media hosting, and BullMQ workers for background jobs. It exposes REST APIs and WebSocket endpoints, and uses Docker (without Nginx per your instruction).

##### File structure
- Top-level: Dockerfile, docker-compose.yml, package.json, README, src/, public/
- src/app.js and src/index.js bootstrap the Express app and socket server.
- src/controllers and src/routes implement REST endpoints following an MVC-style division.
- src/middleware holds authentication, error-handling, file-upload, and security middleware.
- src/models defines Mongoose schemas for Users, Videos, Comments, etc.
- src/queues and src/workers implement BullMQ job producers (e.g., video processing, syncLikes) and consumers in separate worker processes.
- src/socket/socketHandler.js ties in WebSocket logic for live streaming/chat.
- src/config holds environment and third-party client (Redis, Passport) setup.
- src/utils holds shared helpers (ApiError/Response, asyncHandler, cloudinary wrapper, video processing).

##### README for additional insights
- Confirms tech stack (Express, MongoDB, Mongoose, JWT, Cloudinary, Redis/BullMQ).
- Describes features: User auth (JWT), video CRUD, pagination, upload → Cloudinary, live streaming.
- Mentions Docker Compose orchestration (app + RTMP server under Nginx normally, but you’re omitting Nginx).

## Diagram (Mermaid)

```mermaid
flowchart TB
    Client["Client (Web/Mobile)"]:::frontend

    subgraph "Express API Container"
        API["Express API Server"]:::api
        AuthMW["Auth Middleware"]:::api
        ErrorMW["Error Handler Middleware"]:::api
        MulterMW["File Upload Middleware"]:::api
        SecurityMW["Security Middleware"]:::api
        subgraph "Controllers"
            UserCtrl["User Controllers"]:::api
            VideoCtrl["Video Controllers"]:::api
            CommentCtrl["Comment Controllers"]:::api
        end
        subgraph "Routes"
            UserRoutes["User Routes"]:::api
            VideoRoutes["Video Routes"]:::api
            CommentRoutes["Comment Routes"]:::api
        end
        Models["Mongoose Models"]:::db
        Config["Configuration"]:::infra
        CloudinaryUtil["Cloudinary Integration"]:::external
        UpstashUtil["Redis/Upstash Utils"]:::cache
        Producer["BullMQ Producer"]:::api
        utils["General Utils"]:::infra
    end

    WS["WebSocket Server"]:::api

    Mongo["MongoDB"]:::db
    Redis["Redis (Cache & Broker)"]:::cache
    subgraph "Workers Container"
        VideoWorker["Video Processing Worker"]:::workers
        SyncWorker["SyncLikes Worker"]:::workers
    end
    Cloudinary["Cloudinary CDN"]:::external
    RTMP["RTMP Ingest Server"]:::infra

    Client -->|HTTP REST| API
    Client -->|WebSocket| WS
    API -->|queries| Mongo
    API -->|caches/queues| Redis
    API -->|uploads/deletes| Cloudinary
    API -->|publishes jobs| Redis
    API -->|streams ingest| RTMP
    Redis -->|jobs| VideoWorker
    Redis -->|jobs| SyncWorker
    VideoWorker -->|updates| Mongo
    VideoWorker -->|stores media| Cloudinary
    SyncWorker -->|updates| Mongo

    %% Click Events
    click API "https://github.com/chandi977/youtube-backend/blob/master/src/index.js"
    click API "https://github.com/chandi977/youtube-backend/blob/master/src/app.js"
    click UserCtrl "https://github.com/chandi977/youtube-backend/tree/master/src/controllers"
    click VideoCtrl "https://github.com/chandi977/youtube-backend/blob/master/src/controllers/video.controller.js"
    click CommentCtrl "https://github.com/chandi977/youtube-backend/blob/master/src/controllers/comment.controller.js"
    click UserRoutes "https://github.com/chandi977/youtube-backend/blob/master/src/routes/video.routes.js"
    click VideoRoutes "https://github.com/chandi977/youtube-backend/blob/master/src/routes/video.routes.js"
    click CommentRoutes "https://github.com/chandi977/youtube-backend/blob/master/src/routes/comment.routes.js"
    click AuthMW "https://github.com/chandi977/youtube-backend/blob/master/src/middlewares/auth.middleware.js"
    click ErrorMW "https://github.com/chandi977/youtube-backend/blob/master/src/middlewares/errorHandler.middleware.js"
    click MulterMW "https://github.com/chandi977/youtube-backend/blob/master/src/middlewares/multer.middleware.js"
    click SecurityMW "https://github.com/chandi977/youtube-backend/blob/master/src/middlewares/security.middleware.js"
    click Models "https://github.com/chandi977/youtube-backend/tree/master/src/models"
    click WS "https://github.com/chandi977/youtube-backend/blob/master/src/socket/socketHandler.js"
    click Config "https://github.com/chandi977/youtube-backend/blob/master/src/config/index.js"
    click Config "https://github.com/chandi977/youtube-backend/blob/master/src/config/redis.js"
    click Config "https://github.com/chandi977/youtube-backend/blob/master/src/config/passport.js"
    click CloudinaryUtil "https://github.com/chandi977/youtube-backend/blob/master/src/utils/cloudinary.js"
    click UpstashUtil "https://github.com/chandi977/youtube-backend/blob/master/src/utils/upstash.js"
    click Producer "https://github.com/chandi977/youtube-backend/blob/master/src/queues/videoQueue.js"
    click VideoWorker "https://github.com/chandi977/youtube-backend/blob/master/src/workers/videoWorker.js"
    click SyncWorker "https://github.com/chandi977/youtube-backend/blob/master/src/workers/syncLikes.js"
    click utils "https://github.com/chandi977/youtube-backend/blob/master/src/utils/ApiError.js"
    click utils "https://github.com/chandi977/youtube-backend/blob/master/src/utils/ApiResponse.js"
    click utils "https://github.com/chandi977/youtube-backend/blob/master/src/utils/asyncHandler.js"
    click utils "https://github.com/chandi977/youtube-backend/blob/master/src/utils/logger.js"
    click Dockerfile "https://github.com/chandi977/youtube-backend/tree/master/Dockerfile"
    click Dockerfile "https://github.com/chandi977/youtube-backend/blob/master/docker-compose.yml"
    click Temp "https://github.com/chandi977/youtube-backend/blob/master/public/temp/.gitkeep"

    %% Styles
    classDef frontend fill:#9fdfbf,stroke:#333,stroke-width:1px
    classDef api fill:#f9f,stroke:#333,stroke-width:1px
    classDef db fill:#bbf,stroke:#333,stroke-width:1px
    classDef cache fill:#fbf,stroke:#333,stroke-width:1px
    classDef workers fill:#fbb,stroke:#333,stroke-width:1px
    classDef external fill:#ffb,stroke:#333,stroke-width:1px
    classDef infra fill:#bfb,stroke:#333,stroke-width:1px
```
