# quangdangfit/goshop architecture

> Architecture diagram of the GitHub repository quangdangfit/goshop, 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/quangdangfit/goshop
- Repository: https://github.com/quangdangfit/goshop

## Overview

##### Project Overview
- This is an e-commerce backend built with Go, using the Gin framework. It exposes both RESTful APIs and gRPC endpoints.
- Its purpose is to serve as the server for an e-commerce website, handling domains like carts, orders, products, and users.
- Key features include DDD (Domain-Driven Design) architecture, integration with Postgres (database), Redis (cache), JWT-based authentication, and API documentation via Swagger.

##### Main Components
a. Client Layer:
– External Consumers: Clients (e.g., web or mobile apps) interacting through HTTP (REST API) and possibly gRPC.
b. API and Service Layers:
– HTTP Server (cmd/api and internal/server/http): Handles RESTful interactions, routes (found under internal/order/port/http, internal/product/port/http, internal/user/port/http).
– gRPC Server (internal/server/grpc and grpc handlers under internal/cart/port/grpc, internal/user/port/grpc): Handles gRPC calls.
c. Domain Modules:
– Cart, Order, Product, User functionalities are separated by domain. Each domain has its own packages:
▪ DTOs (Data Transfer Objects)
▪ Models
▪ Repositories (with mocks for testing)
▪ Services (business logic, again with mocks)
▪ Ports (interface adapters for both HTTP and gRPC for Order, Product, and User).
d. Ancillary and Shared Components:
– pkg/config: Handles configuration loading (parameters such as ports, database, and redis URIs).
– pkg/dbs: For database connectivity abstracting Postgres.
– pkg/redis: For Redis interaction.
– pkg/middleware: Handles authentication and other cross-cutting concerns.
– pkg/jtoken, pkg/paging, pkg/response, pkg/utils: Utilities and common functions.
– proto: Contains gRPC proto definitions for cart and user (and auto-generated code under its “gen” subdirectories).

##### Relationships and Interactions
– Clients (web/mobile) interface with the HTTP API (via REST) and gRPC endpoints. These flows are distinct:
▪ REST: Primarily used for orders, products, and users.
▪ gRPC: Used for cart operations and additional user endpoints.
– The API layers invoke domain services defined in internal/*/service packages.
– Services manipulate data through repositories (internal/*/repository) following DDD principles.
– All major business domains share access to a common configuration (from pkg/config) and persistence layers:
▪ Data persistence through Gorm interacting with a PostgreSQL database (see pkg/dbs).
▪ Caching and session handling using Redis (see pkg/redis).
– Middleware (in pkg/middleware) secures requests using JWT (pkg/jtoken).

