# kone-net/go-chat architecture

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

- Interactive diagram: https://gitdiagram.com/kone-net/go-chat
- Repository: https://github.com/kone-net/go-chat

## Overview

##### Project Type and Purpose
- This is a full-stack chat application designed for real-time communication. The backend (written in Go) manages WebSocket connections, HTTP REST API endpoints, database operations (with MySQL via GORM), and distributed messaging via Kafka. The frontend (in a separate repository) is built with React, Redux, and AntDesign.
- Key features include traditional chat (text, images, files), multimedia (voice/video/screen sharing), user management (login, registration, profile update), and distributed deployment.

##### File Structure to Extract Architectural Components
- Top-level directories and files indicate a clear separation between various architectural layers:
- "api" directory: Contains controller code (HTTP endpoints) for operations like managing users, groups, files, and messages.
- "cmd" directory: Contains the main entry point (main.go) that initializes the service.
- "internal" directory:
- dao – for data access using MySQL.
- model – defining database models for users, groups, messages, etc.
- router – binding Gin routes to controller and WebSocket handlers.
- server – implements the WebSocket server logic with client registration, message broadcasting, and disconnection handling.
- service – contains business logic for users, groups, and messages.
- kafka – for Kafka producer/consumer handling global message distribution.
- "pkg" directory: Houses common utilities (constants, error handling, logging with zap, protocol buffer messages, etc.), which are used across the project.
- "deployments" shows Docker files and docker-compose for containerized and distributed deployment.
- "web" holds static assets (which are also used by the frontend or for file upload operations).

##### Extract Additional Insights from the README
- The README details both back-end and front-end technology stacks. It describes WebSocket usage for persistent connections, integration of protocol buffers to serialize/deserialize message contents (allowing both text and binary data transfers), and the use of Kafka for distributed messaging.
- It also explains how WebSocket connections are handled (client registration, reading, and writing) and provides sample code from both Go and JavaScript.
- Deployment and build instructions (via Makefile, Docker, docker-compose) emphasize separation of concerns and modularity.

##### Mapping Out Major Components
a. Back-End Application:
- API Layer: Represent the HTTP controllers (in “api/v1”) that offer endpoints for login, file uploads, friend management, etc.
- WebSocket Server: Show a component responsible for handling WebSocket connections (from “internal/server”, “internal/router/socket.go”). Note how incoming connections are registered and managed.
- Business Logic/Services: Represent “internal/service” components (group_service, user_service, message_service) which process chat logic.
- Data Access Layer: Include “internal/dao” and “internal/model” for MySQL interactions.
- Messaging/Distributed Queue: Show a Kafka component (using “internal/kafka”) that publishes and consumes messages; underline its role in scaling and distributed deployment.
b. Database:
c. File Storage:
- “web/static” folder used for storing uploaded files, screenshots, assets, etc.
d. Front-End Application:
- Even though it is maintained in a separate repository (go-chat-web), include a block for the React application that interacts with the backend over WebSocket and RESTful APIs. Mention its usage of Redux, AntDesign, and protocol buffers.
e. External Services and Deployment:
- Docker & Nginx: Include these as infrastructural components used for containerizing the application, reverse proxying (via deployments/docker/nginx.conf), and load balancing.
- Mention that the system supports distributed deployment where Kafka enables message broadcasting across multiple instances.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Front-End Layer
    subgraph "Front-End Client"
        FE["React Application (Redux, AntDesign, Protocol Buffers)"]:::client
    end

    %% Infrastructure Layer
    subgraph "Deployment & Containerization"
        DEPLOY["Deployment (deployments/docker) + Makefile"]:::infra
    end

    %% Back-End Layer
    subgraph "Back-End Server"
    
        %% API Layer Subgraph
        subgraph "API Layer"
            APIControllers["HTTP Controllers (api/v1)"]:::backend
        end
        
        %% WebSocket Subgraph
        subgraph "WebSocket Server"
            WSManager["WebSocket Manager (internal/server)"]:::backend
            SocketRouter["Socket Router (internal/router/socket.go)"]:::backend
        end
        
        %% Business Logic / Services
        BusinessLogic["Business Logic/Services (internal/service)"]:::backend
        
        %% Data Access Subgraph
        subgraph "Data Access Layer"
            DAO["DAO (internal/dao)"]:::backend
            Models["Models (internal/model)"]:::backend
        end
        
        %% Distributed Messaging
        Kafka["Kafka Message Broker (internal/kafka)"]:::infra
        
        %% Config & Utilities Subgraph
        subgraph "Configuration & Utilities"
            Config["Config (config)"]:::backend
            PkgUtil["Pkg Utilities (pkg)"]:::backend
        end
    end

    %% External Systems
    Database["MySQL Database"]:::database
    FileStorage["File Storage (web/static)"]:::database

    %% Connections from Front-End
    FE -->|"REST_API"| APIControllers
    FE -->|"WebSocket"| SocketRouter

    %% Deployment reverse proxy
    DEPLOY -->|"ReverseProxy"| APIControllers

    %% Back-End internal flows
    APIControllers -->|"Call"| BusinessLogic
    WSManager -->|"Register/Relay"| BusinessLogic
    SocketRouter -->|"Route Messages"| BusinessLogic
    BusinessLogic -->|"DB Operations"| DAO
    BusinessLogic -->|"DB Operations"| Models
    DAO -->|"CRUD"| Database
    Models -->|"ORM"| Database
    BusinessLogic -->|"Publish"| Kafka
    APIControllers -->|"FileUpload"| FileStorage

    %% Usage of Config & Utilities by Business Logic
    BusinessLogic -->|"Uses"| Config
    BusinessLogic -->|"Uses"| PkgUtil

    %% Click Events
    click APIControllers "https://github.com/kone-net/go-chat/tree/main/api/v1"
    click WSManager "https://github.com/kone-net/go-chat/tree/main/internal/server"
    click SocketRouter "https://github.com/kone-net/go-chat/blob/main/internal/router/socket.go"
    click BusinessLogic "https://github.com/kone-net/go-chat/tree/main/internal/service"
    click DAO "https://github.com/kone-net/go-chat/tree/main/internal/dao"
    click Models "https://github.com/kone-net/go-chat/tree/main/internal/model"
    click Kafka "https://github.com/kone-net/go-chat/tree/main/internal/kafka"
    click Config "https://github.com/kone-net/go-chat/tree/main/config"
    click PkgUtil "https://github.com/kone-net/go-chat/tree/main/pkg"
    click Database "https://github.com/kone-net/go-chat/blob/main/chat.sql"
    click FileStorage "https://github.com/kone-net/go-chat/tree/main/web/static"
    click DEPLOY "https://github.com/kone-net/go-chat/tree/main/deployments/docker"

    %% Styles
    classDef client fill:#bbdefb,stroke:#0d47a1,stroke-width:2px;
    classDef backend fill:#c8e6c9,stroke:#2e7d32,stroke-width:2px;
    classDef database fill:#ffcdd2,stroke:#c62828,stroke-width:2px;
    classDef infra fill:#ffe0b2,stroke:#ef6c00,stroke-width:2px;
```
