# mohanavel15/chatapp architecture

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

- Interactive diagram: https://gitdiagram.com/mohanavel15/chatapp
- Repository: https://github.com/mohanavel15/chatapp

## Overview

##### Project Type and Purpose
- This is a full-stack chat application (PWA) with both real-time (WebSocket) and request/response (REST) interactions.
- Features: user authentication, channels, messages, relationships (friends), file uploads, email notifications, push notifications (future).

##### File Structure
- Top-level GO files (gateway.go, main.go, middleware.go) bootstrap and wire together the services.
- `server/` directory contains:
- config (environment)
- database (MongoDB models & access)
- mail (SMTP integration)
- request/response DTOs
- restapi (HTTP endpoints)
- websocket (real-time event handling)
- utils (authentication, helpers)
- `web/` directory contains the React/PWA frontend built with Vite, Tailwind, TypeScript.
- Docker and docker-compose files orchestrate MongoDB, the Go server, and the static frontend.

##### README
- Confirms use of MongoDB, JWT auth, SMTP mail server.
- Describes build steps and environment variables.

##### Main Components & Their Interactions
a. Frontend (PWA)
- Hosted as static assets (index.html + JS/CSS) served by the Go server or CDN.
- React app uses HTTP REST for CRUD operations (login, fetch channels, messages, relationships).
- Opens a WebSocket for real-time message delivery and events (user online/offline, new messages).
b. API Gateway / HTTP Server
- gateway.go + middleware.go route incoming HTTP + WS connections to handlers.
- RestAPI handlers under `server/restapi/` (auth, channels, messages, invites, pins, files).
- WebSocket handlers under `server/websocket/` (connect/disconnect, event dispatch).
- Authentication middleware verifies JWTs on HTTP and WS.
c. Database Layer
- MongoDB (configured via MONGO_URI, MONGO_DATABASE).
- Collections for users, channels, messages, relationships, etc.
- Access layer in `server/database/`.
d. Mail Service
- SMTP integration to send verification, password reset, invites.
e. External Services
- SMTP server
- (Future) Push Notification service

##### Relationships & Data Flows
- Frontend → HTTP REST → API Gateway → Business logic → Database
- Frontend → WebSocket → Gateway → Event handlers → Database & back → Frontend
- Backend → SMTP → Mail Server
- Backend → MongoDB → Storage

##### Architectural Patterns & Design Principles
- Layered architecture:
- API layer (REST + WebSocket)
- Service/handler layer (business logic)
- Repository layer (database access)
- Separation of concerns: config, models, requests, responses isolated.
- JWT-based stateless authentication.
- Event-driven real-time updates via WebSocket channel.

