# ziaddevv/chat_app architecture

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

- Interactive diagram: https://gitdiagram.com/ziaddevv/chat_app
- Repository: https://github.com/ziaddevv/chat_app

## Overview

##### Project Type and Purpose
- This is a full-stack, real-time chat app:
- Frontend: Single-page HTML/CSS/JS (index.html + TailwindCSS)
- Backend: Go server (main.go) exposing HTTP endpoints and WebSocket support
- Database: SQLite (chat.db)
- File storage: local filesystem (`uploads/`)

##### File Structure
- Top level:
- `index.html` – static client UI
- `main.go` – Go HTTP/WebSocket server and handlers
- `chat.db` – SQLite database file
- `uploads/` – directory for user-uploaded files
- No separate `frontend/` or `backend/` folders – the Go server serves the static HTML and handles all dynamic logic.
- Configuration is embedded in Go code; no external Docker/CI scripts present.

##### Key Insights from README
- Real-time messaging via Gorilla WebSocket
- User session management via Gorilla Sessions
- Public/private chat rooms, auth (register/login/logout), file uploads
- Tech stack confirms: Go, WebSockets, SQLite, vanilla JS + TailwindCSS

##### Main Components
a. Browser (Client)
– UI in index.html, connects via HTTP & WebSocket
b. Go Server
– HTTP endpoint (serves index.html, auth routes, upload handler)
– WebSocket endpoint (real-time message relay, room management)
– Session store (in-memory/cookie)
– File upload handler writing to `uploads/`
c. SQLite Database
– Persists users, sessions, chat history, room metadata
d. Filesystem Storage
– Stores uploaded files under `uploads/`
e. External Interfaces (optional)
– Web browser’s WebSocket client
– Browser’s HTTP client for REST calls

##### Relationships and Data Flows
- Browser → HTTP (port 8080) → Go Server – static assets + REST endpoints
- Browser ↔ WebSocket → Go Server – send/receive chat messages in real time
- Go Server → SQLite – CRUD operations for users, rooms, messages
- Go Server → Filesystem – write/read uploaded files
- Session Cookies flow between Browser ↔ Go Server

##### Architectural Patterns and Design Principles
- Single Go binary acts as both static file server and API/WebSocket server (monolith)
- Pub/Sub pattern within Go for broadcasting messages to all connected clients in a room
- MVC-style separation (handlers/services/models) implied in Go code
- Persistent storage (SQLite) for durable state, ephemeral memory for live WS connections

##### Final Checklist
- Clear component labels (Frontend, API/WS Server, DB, Storage)
- Internal modules of the Go server (optional detail: Auth, WS Hub, File Handler, DB Layer)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Components
    Browser["Browser / Frontend UI"]:::frontend
    GoServerContainer["Go Server"]:::backend
    SQLite["SQLite DB (chat.db)"]:::db
    subgraph "Filesystem Storage"
        FSDir["uploads/"]:::storage
        SampleFile["20250912210507_test.txt"]:::storage
    end

    %% Go Server Internal Modules
    subgraph "Go Server Modules"
        Auth["Auth Handlers"] 
        WS["WebSocket Hub"] 
        FileH["File Upload Handler"]
        DBLayer["DB Layer"]
    end

    %% Connections
    Browser -->|"HTTP (port 8080)"| GoServerContainer
    Browser <-->|"WebSocket ws://.../chat"| WS
    Browser <-->|"Session Cookies"| GoServerContainer
    GoServerContainer --> Auth
    GoServerContainer --> WS
    GoServerContainer --> FileH
    GoServerContainer --> DBLayer
    DBLayer -->|"SQL queries (CRUD)"| SQLite
    FileH -->|"WRITE/READ file"| FSDir

    %% Click Events
    click Browser "https://github.com/ziaddevv/chat_app/blob/main/index.html"
    click GoServerContainer "https://github.com/ziaddevv/chat_app/blob/main/main.go"
    click SQLite "https://github.com/ziaddevv/chat_app/blob/main/chat.db"
    click FSDir "https://github.com/ziaddevv/chat_app/tree/main/uploads/"
    click SampleFile "https://github.com/ziaddevv/chat_app/blob/main/uploads/20250912210507_test.txt"

    %% Styles
    classDef frontend fill:#cce5ff,stroke:#004085;
    classDef backend fill:#d4edda,stroke:#155724;
    classDef db fill:#e2d1f9,stroke:#5a2490;
    classDef storage fill:#fff3cd,stroke:#856404;
```
