# azarenkov/chat-app architecture

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

- Interactive diagram: https://gitdiagram.com/azarenkov/chat-app
- Repository: https://github.com/azarenkov/chat-app

## Overview

This project is a web-based chat application built in Rust using Actix Web. The application supports user registration, login with JWT authentication (with tokens valid for 1 hour), retrieval of chat history, and real-time chatting through WebSocket connections. MongoDB is used for persistent data storage, and Sentry is integrated for error logging. Docker (via docker-compose) and Nginx are also part of the ecosystem for deployment and reverse proxy management.

##### Main Components
- Client: Representing end-users (mobile or web clients) that perform HTTP requests (for registration, login, fetching chat history) and establish WebSocket connections for real-time messaging.
- HTTP API Server: The central server (built with Actix Web) that exposes endpoints for authentication, message handling, and WebSocket communication.
- Controllers: Clearly separated modules such as the auth_controller and message_controller that handle incoming HTTP requests and route them appropriately.
- Services: Business logic is abstracted in layers like auth_service, message_service, and JWT service (encapsulated in src/services and infrastructure/jwt). This layer processes the logic needed for authenticating users, sending messages, and managing JWT tokens.
- Repository/Data Access Layer: Modules in src/repositories (such as user_repository and message_repository) which interface with MongoDB. They encapsulate database operations.
- Infrastructure: Contains essential supportive components, including:
- Database Connector (src/infrastructure/db) for handling MongoDB connections.
- JWT Management (src/infrastructure/jwt) for token generation/verification.
- WebSocket Handling (src/infrastructure/ws) to manage real-time communication.
- App Setup (initialization routines and configuration management).
- External Services and Tools:
- MongoDB: The document-oriented database storing user information and chat history.
- Sentry: Integrated middleware for monitoring and logging errors.
- Nginx: Likely used as a reverse proxy/load balancer (as indicated by the badge in the README).
- Docker/Docker-Compose: Used for containerization and orchestrating services (as seen with docker-compose.yml and init-mongo.js).

##### Relationships and Interactions
- Clients communicate with the API server through HTTP/HTTPS for registration, login, and retrieving old messages.
- For authentication, the auth_controller handles user credentials, delegates to the auth_service, which in turn utilizes the JWT service and repository layer for user verification and token generation.
- Once authenticated, clients use the JWT token to establish a WebSocket connection (managed by the WebSocket infrastructure). The ws module processes incoming messages, which are then sent via the message_controller and further handled in the service and repository layers.
- Across these interactions, controllers call corresponding services; services interact with repositories to read/write from MongoDB.
- Middleware (Sentry) monitors HTTP requests and WebSocket activities, capturing errors and performance metrics.
- Docker and Nginx play roles in deployment and traffic management.

##### Architectural Patterns and Design Principles
- Layered Architecture: The separation of controllers, services, repositories, and infrastructure clearly indicates a layered approach, making the system modular and maintainable.
- Dependency Abstraction: Abstract service interfaces (e.g., auth_service_abstract, jwt_service_abstract) facilitate easier modifications or testing.
- Real-Time and HTTP Duality: Handling both stateless HTTP requests and persistent WebSocket connections highlights a dual-mode interaction pattern.
- Security: Incorporation of JWT tokens for session management and authentication provides a secure access layer.

## Diagram (Mermaid)