##### Architectural Patterns & Design Principles
– Domain Driven Design (DDD): Each subdirectory under “internal” represents a bounded context (e.g., cart, order, product, user).
– Separation of Concerns: Clear decoupling of API layer (HTTP & gRPC) from business logic (services) and data access (repositories).
– Testability: Presence of dedicated mocks for repositories and services indicates an emphasis on unit testing and modular design.
– Use of code generation: The proto folder and associated generated code showcase a structured approach to managing gRPC contracts.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Client Layer"
        A["Clients (Web/Mobile)"]:::client
    end

    subgraph "API Layer"
        B["HTTP Server"]:::api
        C["gRPC Server"]:::api
    end

    A -->|"REST/gRPC"| B
    A -->|"REST/gRPC"| C

    subgraph "Cart Domain"
        CD1["Cart DTOs"]:::domain
        CD2["Cart Models"]:::domain
        CD3["Cart Repository"]:::domain
        CD4["Cart Service"]:::domain
        CD5["Cart gRPC Ports"]:::domain
    end

    subgraph "Order Domain"
        OD1["Order DTOs"]:::domain
        OD2["Order Models"]:::domain
        OD3["Order Repository"]:::domain
        OD4["Order Service"]:::domain
        OD5["Order HTTP Ports"]:::domain
    end

    subgraph "Product Domain"
        PD1["Product DTOs"]:::domain
        PD2["Product Models"]:::domain
        PD3["Product Repository"]:::domain
        PD4["Product Service"]:::domain
        PD5["Product HTTP Ports"]:::domain
    end

    subgraph "User Domain"
        UD1["User DTOs"]:::domain
        UD2["User Models"]:::domain
        UD3["User Repository"]:::domain
        UD4["User Service"]:::domain
        UD5["User HTTP Ports"]:::domain
        UD6["User gRPC Ports"]:::domain
    end

    subgraph "Shared Components"
        SC1["Configuration"]:::shared
        SC2["Database (PostgreSQL)"]:::database
        SC3["Redis"]:::cache
        SC4["Middleware"]:::shared
        SC5["JWT"]:::shared
        SC6["Paging"]:::shared
        SC7["Response"]:::shared
        SC8["Utils"]:::shared
        SC9["gRPC Proto"]:::shared
    end

    %% API Layer connections to domain ports
    B -->|"REST"| OD5
    B -->|"REST"| PD5
    B -->|"REST"| UD5

    C -->|"gRPC"| CD5
    C -->|"gRPC"| UD6

    %% Within Order Domain flow
    OD5 --> OD4
    OD4 --> OD3
    OD3 -->|"DB"| SC2
    OD4 -.-> OD1
    OD4 -.-> OD2
    OD4 -->|"config"| SC1
    OD4 -->|"cache"| SC3
    OD4 -->|"JWT"| SC5

    %% Within Product Domain flow
    PD5 --> PD4
    PD4 --> PD3
    PD3 -->|"DB"| SC2
    PD4 -.-> PD1
    PD4 -.-> PD2
    PD4 -->|"config"| SC1
    PD4 -->|"cache"| SC3
    PD4 -->|"JWT"| SC5

    %% Within User Domain flow (HTTP ports)
    UD5 --> UD4
    UD4 --> UD3
    UD3 -->|"DB"| SC2
    UD4 -.-> UD1
    UD4 -.-> UD2
    UD4 -->|"config"| SC1
    UD4 -->|"cache"| SC3
    UD4 -->|"JWT"| SC5

    %% Within User Domain flow (gRPC ports)
    UD6 --> UD4

    %% Within Cart Domain flow
    CD5 --> CD4
    CD4 --> CD3
    CD3 -->|"DB"| SC2
    CD4 -.-> CD1
    CD4 -.-> CD2
    CD4 -->|"config"| SC1
    CD4 -->|"cache"| SC3
    CD4 -->|"JWT"| SC5

    %% API Layer uses Middleware and gRPC Proto
    B -->|"auth"| SC4
    C -->|"auth"| SC4
    C -->|"proto"| SC9

    %% Click Events
    click B "https://github.com/quangdangfit/goshop/blob/master/internal/server/http/server.go"
    click C "https://github.com/quangdangfit/goshop/blob/master/internal/server/grpc/server.go"

    click CD1 "https://github.com/quangdangfit/goshop/tree/master/internal/cart/dto"
    click CD2 "https://github.com/quangdangfit/goshop/tree/master/internal/cart/model"
    click CD3 "https://github.com/quangdangfit/goshop/tree/master/internal/cart/repository"
    click CD4 "https://github.com/quangdangfit/goshop/tree/master/internal/cart/service"
    click CD5 "https://github.com/quangdangfit/goshop/tree/master/internal/cart/port/grpc"

    click OD1 "https://github.com/quangdangfit/goshop/tree/master/internal/order/dto"
    click OD2 "https://github.com/quangdangfit/goshop/tree/master/internal/order/model"
    click OD3 "https://github.com/quangdangfit/goshop/tree/master/internal/order/repository"
    click OD4 "https://github.com/quangdangfit/goshop/tree/master/internal/order/service"
    click OD5 "https://github.com/quangdangfit/goshop/tree/master/internal/order/port/http"

    click PD1 "https://github.com/quangdangfit/goshop/tree/master/internal/product/dto"
    click PD2 "https://github.com/quangdangfit/goshop/tree/master/internal/product/model"
    click PD3 "https://github.com/quangdangfit/goshop/tree/master/internal/product/repository"
    click PD4 "https://github.com/quangdangfit/goshop/tree/master/internal/product/service"
    click PD5 "https://github.com/quangdangfit/goshop/tree/master/internal/product/port/http"

    click UD1 "https://github.com/quangdangfit/goshop/tree/master/internal/user/dto"
    click UD2 "https://github.com/quangdangfit/goshop/tree/master/internal/user/model"
    click UD3 "https://github.com/quangdangfit/goshop/tree/master/internal/user/repository"
    click UD4 "https://github.com/quangdangfit/goshop/tree/master/internal/user/service"
    click UD5 "https://github.com/quangdangfit/goshop/tree/master/internal/user/port/http"
    click UD6 "https://github.com/quangdangfit/goshop/tree/master/internal/user/port/grpc"

    click SC1 "https://github.com/quangdangfit/goshop/tree/master/pkg/config"
    click SC2 "https://github.com/quangdangfit/goshop/tree/master/pkg/dbs"
    click SC3 "https://github.com/quangdangfit/goshop/tree/master/pkg/redis"
    click SC4 "https://github.com/quangdangfit/goshop/tree/master/pkg/middleware"
    click SC5 "https://github.com/quangdangfit/goshop/tree/master/pkg/jtoken"
    click SC6 "https://github.com/quangdangfit/goshop/tree/master/pkg/paging"
    click SC7 "https://github.com/quangdangfit/goshop/tree/master/pkg/response"
    click SC8 "https://github.com/quangdangfit/goshop/tree/master/pkg/utils"
    click SC9 "https://github.com/quangdangfit/goshop/tree/master/proto"

    %% Styles
    classDef client fill:#fceabb,stroke:#f8b500,stroke-width:2px;
    classDef api fill:#f9d5d3,stroke:#d17d7d,stroke-width:2px;
    classDef domain fill:#d3f9d8,stroke:#7dd179,stroke-width:2px;
    classDef shared fill:#d3e0f9,stroke:#7d8fd1,stroke-width:2px;
    classDef database fill:#f9f9c5,stroke:#d9d900,stroke-width:2px;
    classDef cache fill:#e5f9f3,stroke:#19d3b8,stroke-width:2px;
```