##### Technologies & Frameworks to Call Out
- Go (Gin‐style router or net/http)
- MongoDB driver
- React + TypeScript + Vite + TailwindCSS
- WebSocket (native Go)
- Docker & Docker-Compose
- SMTP (net/smtp or third-party)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend (PWA)"
        direction TB
        Browser["Browser (PWA)"]:::frontend
        SW["Service Worker"]:::frontend
        RESTClient["REST API Client"]:::frontend
        WSClient["WebSocket Client"]:::frontend
    end

    Browser -->|"uses"| RESTClient
    Browser -->|"connects"| WSClient
    SW -->|"caches"/assets| Browser

    click Browser "https://github.com/mohanavel15/chatapp/tree/main/web/"
    click SW "https://github.com/mohanavel15/chatapp/tree/main/web/src/"

    %% Backend Layer
    subgraph "Backend Services"
        direction TB
        subgraph "Bootstrap"
            Main["main.go"]:::service
            GatewayCode["gateway.go"]:::service
            Middleware["middleware.go"]:::service
        end

        subgraph "API Layer"
            direction TB
            GW["API Gateway"]:::service
            AuthMW["Auth Middleware"]:::service
        end

        subgraph "REST API Handlers"
            direction TB
            AuthAPI["Auth Endpoints"]:::service
            UserAPI["User Endpoints"]:::service
            ChannelAPI["Channel Endpoints"]:::service
            MessageAPI["Message Endpoints"]:::service
            RelationshipAPI["Relationship Endpoints"]:::service
            InviteAPI["Invite Endpoints"]:::service
            PinAPI["Pin Endpoints"]:::service
            BanAPI["Ban Endpoints"]:::service
            FilesAPI["File Endpoints"]:::service
            RecipientAPI["Recipient Endpoints"]:::service
        end

        subgraph "WebSocket Handlers"
            direction TB
            WSCore["WebSocket Server"]:::service
            Connect["Connect/Disconnect"]:::service
            EventHandler["Event Dispatcher"]:::service
        end

        subgraph "Repository Layer"
            direction TB
            UserRepo["User Repository"]:::service
            ChannelRepo["Channel Repository"]:::service
            MessageRepo["Message Repository"]:::service
            RelationshipRepo["Relationship Repository"]:::service
        end

        DB["MongoDB"]:::db
        MailSvc["SMTP Mail Service"]:::external
    end

    %% Connections
    RESTClient -->|"HTTP Requests"| GW
    WSClient -->|"WS Connection"| WSCore
    GW -->|"applies"| AuthMW
    AuthMW --> AuthAPI
    AuthMW --> UserAPI
    AuthMW --> ChannelAPI
    AuthMW --> MessageAPI
    AuthMW --> RelationshipAPI
    AuthMW --> InviteAPI
    AuthMW --> PinAPI
    AuthMW --> BanAPI
    AuthMW --> FilesAPI
    AuthMW --> RecipientAPI

    AuthAPI -->|calls| UserRepo
    UserAPI -->|calls| UserRepo
    ChannelAPI -->|calls| ChannelRepo
    MessageAPI -->|calls| MessageRepo
    RelationshipAPI -->|calls| RelationshipRepo
    InviteAPI -->|calls| UserRepo
    PinAPI -->|calls| ChannelRepo
    BanAPI -->|calls| UserRepo
    FilesAPI -->|calls| MessageRepo
    RecipientAPI -->|calls| RelationshipRepo

    WSCore --> Connect
    Connect --> EventHandler
    EventHandler -->|dispatches| MessageRepo
    EventHandler -->|dispatches| ChannelRepo

    UserRepo --> DB
    ChannelRepo --> DB
    MessageRepo --> DB
    RelationshipRepo --> DB

    AuthAPI -->|sends email via| MailSvc

    %% Click Events
    click Main "https://github.com/mohanavel15/chatapp/blob/main/main.go"
    click GatewayCode "https://github.com/mohanavel15/chatapp/blob/main/gateway.go"
    click Middleware "https://github.com/mohanavel15/chatapp/blob/main/middleware.go"
    click AuthAPI "https://github.com/mohanavel15/chatapp/blob/main/server/restapi/auth.go"
    click UserAPI "https://github.com/mohanavel15/chatapp/blob/main/server/restapi/user.go"
    click ChannelAPI "https://github.com/mohanavel15/chatapp/blob/main/server/restapi/channel.go"
    click MessageAPI "https://github.com/mohanavel15/chatapp/blob/main/server/restapi/message.go"
    click RelationshipAPI "https://github.com/mohanavel15/chatapp/blob/main/server/restapi/relationship.go"
    click InviteAPI "https://github.com/mohanavel15/chatapp/blob/main/server/restapi/invite.go"
    click PinAPI "https://github.com/mohanavel15/chatapp/blob/main/server/restapi/pin.go"
    click BanAPI "https://github.com/mohanavel15/chatapp/blob/main/server/restapi/ban.go"
    click FilesAPI "https://github.com/mohanavel15/chatapp/blob/main/server/restapi/files.go"
    click RecipientAPI "https://github.com/mohanavel15/chatapp/blob/main/server/restapi/recipient.go"
    click WSCore "https://github.com/mohanavel15/chatapp/blob/main/server/websocket/websocket.go"
    click Connect "https://github.com/mohanavel15/chatapp/blob/main/server/websocket/connect.go"
    click EventHandler "https://github.com/mohanavel15/chatapp/blob/main/server/websocket/eventhandler.go"
    click UserRepo "https://github.com/mohanavel15/chatapp/blob/main/server/database/user.go"
    click ChannelRepo "https://github.com/mohanavel15/chatapp/blob/main/server/database/channel.go"
    click MessageRepo "https://github.com/mohanavel15/chatapp/blob/main/server/database/message.go"
    click RelationshipRepo "https://github.com/mohanavel15/chatapp/blob/main/server/database/relationship.go"
    click DB "https://github.com/mohanavel15/chatapp/blob/main/server/database/database.go"
    click MailSvc "https://github.com/mohanavel15/chatapp/blob/main/server/mail/mail.go"
    click Main "https://github.com/mohanavel15/chatapp/blob/main/main.go"

    %% Styles
    classDef frontend fill:#FFEE58,stroke:#F9A825,color:#000;
    classDef service fill:#64B5F6,stroke:#1E88E5,color:#fff;
    classDef db fill:#81C784,stroke:#388E3C,color:#fff;
    classDef external fill:#FFD54F,stroke:#F57F17,color:#000;
```