```mermaid
graph TD
  %% External Components
  Client["Client (Mobile/Web)"]:::external
  Nginx["Nginx (Reverse Proxy)"]:::external
  Docker["Docker Compose"]:::external
  Sentry["Sentry (Error Logging)"]:::external
  MongoDB["MongoDB"]:::database

  %% API Server Component
  API["HTTP API Server"]:::internal
  click API "https://github.com/azarenkov/chat-app/blob/main/src/main.rs"

  %% Subgraph: Controllers
  subgraph "Controllers"
    AuthCtrl["Auth Controller"]:::internal
    click AuthCtrl "https://github.com/azarenkov/chat-app/blob/main/src/controllers/auth_controller.rs"
    MsgCtrl["Message Controller"]:::internal
    click MsgCtrl "https://github.com/azarenkov/chat-app/blob/main/src/controllers/message_controller.rs"
    AppState["App State"]:::internal
    click AppState "https://github.com/azarenkov/chat-app/blob/main/src/controllers/shared/app_state.rs"
  end

  %% Subgraph: Services
  subgraph "Services"
    AuthService["Auth Service"]:::internal
    click AuthService "https://github.com/azarenkov/chat-app/blob/main/src/services/auth_service.rs"
    AuthServiceAbs["Auth Service Abstract"]:::internal
    click AuthServiceAbs "https://github.com/azarenkov/chat-app/blob/main/src/services/auth_service_abstract.rs"
    MsgService["Message Service"]:::internal
    click MsgService "https://github.com/azarenkov/chat-app/blob/main/src/services/message_service.rs"
    MsgServiceAbs["Message Service Abstract"]:::internal
    click MsgServiceAbs "https://github.com/azarenkov/chat-app/blob/main/src/services/message_service_abstract.rs"
    JWTServiceAbs["JWT Service Abstraction"]:::internal
    click JWTServiceAbs "https://github.com/azarenkov/chat-app/blob/main/src/services/jwt_service_abstract.rs"
    WSSenderAbs["WebSocket Sender Abstraction"]:::internal
    click WSSenderAbs "https://github.com/azarenkov/chat-app/blob/main/src/services/web_socket_sender_abstract.rs"
  end

  %% Subgraph: Repositories
  subgraph "Repositories"
    UserRepo["User Repository"]:::internal
    click UserRepo "https://github.com/azarenkov/chat-app/blob/main/src/repositories/user_repository.rs"
    MsgRepo["Message Repository"]:::internal
    click MsgRepo "https://github.com/azarenkov/chat-app/blob/main/src/repositories/message_repository.rs"
  end

  %% Infrastructure Components
  subgraph "Infrastructure"
    DBConn["Database Connector"]:::internal
    click DBConn "https://github.com/azarenkov/chat-app/blob/main/src/infrastructure/db/connection.rs"
    JWTMgmt["JWT Management"]:::internal
    click JWTMgmt "https://github.com/azarenkov/chat-app/blob/main/src/infrastructure/jwt/jwt_service.rs"
    WSHandling["WebSocket Handling"]:::internal
    click WSHandling "https://github.com/azarenkov/chat-app/blob/main/src/infrastructure/ws/web_socket_sender.rs"
    AppSetup["App Setup"]:::internal
    click AppSetup "https://github.com/azarenkov/chat-app/blob/main/src/infrastructure/app_setup.rs"
    Config["Configuration"]:::internal
    click Config "https://github.com/azarenkov/chat-app/blob/main/src/config.rs"
  end

  %% Relationships from External Clients
  Client -->|"HTTP_Request"| Nginx
  Nginx -->|"ReverseProxy"| API
  Client -->|"WebSocket"| WSHandling

  %% Docker encapsulation
  Docker --> API
  Docker --> MongoDB

  %% API Server initialization
  API -->|"Initializes"| AppSetup
  API -->|"Loads"| Config
  API -->|"MonitoredBy"| Sentry

  %% API Server calls Controllers
  API -->|"Routes"| AuthCtrl
  API -->|"Routes"| MsgCtrl
  API -->|"Uses"| AppState

  %% Controllers delegate to Services
  AuthCtrl -->|"Calls"| AuthService
  MsgCtrl  -->|"Calls"| MsgService

  %% Services interactions
  AuthService -->|"UsesJWT"| JWTMgmt
  JWTServiceAbs -->|"AbstractionFor"| JWTMgmt
  WSSenderAbs -->|"Uses"| WSHandling
  AuthService -->|"Utilizes"| AuthServiceAbs
  MsgService  -->|"Utilizes"| MsgServiceAbs

  %% Services call Repositories
  AuthService -->|"DB_Query"| UserRepo
  MsgService  -->|"DB_Operation"| MsgRepo

  %% Repositories use Database Connector
  UserRepo -->|"DB_Op"| DBConn
  MsgRepo  -->|"DB_Op"| DBConn

  %% Database Connector connects to MongoDB
  DBConn -->|"Connects"| MongoDB

  %% Sentry monitors WebSocket operations
  WSHandling -->|"MonitoredBy"| Sentry

  %% Style classes
  classDef external fill:#f9e79f,stroke:#d35400,stroke-width:2px;
  classDef internal fill:#aed6f1,stroke:#2471a3,stroke-width:2px;
  classDef database fill:#abebc6,stroke:#1e8449,stroke-width:2px;
```
