# tqthy/cdn-for-ecommerce architecture

> Architecture diagram of the GitHub repository tqthy/cdn-for-ecommerce, 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/tqthy/cdn-for-ecommerce
- Repository: https://github.com/tqthy/cdn-for-ecommerce

## Overview

##### Main Components
- cdn-client: This is the frontend component (built with Vite, React/TypeScript) that serves static assets and user interfaces.
- load-balancer: Contains Dockerfile, Nginx configuration (njs scripts), and Lua scripts. It is responsible for managing and routing incoming requests across available servers.
- edge-server: Also built using Nginx with enhanced Lua and NJS scripts. This layer likely handles caching strategies, request filtering, and can serve as a proxy between the load-balancer and the origin server.
- origin: A NestJS-based backend (as seen in the “origin” folder with app.module.ts and several sub-modules including file, minio, and redis integrations). This is the core API and business logic layer that processes file uploads, interacts with storage backends (Minio integration is indicated), and handles event notifications (webhooks).
- storage: Although minimal in content, this directory represents the file storage system or mount point for static content.
- monitoring: Includes Grafana (preconfigured with plugins and a database) and Prometheus configurations to track system performance and issues.
- (Additional Test/Utility Directories): Directories like [test]tc and load_tests indicate scripts for testing the system, but these can be treated as ancillary support tools.

##### Relationships and Data Flow
- User requests (originating from a web browser) first encounter the load-balancer.
- The load-balancer routes these requests—via defined Nginx/Lua logic—to the edge-server, which implements caching or rapid response layers.
- If content isn’t cached or needs processing, the edge-server forwards requests to the origin server where business logic (via NestJS modules) comes into play.
- The origin server interacts with external services, such as:
- Object storage (evidenced by the minio/minio-storage modules and webhook instructions indicating integration with a Minio client),
- Caching or state mechanisms (via redis in the origin module and possibly through Lua scripts in the servers).
- The frontend (cdn-client) is served separately—it may be delivered by the load-balancer or edge-server and communicates with the backend over APIs.
- Monitoring components (Grafana and Prometheus) collect system metrics from the various containers and infrastructure components.

##### Architectural Patterns and Design Principles
- Containerized Microservices: Each core component runs in its own container (docker-compose indicates orchestration).
- Separation of Concerns: There is a clear isolation between the frontend (presentation layer), edge (caching/proxy), load-balancer (traffic management), and origin/backend (business logic) layers.
- Event-driven / Webhook Integration: The README mentions webhooks for Minio events.
- Caching and Smart Routing: Use of Nginx with Lua and NJS in both edge-server and load-balancer shows dynamic request handling and caching logic.

- Containers and Interactions:
- Docker orchestration by indicating that all components run within a Docker network (perhaps as a surrounding “Docker Compose” boundary).
- If possible, include annotations with key configuration details (e.g., “Nginx + Lua scripts” on the edge-server and load-balancer).
- Message flow when static assets and API requests are processed.
- Optionally, mention that internal modules in the Origin (e.g., file handling, minio integration, and caching via Redis) are key sub-components, which could be detailed in a zoomed-in view for further clarity.

## Diagram (Mermaid)

```mermaid
flowchart TD
    user["User (Browser)"]:::user

    subgraph "Docker Compose Environment"
        LB["Load Balancer (Nginx+Lua)"]:::loadbalancer
        CC["cdn-client (Frontend - Vite/React)"]:::frontend
        ES["Edge Server (Nginx+Lua)"]:::edge
        ORG["Origin (NestJS Backend)"]:::backend
        ST["Storage (Minio/Filesystem)"]:::storage
        MON["Monitoring (Grafana/Prometheus)"]:::monitoring
    end

    user -->|"sends_request"| LB
    LB -->|"serves_static"| CC
    LB -->|"routes_request"| ES
    ES -->|"forwards_API"| ORG
    ORG -->|"file_ops"| ST

    LB -->|"metrics"| MON
    CC -->|"metrics"| MON
    ES -->|"metrics"| MON
    ORG -->|"metrics"| MON
    ST -->|"metrics"| MON

    click CC "https://github.com/tqthy/cdn-for-ecommerce/tree/main/cdn-client/"
    click LB "https://github.com/tqthy/cdn-for-ecommerce/tree/main/load-balancer/"
    click ES "https://github.com/tqthy/cdn-for-ecommerce/tree/main/edge-server/"
    click ORG "https://github.com/tqthy/cdn-for-ecommerce/tree/main/origin/"
    click ST "https://github.com/tqthy/cdn-for-ecommerce/tree/main/storage/"
    click MON "https://github.com/tqthy/cdn-for-ecommerce/tree/main/monitoring/"

    classDef user fill:#e8daef,stroke:#8e44ad,stroke-width:2px
    classDef frontend fill:#d1f2eb,stroke:#1abc9c,stroke-width:2px
    classDef loadbalancer fill:#fdebd0,stroke:#f39c12,stroke-width:2px
    classDef edge fill:#fcf3cf,stroke:#f1c40f,stroke-width:2px
    classDef backend fill:#d6eaf8,stroke:#3498db,stroke-width:2px
    classDef storage fill:#ecf0f1,stroke:#95a5a6,stroke-width:2px
    classDef monitoring fill:#f9e79f,stroke:#f39c12,stroke-width:2px
```
