# phuoctmse/twitter-api architecture

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

- Interactive diagram: https://gitdiagram.com/phuoctmse/twitter-api
- Repository: https://github.com/phuoctmse/twitter-api

## Overview

##### Main Components

A. API Layer
- Entry point (src/index.ts)
- Routes layer (src/routes)
- Controllers layer (src/controllers)
- Middleware layer (src/middlewares)

B. Business Logic Layer
- Services layer (src/services)
- Utils layer (src/utils)

C. Data Layer
- Database service (MongoDB)
- Schemas (src/models/schemas)
- Data models (src/models)

D. Security Components
- Authentication (JWT handling - src/utils/jwt.ts)
- Encryption (src/utils/crypto.ts)

- [Client Applications]
- ↓
- [API Gateway Layer]
- ↓
- [Business Logic Layer]
- ↓
- [Data Access Layer]
- ↓
- [Database Layer]

##### Component Relationships to Highlight

- How requests flow from routes → controllers → services → database
- Authentication flow using JWT tokens
- Demonstrate error handling pathways
- Relationship between schemas and the database

##### Detailed Component Breakdown

API Gateway Layer:
**[Routes (users.routes.ts)] → [Controllers (users.controllers.ts)]**
- ↓
**[Middlewares (errors.middlewares.ts, users.middlewares.ts)]**

Business Logic Layer:
**[Services (users.services.ts)]**
- ↓
**[Utils (crypto.ts, jwt.ts, validation.ts)]**

Data Layer:
**[Schemas (User.schema.ts, RefreshToken.schema.ts)]**
- ↓
**[Database Service (database.services.ts)]**
- ↓
**[MongoDB Database]**

##### Implementation Details

- Environment variable dependencies
- Security middleware placement
- Validation checkpoints
- Error handling paths

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Client
        CA[Client Applications]
    end

    subgraph API_Gateway
        EP["Entry Point"]:::entry
        RT["Routes Layer"]:::routes
        CT["Controllers Layer"]:::controllers
        MW["Middleware Layer"]:::middleware
    end

    subgraph Business_Logic
        SV["Services Layer"]:::services
        UT["Utils Layer"]:::utils
        
        subgraph Utils
            JT["JWT Handler"]:::security
            CR["Crypto"]:::security
            VL["Validation"]:::utils
        end
    end

    subgraph Data_Layer
        DS["Database Service"]:::database
        SC["Schemas"]:::data
        DM["Data Models"]:::data
    end

    subgraph External
        DB[(MongoDB)]:::external
    end

    %% Connections
    CA --> EP
    EP --> RT
    RT --> CT
    CT --> MW
    MW --> SV
    SV --> UT
    SV --> DS
    DS --> SC
    SC --> DB
    DM --> SC

    %% Click events
    click EP "https://github.com/phuoctmse/Twitter-Api/blob/master/src/index.ts"
    click RT "https://github.com/phuoctmse/Twitter-Api/blob/master/src/routes/users.routes.ts"
    click CT "https://github.com/phuoctmse/Twitter-Api/blob/master/src/controllers/users.controllers.ts"
    click MW "https://github.com/phuoctmse/Twitter-Api/tree/master/src/middlewares"
    click SV "https://github.com/phuoctmse/Twitter-Api/blob/master/src/services/users.services.ts"
    click DS "https://github.com/phuoctmse/Twitter-Api/blob/master/src/services/database.services.ts"
    click SC "https://github.com/phuoctmse/Twitter-Api/tree/master/src/models/schemas"
    click DM "https://github.com/phuoctmse/Twitter-Api/blob/master/src/models/requests/User.requests.ts"
    click JT "https://github.com/phuoctmse/Twitter-Api/blob/master/src/utils/jwt.ts"
    click CR "https://github.com/phuoctmse/Twitter-Api/blob/master/src/utils/crypto.ts"
    click VL "https://github.com/phuoctmse/Twitter-Api/blob/master/src/utils/validation.ts"

    %% Styles
    classDef entry fill:#87CEEB
    classDef routes fill:#4169E1
    classDef controllers fill:#1E90FF
    classDef middleware fill:#00BFFF
    classDef services fill:#98FB98
    classDef utils fill:#90EE90
    classDef security fill:#FF6B6B
    classDef data fill:#FFD700
    classDef database fill:#DDA0DD
    classDef external fill:#A9A9A9

    %% Legend
    subgraph Legend
        L1[API Components]:::routes
        L2[Business Logic]:::services
        L3[Data Components]:::data
        L4[Security]:::security
        L5[External Systems]:::external
    end
```
