# chirag-and-dheeraj/video-streaming-server architecture

> Architecture diagram of the GitHub repository chirag-and-dheeraj/video-streaming-server, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/chirag-and-dheeraj/video-streaming-server
- Repository: https://github.com/chirag-and-dheeraj/video-streaming-server

## Overview

##### Project type and purpose
- This is a full-stack, on-demand video streaming server called “Dekho.”
- The backend is a monolithic Go application that implements HLS (.m3u8 + .ts chunk) streaming, user authentication, video upload/metadata storage, and serves both API endpoints and static assets.
- The frontend is plain HTML/CSS/JS using HLS.js to play streams.

##### File structure
- Top-level directories:
- client/           → pure HTML pages (index, login, register, upload, watch, list)
- static/           → CSS, JS bundles, logo, videoList components
- controllers/      → HTTP handlers for users and videos
- services/         → business logic (upload, transcoding, HLS manifest generation)
- repositories/     → database access for user and video entities
- database/         → SQL migrations and database initialization
- middleware/       → auth, logging, CORS, rate-limiting
- config/           → config loader (env, Docker, Appwrite keys)
- utils/, types/    → shared utilities and type definitions
- main.go           → application bootstrap (router, middleware, DI)
- Dockerfile, docker-compose.yml, Makefile → containerization and local/dev scripts

##### Extract additional insights from README
- Backend tech: Go 1.23, PostgreSQL 16.3, Appwrite Storage bucket, FFMPEG (for chunking), golang-migrate
- Frontend tech: HTML/CSS/JS, HLS.js
- Runtime: served on localhost:8000 (or in Docker) combining API + static server
- External dependencies: Appwrite REST API for object storage, PostgreSQL for metadata

##### Main components and interactions
- Browser / Client UI
- Static File Server (serving HTML, CSS, JS assets)
- Go Application Server
- HTTP Router & Middleware
- Controllers (UserController, VideoController)
- Services (UploadService, HLSService)
- Repositories (UserRepo, VideoRepo)
- FFMPEG Processor (invoked as CLI subprocess)
- PostgreSQL Database
- Appwrite Storage Bucket (for storing raw uploads, TS chunks, thumbnails)
- HLS.js Player (in the browser, embedded in watch.html)

1. Browser → Static File Server: GET HTML/CSS/JS
2. Browser → Go Server API:
- POST /register, /login → Auth → JWT/session
- GET /videos   → fetch metadata (JSON) from VideoRepo → DB
- POST /upload  → multipart upload → Go Server streams file to Appwrite Storage
3. Go Server → FFMPEG: invoke CLI to transcode & segment raw video blob into .ts chunks + .m3u8 manifest
4. FFMPEG → Appwrite Storage: upload TS chunk files and manifest
5. Browser (HLS.js) → Go Server: GET /videos/{id}/playlist.m3u8 and .ts segments → streamed over HTTP
6. Go Server → PostgreSQL: read/write user & video metadata using SQL migrations and Repositories

c. Architectural patterns & design principles:
- Layered MVC (controllers → services → repositories → database)
- Separation of concerns: video ingestion, processing, storage, metadata, playback
- Externalize configuration via env files and config module
- Containerized deployment via Docker + docker-compose
- Use of CI/CD-style migrations (golang-migrate)
- Single binary Go server serving both API and static assets

- Go net/http, Gorilla Mux (or native router)
- ffmpeg CLI for segmentation
- PostgreSQL
- Appwrite Storage REST API
- HTML/CSS/Vanilla JS + HLS.js

## Diagram (Mermaid)

