# vhqtvn/vh-mail-hook architecture

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

- Interactive diagram: https://gitdiagram.com/vhqtvn/vh-mail-hook
- Repository: https://github.com/vhqtvn/vh-mail-hook

## Overview

##### Main Components
- Mail Service: Located in the “crates/mail-service” directory. It is responsible for receiving, processing, encrypting, and managing emails. This service implements SMTP handling (with dedicated modules for SMTP server, handler, and DNS logic), rate limiting, and mailbox specific encryption.
- Web Application: Found in “crates/web-app”. It offers a RESTful API for mailbox management and authentication functionalities. Note that the web-app has both a frontend (using modern frameworks like Svelte, as seen by the presence of Svelte components, tailwind config, and Vite configuration) and a backend (Rust APIs, authentication logic, and integration with the common utilities).
- Common Library: In “crates/common”. This provides shared utilities such as database connectivity, migration scripts, rate limiting logic, and security functions across other crates.
- Client Library: In the “packages/npm” directory. This officially supports external JavaScript/TypeScript integrations with the VHMailHook system.

##### Relationships and Data Flows
- The Mail Service receives incoming emails via SMTP. It handles tasks like rate limiting and encryption (using AGE and other encryption modules) and stores processed email data.
- The Web Application offers API endpoints (for authentication, mailbox management, etc.) and interacts with the database (configured via SQLite). It also has front-end aspects that consume these APIs and provide a modern two-column layout.
- Both services share common utilities from the “common” crate, so there’s a dependency/integration aspect where shared modules (like database layers or security services) are reused.
- External OAuth providers (GitHub, Google) and a Telegram login widget are integrated with the web service for authentication. Also consider that the mail service may be exposed externally for incoming SMTP traffic.
- The Client Library acts as a wrapper for external apps/integrations, ensuring that API calls to the web application are properly formatted.

##### Architectural Patterns and Design Principles
- Modular Cargo Workspace: Each crate (common, mail-service, web-app) is separated by functionality, promoting a layered architecture.
- Separation of Concerns: Clearly divides email processing (mail-service) and API/front-end management (web-app).
- RESTful API: The web application exposes a set of APIs (detailed in README) for user authentication and mailbox operations.
- Automation and CI/CD: The presence of GitHub Workflows and Dockerfile suggest a containerized environment—good to include deployment pipelines, docker containers, and possibly automated publishing or building stages.

## Diagram (Mermaid)

```mermaid
graph TD
    %% External Entities
    ExtSMTP["External SMTP Clients"]:::external
    OAuth["OAuth Providers (GitHub,Google,Telegram)"]:::external
    CI_CD["CI/CD Pipeline (GitHub Workflows, Docker)"]:::ci
    DB["SQLite DB"]:::ci

    %% Common and Client Libraries
    CommonLib["Common Library"]:::common
    ClientLib["Client Library"]:::common

    %% Mail Service Subgraph
    subgraph "Mail Service"
        MS1["SMTP Handling"]:::mail
        MS2["Encryption Module"]:::mail
        MS3["Service Logic"]:::mail
    end

    %% Web Application Subgraph
    subgraph "Web Application"
        WA1["Web App Backend"]:::backend
        WA2["Web App Frontend"]:::frontend
    end

    %% Data Flow for Mail Service
    ExtSMTP -->|"SMTP_emails"| MS1
    MS1 -->|"Process"| MS2
    MS2 -->|"Forward"| MS3
    MS3 -->|"Stores"| DB
    MS3 -->|"Uses_shared"| CommonLib

    %% Data Flow for Web Application
    WA2 <-->|"APICalls"| WA1
    OAuth -->|"Auth"| WA1
    ClientLib -->|"API_wrapper"| WA1
    WA1 -->|"Uses_shared"| CommonLib

    %% CI/CD Deployment Flow
    CI_CD -->|"Deploys"| MS3
    CI_CD -->|"Deploys"| WA1

    %% Click Events
    click MS1 "https://github.com/vhqtvn/vh-mail-hook/tree/main/crates/mail-service/src/smtp"
    click MS2 "https://github.com/vhqtvn/vh-mail-hook/tree/main/crates/mail-service/src/security"
    click MS3 "https://github.com/vhqtvn/vh-mail-hook/blob/main/crates/mail-service/src/service.rs"
    click WA1 "https://github.com/vhqtvn/vh-mail-hook/tree/main/crates/web-app/src"
    click WA2 "https://github.com/vhqtvn/vh-mail-hook/tree/main/crates/web-app/frontend"
    click CommonLib "https://github.com/vhqtvn/vh-mail-hook/tree/main/crates/common"
    click ClientLib "https://github.com/vhqtvn/vh-mail-hook/tree/main/packages/npm"

    %% Styles
    classDef mail fill:#ffcc99,stroke:#d35400,stroke-width:2px;
    classDef backend fill:#aed6f1,stroke:#2471a3,stroke-width:2px;
    classDef frontend fill:#fadbd8,stroke:#c0392b,stroke-width:2px;
    classDef common fill:#fcf3cf,stroke:#f1c40f,stroke-width:2px;
    classDef external fill:#d5f5e3,stroke:#27ae60,stroke-width:2px;
    classDef ci fill:#d6eaf8,stroke:#3498db,stroke-width:2px;
```
