# cntt-uth/shopee-clone-be architecture

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

- Interactive diagram: https://gitdiagram.com/cntt-uth/shopee-clone-be
- Repository: https://github.com/cntt-uth/shopee-clone-be

## Overview

##### Main Components
- Frontend & External Clients: Although this repository is for the backend API, note that it is developed to work in tandem with a separately hosted frontend. Indicate that external clients (e.g., the Shopee-clone Frontend) interact with this API.
- API Gateway (Express Application): Represent the Express server as the main entry point for requests, where middleware (authentication, error handling, rate limiting, helmet, etc.) is applied.
- Controllers & Routes: Show that HTTP requests are routed through clearly organized controllers (for addresses, authentication, products, orders, payments, shipping, etc.) which then forward the requests as necessary.
- Data Access Layer: Illustrate the repository components that interact with the database using TypeORM. Add “models” or “entities” (DTOs and entity definitions) that structure the data.
- Database: Display the MariaDB database and mention that migrations and seeders are used to initialize data.
- External Services & Libraries:
- Cloudinary & Sharp for media upload and image processing.
- Winston for logging.
- Swagger (API docs available at /api-docs) for documentation.
- Data Crawling & Scripts: Optionally include a side module for the “crawl” functionality (external scripts that scrape or seed product, shop, and category data) to show data ingestion paths.

##### Relationships and Interactions
- From the API gateway, split the flow into the controller layer which then relays validated data to the service layer (using Awilix for DI).
- That the service layer talks to the repository layer which in turn interacts with the database.
- Middleware (authentication, validation, error handling) as intercepting the API request flow.
- Integration flows where media files pass through Cloudinary/Sharp and logs/errors funnel into Winston-based logging.

##### Architectural Patterns & Design Principles
- Layered Architecture: Separate the system into clear layers (API/Controller, Service, Repository/Data Access, and External Services).
- Dependency Injection: Emphasize how Awilix is used to decouple modules, making each component easily testable and maintainable.
- Modularization: Use clear boundaries (controllers, services, repositories) along with a modular file structure as shown by the folder hierarchy.
- RESTful Design: Represent that the API handles various endpoints (addresses, auth, cate, products, orders, etc.) compliant with REST principles.

## Diagram (Mermaid)

```mermaid
graph TD
    %% External Clients
    ExtClients["External Clients (Shopee-clone Frontend)"]:::client

    %% API Gateway Layer
    ExpressServer["Express Server"]:::server
    Middleware["Middleware\n(Authentication,ErrorHandling,RateLimiting)"]:::middleware

    %% Controllers & Routes Layer
    subgraph "Controllers & Routes"
        Routes["Routes"]:::controller
        Controllers["Controllers"]:::controller
    end

    %% Service Layer
    subgraph "Service Layer"
        DIContainer["DI Container (Awilix)"]:::service
        Services["Services"]:::service
    end

    %% Data Access Layer
    subgraph "Data Access Layer"
        Repositories["Repositories"]:::repository
        Entities["Entities/Models"]:::repository
    end

    %% Database Setup
    subgraph "Database Setup"
        Migrations["Migrations"]:::database
        ORMConfig["ORM Config"]:::database
        MariaDB["MariaDB Database"]:::database
    end

    %% External Services & Libraries
    subgraph "External Services"
        Cloudinary["Cloudinary & Sharp Service"]:::external
        Winston["Winston Logging"]:::external
        Swagger["Swagger API Docs (/api-docs)"]:::external
    end

    %% Data Crawling & Scripts
    subgraph "Data Crawling & Scripts"
        Crawling["Crawling Functionality"]:::scripts
        Scripts["Supplemental Scripts"]:::scripts
    end

    %% Flow Connections
    ExtClients -->|"sends request"| ExpressServer
    ExpressServer -->|"applies"| Middleware
    Middleware -->|"routes to"| Routes
    Routes -->|"invokes"| Controllers
    Controllers -->|"uses DI"| DIContainer
    DIContainer -->|"resolves"| Services
    Services -->|"calls"| Repositories
    Repositories -->|"queries"| MariaDB
    Migrations -->|"initialize"| MariaDB
    ORMConfig -->|"configures"| MariaDB

    %% External integrations
    ExpressServer ---|"exposes docs"| Swagger
    ExpressServer ---|"logs via"| Winston
    Services ---|"media ops"| Cloudinary

    %% Data crawling integration (auxiliary)
    Crawling ---|"seeds data"| MariaDB
    Scripts ---|"seeds data"| MariaDB

    %% Click Events
    click ExpressServer "https://github.com/cntt-uth/shopee-clone-be/blob/main/src/app.ts"
    click Controllers "https://github.com/cntt-uth/shopee-clone-be/tree/main/src/controllers/"
    click Routes "https://github.com/cntt-uth/shopee-clone-be/tree/main/src/routes/"
    click Services "https://github.com/cntt-uth/shopee-clone-be/tree/main/src/services/"
    click DIContainer "https://github.com/cntt-uth/shopee-clone-be/blob/main/src/container.ts"
    click Repositories "https://github.com/cntt-uth/shopee-clone-be/tree/main/src/repository/"
    click Entities "https://github.com/cntt-uth/shopee-clone-be/tree/main/src/models/entity/"
    click Migrations "https://github.com/cntt-uth/shopee-clone-be/tree/main/src/migrations/"
    click ORMConfig "https://github.com/cntt-uth/shopee-clone-be/blob/main/src/config/ormconfig.js"
    click Cloudinary "https://github.com/cntt-uth/shopee-clone-be/blob/main/src/services/cloud.service.ts"
    click Winston "https://github.com/cntt-uth/shopee-clone-be/blob/main/src/config/winston_config.ts"
    click Swagger "https://github.com/cntt-uth/shopee-clone-be/tree/main/openapi/"
    click Middleware "https://github.com/cntt-uth/shopee-clone-be/tree/main/src/middlewares/"
    click Crawling "https://github.com/cntt-uth/shopee-clone-be/tree/main/crawl/"
    click Scripts "https://github.com/cntt-uth/shopee-clone-be/tree/main/scripts/"

    %% Styles
    classDef client fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef server fill:#D6EAF8,stroke:#1B4F72,stroke-width:2px;
    classDef middleware fill:#FCF3CF,stroke:#B7950B,stroke-width:2px;
    classDef controller fill:#FAD7A0,stroke:#D35400,stroke-width:2px;
    classDef service fill:#D5F5E3,stroke:#27AE60,stroke-width:2px;
    classDef repository fill:#F9E79F,stroke:#B9770E,stroke-width:2px;
    classDef database fill:#E5E7E9,stroke:#566573,stroke-width:2px;
    classDef external fill:#E8DAEF,stroke:#8E44AD,stroke-width:2px;
    classDef scripts fill:#FDEDEC,stroke:#CB4335,stroke-width:2px;
```