```mermaid
graph TD
    %% Client Side
    subgraph "Client Side"
        direction TB
        Browser["Browser / Client UI"]:::client
        HLS["HLS.js Player"]:::client
    end

    %% Static Server
    StaticServer["Static File Server"]:::compute

    %% Go Application Container
    subgraph "Go Application (Container)"
        direction TB
        Main["main.go"]:::compute
        Router["HTTP Router & Middleware"]:::compute
        Config["Config Loader"]:::compute
        UCtrl["UserController"]:::compute
        VCtrl["VideoController"]:::compute
        Services["UploadService / HLSService"]:::compute
        URepo["User Repository"]:::compute
        VRepo["Video Repository"]:::compute
        Types["Shared Types"]:::compute
        Utils["Utility Functions"]:::compute
        DBInit["Database Init & Migrations"]:::compute
    end

    %% External Services & Infrastructure
    FFMPEG["FFMPEG CLI"]:::compute
    Appwrite["Appwrite Storage Bucket"]:::datastore
    Postgres["PostgreSQL Database"]:::datastore

    subgraph "Infrastructure & Scripts"
        direction TB
        DockerfileNode["Dockerfile"]:::compute
        Compose["docker-compose.yml"]:::compute
        Make["Makefile"]:::compute
        Scripts["Dev/Prod Scripts"]:::compute
    end

    %% Data Flows
    Browser -->|GET HTML/CSS/JS| StaticServer
    Browser -->|POST /register, /login| Main
    Browser -->|GET /videos, POST /upload| Main
    HLS -->|GET /playlist & .ts segments| Main

    StaticServer --> Main

    Main --> Router
    Main --> Config
    Main --> DBInit
    Router --> UCtrl
    Router --> VCtrl

    UCtrl --> URepo
    VCtrl --> VRepo
    VCtrl --> Services
    Services --> URepo
    Services --> VRepo
    Services --> FFMPEG
    Services --> Appwrite

    URepo --> Postgres
    VRepo --> Postgres

    Main --> Types
    Main --> Utils

    %% External processing
    FFMPEG --> Appwrite

    %% Infrastructure relations
    Main -.-> DockerfileNode
    Main -.-> Compose
    Main -.-> Make
    Main -.-> Scripts
    Postgres -.-> Compose

    %% Click Events
    click Browser "https://github.com/chirag-and-dheeraj/video-streaming-server/tree/main/client/"
    click HLS "https://github.com/chirag-and-dheeraj/video-streaming-server/blob/main/client/watch.html"
    click StaticServer "https://github.com/chirag-and-dheeraj/video-streaming-server/tree/main/static/"
    click Main "https://github.com/chirag-and-dheeraj/video-streaming-server/blob/main/main.go"
    click Router "https://github.com/chirag-and-dheeraj/video-streaming-server/blob/main/middleware/middleware.go"
    click Config "https://github.com/chirag-and-dheeraj/video-streaming-server/blob/main/config/config.go"
    click UCtrl "https://github.com/chirag-and-dheeraj/video-streaming-server/blob/main/controllers/user.go"
    click VCtrl "https://github.com/chirag-and-dheeraj/video-streaming-server/blob/main/controllers/video.go"
    click Services "https://github.com/chirag-and-dheeraj/video-streaming-server/blob/main/services/user.go"
    click URepo "https://github.com/chirag-and-dheeraj/video-streaming-server/blob/main/repositories/user.go"
    click VRepo "https://github.com/chirag-and-dheeraj/video-streaming-server/blob/main/repositories/video.go"
    click DBInit "https://github.com/chirag-and-dheeraj/video-streaming-server/blob/main/database/database.go"
    click DBInit "https://github.com/chirag-and-dheeraj/video-streaming-server/tree/main/database/migrations/"
    click Types "https://github.com/chirag-and-dheeraj/video-streaming-server/blob/main/types/types.go"
    click Utils "https://github.com/chirag-and-dheeraj/video-streaming-server/blob/main/utils/utils.go"
    click DockerfileNode "https://github.com/chirag-and-dheeraj/video-streaming-server/tree/main/Dockerfile"
    click Compose "https://github.com/chirag-and-dheeraj/video-streaming-server/blob/main/docker-compose.yml"
    click Make "https://github.com/chirag-and-dheeraj/video-streaming-server/tree/main/Makefile"
    click Scripts "https://github.com/chirag-and-dheeraj/video-streaming-server/blob/main/scripts/start_dev.sh"

    %% Styles
    classDef datastore fill:#ADD8E6,stroke:#000,color:#000
    classDef compute fill:#8FBC8F,stroke:#000,color:#000
    classDef client fill:#FFD700,stroke:#000,color:#000
```
