# kartik-212004/social-media architecture

> Architecture diagram of the GitHub repository kartik-212004/social-media, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kartik-212004/social-media
- Repository: https://github.com/kartik-212004/social-media

## Overview

##### Main Components
- Frontend Client: Represented by the Next.js pages and UI components. This includes the various pages (account, dashboard, profile, search, etc.) and client-side components (navbar, middle/bar sidebars, login/signup forms, dark/light theme components, etc.).
- API & Server: The backend functions are implemented as Next.js API routes—organized under the app/api directory. These endpoints handle authentication, posts (create, delete, like, upload), likes, user settings, search functionalities, and AWS S3 operations.
- Real-Time Communication: Include the WebSocket server module (app/server/socket.js) which manages real-time notifications, messaging, or updates.
- Database: A PostgreSQL instance managed through Prisma (prisma/schema.prisma and migrations) that stores user data, posts, likes, and other application state.
- Media Storage: AWS S3 integration for handling media uploads and retrieval.
- External Services & Authentication: NextAuth.js for secure user authentication, alongside integrations with supporting libraries and services noted in the README.

##### Relationships and Interactions
- The Client (user’s browser) communicates with the Next.js application which serves both the UI and performs server-side rendering (SSR/SSG).
- The UI layer makes HTTP calls to the API endpoints to manage user actions (signup, login, posting content, liking posts, etc.).
- API endpoints interact with the PostgreSQL database via Prisma for CRUD operations on user data and posts.
- When handling media, API endpoints offload upload and deletion operations to AWS S3.
- Real-time communications are managed by a separate service (WebSocket server) that pushes notifications or live updates back to the client.
- NextAuth.js hooks into the API layer to manage secure authentication flows.

##### Important Architectural Patterns
- Separation of Concerns: Clear division between presentation (client-side UI) and data/API logic (server-side API routes).
- Modular Routing: The API routes are logically organized by functionality (auth, posts, settings, search, AWS S3 endpoints) to keep functionality isolated.
- Server-Side Rendering (SSR) and Static Site Generation (SSG): Leveraging Next.js’s built-in capabilities for improved performance and SEO.
- Real-Time Updates: Utilizing a WebSocket server to provide immediate feedback for notifications and messages.
- Use of Environment Configuration and Containerization: With Docker, GitHub workflows, and environment settings (.env.example) to manage deployment and consistency.

- The separation of the Next.js frontend and backend/API routes is crucial for understanding the flow of data.

## Diagram (Mermaid)

```mermaid
graph TD
    subgraph "Frontend Layer"
        FE("Frontend (Next.js UI)\n(Next.js, TypeScript, Tailwind CSS)"):::frontend
    end

    subgraph "Backend Layer"
        BE("Backend API (Next.js API Routes)\n(API endpoints for posts, likes, settings, S3)"):::backend
        AUTH("Authentication (NextAuth.js)"):::auth
        BE -->|"includes"| AUTH
    end

    DB("Database (PostgreSQL via Prisma)"):::database
    MS("Media Storage (AWS S3)"):::media
    WS("Real-Time Service (WebSocket Server)"):::realtime

    FE -->|"API_Calls"| BE
    BE -->|"DB_Operations"| DB
    BE -->|"Media_Ops"| MS
    BE -->|"RealTime_Notifications"| WS
    WS -->|"Push_Updates"| FE

    click FE "https://github.com/kartik-212004/social-media/tree/main/app"
    click BE "https://github.com/kartik-212004/social-media/tree/main/app/api"
    click AUTH "https://github.com/kartik-212004/social-media/tree/main/app/api/auth"
    click WS "https://github.com/kartik-212004/social-media/blob/main/app/server/socket.js"
    click DB "https://github.com/kartik-212004/social-media/blob/main/prisma/schema.prisma"
    click MS "https://github.com/kartik-212004/social-media/tree/main/app/api/s3"

    classDef frontend fill:#aee9f5,stroke:#333,stroke-width:2px;
    classDef backend fill:#d7e9a1,stroke:#333,stroke-width:2px;
    classDef auth fill:#f3c6c7,stroke:#333,stroke-width:2px;
    classDef database fill:#f9e79f,stroke:#333,stroke-width:2px;
    classDef media fill:#fad7a0,stroke:#333,stroke-width:2px;
    classDef realtime fill:#c39bd3,stroke:#333,stroke-width:2px;
```
