# sunls24/tmail architecture

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

- Interactive diagram: https://gitdiagram.com/sunls24/tmail
- Repository: https://github.com/sunls24/tmail

## Overview

##### Main Components
- Backend Server: Highlight the Go application. Mark the entry point (cmd/main.go) that brings together configuration (config/config.go) and internal application logic.
- ORM/Data Layer: Emphasize the “ent” directory, which includes model schemas (attachment, envelope), migration scripts (ent/migrate), query and mutation logic, and test scaffolding. This layer serves as the bridge between the app and the database.
- Internal Business Logic: Group components under the “internal” folder. Include:
– API endpoints (internal/api) that process incoming HTTP requests.
– Domain or business logic (internal/api/domain.go, fetch.go, report.go).
– Pub/sub mechanics (internal/pubsub) that may support the long polling mechanism.
– Scheduling (internal/schedule) for tasks like email expiration or clean-up.
– Utility functions overall (internal/utils).
- Frontend/UI: Represent the “web” folder containing Astro configurations, React/TSX components, pages, layouts, and styling. Note that the frontend is responsible for presenting UI elements (such as email lists, attachments, theme switching) and handling user interactions.
- Deployment/Infrastructure: Include Docker configuration files (.dockerignore, Dockerfile), CI/CD scripts (.github/workflows), and deployment instructions (deploy.md).

##### Out the Relationships and Data Flow
- Frontend to the Backend API, showing HTTP connections (or long polling channels) that allow users to request email information.
- That the Backend (cmd/main.go, internal/api) processes requests and, when needed, interacts with the ORM/Data Layer (ent) to store or retrieve email data and attachments.
- Database as an external component that the “ent” ORM interacts with. Even if the database is not explicitly visible in the file tree, it is an essential part of the architecture due to the migration scripts and data model definitions.
- Additional flows such as the internal Pub/Sub mechanism for pushing email updates to long polling connections.

##### Architectural Patterns and Technologies
- Separation of concerns: Clearly differentiate between the backend (handling data, API, business logic), the ORM/data layer (modeling emails/attachments), and the frontend (UI rendering with Astro and React).
- Long Polling: Mark the asynchronous, event-driven communication between the frontend and backend for real-time email retrieval.
- Micro Modularization: The use of designated directories (cmd, internal, ent, web) shows modular design with each piece handling a distinct responsibility.
- Tools & Frameworks:
- Golang for the backend logic.
- Ent as the ORM to define and manipulate the data schema.
- Astro and React used in the frontend for rendering components and managing interactive UI.
- Docker & GitHub Actions for containerization and CI/CD processes.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Client Layer"
        Client("User/Client"):::frontend
    end

    subgraph "Frontend Layer"
        FE("Frontend/UI"):::frontend
    end

    subgraph "Backend Layer"
        BS("Backend Server"):::backend
        API("API Endpoints"):::api
        PS("Pub/Sub Mechanism"):::pubsub
        SCH("Scheduling"):::backend
        UT("Utility Functions"):::utility
        ORM("ORM/Data Layer"):::orm
    end

    DB("Database"):::orm
    DEP("Deployment/Infrastructure"):::deployment

    Client -->|"HTTP_Request"| FE
    FE -->|"LongPolling_HTTP"| API
    BS -->|"RouteSetup"| API
    API -->|"Data_Query"| ORM
    ORM -->|"Executes_Migrations"| DB
    API -->|"Notify"| PS
    PS -->|"Email_Update"| FE
    SCH -->|"Cleanup_Task"| ORM
    API -->|"HelperCalls"| UT
    DEP -->|"Deploys"| BS

    click BS "https://github.com/sunls24/tmail/blob/main/cmd/main.go"
    click BS "https://github.com/sunls24/tmail/blob/main/config/config.go"
    click ORM "https://github.com/sunls24/tmail/tree/main/ent/"
    click API "https://github.com/sunls24/tmail/tree/main/internal/api/"
    click PS "https://github.com/sunls24/tmail/blob/main/internal/pubsub/pubsub.go"
    click SCH "https://github.com/sunls24/tmail/blob/main/internal/schedule/schedule.go"
    click UT "https://github.com/sunls24/tmail/blob/main/internal/utils/utils.go"
    click FE "https://github.com/sunls24/tmail/tree/main/web/"
    click DEP "https://github.com/sunls24/tmail/tree/main/Dockerfile"
    click DEP "https://github.com/sunls24/tmail/blob/main/.dockerignore"
    click DEP "https://github.com/sunls24/tmail/blob/main/.github/workflows/docker.yml"
    click DEP "https://github.com/sunls24/tmail/blob/main/deploy.md"

    classDef frontend fill:#AED6F1,stroke:#3498DB,stroke-width:2px;
    classDef backend fill:#A9DFBF,stroke:#27AE60,stroke-width:2px;
    classDef orm fill:#F9E79F,stroke:#F1C40F,stroke-width:2px;
    classDef api fill:#FADBD8,stroke:#E74C3C,stroke-width:2px;
    classDef deployment fill:#D7BDE2,stroke:#8E44AD,stroke-width:2px;
    classDef utility fill:#D5DBDB,stroke:#566573,stroke-width:2px;
    classDef pubsub fill:#F5CBA7,stroke:#E67E22,stroke-width:2px;
```
