# zlingapp/server architecture

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

- Interactive diagram: https://gitdiagram.com/zlingapp/server
- Repository: https://github.com/zlingapp/server

## Overview

##### Main Components

a) Client Interface Layer:
- HTTP Server (actix-web)
- WebSocket Server (actix-ws)
- SSL/TLS termination point (when enabled)

b) Authentication & Authorization:
- Token Management System
- Access Token Handler
- Refresh Token Storage

c) Core Services:
- User Management
- Guild (Server) Management
- Channel Management
- Friend System
- Messaging System
- Voice Chat System (WebRTC/mediasoup)
- Media Storage
- Real-time Event System

d) Data Storage:
- PostgreSQL Database
- File Storage System

##### Component Relationships

**[Clients] → [HTTP/WebSocket Layer]**
- ↓
**[Authentication Layer] → [Token Management]**
- ↓
- [Core Services Layer]
- ↙     ↓     ↓     ↘
- [Voice] [Messages] [Media] [Users/Guilds]
- ↘     ↓     ↓     ↙
- [Database & Storage Layer]

##### Specific Implementation Details

a) HTTP/WebSocket Layer:
- Actix-web handling HTTP requests
- WebSocket connections for real-time events
- SSL/TLS termination point if enabled

b) Voice System:
- Mediasoup SFU components
- WebRTC ports (10000-10003)
- UDP/TCP transport options

c) Database Layer:
- PostgreSQL with connection pool
- Migration system
- Separate media storage system

##### Important Flows to Highlight

a) Authentication Flow:
**Client → Login Request → Token Issuing → Access/Refresh Tokens**

b) Real-time Communication:
**Client → WebSocket → PubSub System → Event Distribution**

c) Voice Chat Flow:
**Client → WebRTC Negotiation → mediasoup → Peer Distribution**

##### Additional Elements

- Scaling points (NUM_WEB_WORKERS)
- Security boundaries
- Public/private network boundaries

##### Technical Details to Note

- Database migrations system
- Token signing mechanism
- Media file storage path
- WebRTC ports and configurations
- Connection pooling

- Nested components
- Clear labeling
- Proper grouping of related components

- Component boundaries
- Configuration points
- Scaling indicators

This will create a comprehensive yet understandable representation of Zling's architecture that highlights its key features and components while maintaining technical accuracy.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Client Layer"
        CL[Client Applications]
    end

    subgraph "Interface Layer"
        WS["WebSocket Server"]:::realtime
        HTTP["HTTP Server"]:::interface
        SSL["SSL/TLS Termination"]:::security
    end

    subgraph "Authentication Layer"
        Auth["Authentication System"]:::security
        TokenMgmt["Token Management"]:::security
        RefreshStore["Refresh Token Storage"]:::security
    end

    subgraph "Core Services"
        UserMgmt["User Management"]:::core
        GuildMgmt["Guild Management"]:::core
        ChannelMgmt["Channel Management"]:::core
        FriendSys["Friend System"]:::core
        MsgSys["Messaging System"]:::core
        VoiceSys["Voice Chat System"]:::realtime
        MediaStore["Media Storage"]:::core
        RTEvents["Real-time Events"]:::realtime
        InviteSys["Invite System"]:::core
        BotSys["Bot System"]:::core
        UserSettings["User Settings"]:::core
    end

    subgraph "Storage Layer"
        DB[(PostgreSQL)]:::storage
        FileStore[(File Storage)]:::storage
    end

    %% Connections
    CL --> HTTP
    CL --> WS
    HTTP & WS --> SSL
    SSL --> Auth
    Auth --> TokenMgmt
    TokenMgmt --> RefreshStore

    Auth --> UserMgmt
    Auth --> GuildMgmt
    Auth --> ChannelMgmt
    Auth --> FriendSys
    Auth --> MsgSys
    Auth --> VoiceSys
    Auth --> MediaStore
    Auth --> RTEvents
    Auth --> InviteSys
    Auth --> BotSys
    Auth --> UserSettings

    UserMgmt & GuildMgmt & ChannelMgmt & FriendSys & MsgSys & MediaStore & InviteSys & BotSys & UserSettings --> DB
    MediaStore --> FileStore
    VoiceSys --> RTEvents
    
    %% Click Events
    click Auth "https://github.com/zlingapp/server/tree/master/src/auth/"
    click TokenMgmt "https://github.com/zlingapp/server/blob/master/src/auth/token.rs"
    click GuildMgmt "https://github.com/zlingapp/server/tree/master/src/guilds/"
    click ChannelMgmt "https://github.com/zlingapp/server/tree/master/src/channels/"
    click FriendSys "https://github.com/zlingapp/server/tree/master/src/friends/"
    click MsgSys "https://github.com/zlingapp/server/tree/master/src/messaging/"
    click VoiceSys "https://github.com/zlingapp/server/tree/master/src/voice/"
    click MediaStore "https://github.com/zlingapp/server/tree/master/src/media/"
    click RTEvents "https://github.com/zlingapp/server/tree/master/src/realtime/"
    click DB "https://github.com/zlingapp/server/blob/master/src/db.rs"
    click InviteSys "https://github.com/zlingapp/server/tree/master/src/invites/"
    click BotSys "https://github.com/zlingapp/server/tree/master/src/bot/"
    click UserSettings "https://github.com/zlingapp/server/tree/master/src/settings/"

    %% Styles
    classDef interface fill:#2374AB
    classDef security fill:#FFD700
    classDef core fill:#4CAF50
    classDef storage fill:#FF6B6B
    classDef realtime fill:#9C27B0

    %% Legend
    subgraph Legend
        L1[Interface Components]:::interface
        L2[Security Components]:::security
        L3[Core Services]:::core
        L4[Storage Systems]:::storage
        L5[Real-time Components]:::realtime
    end
```
