# dangpham112000/go-ecommerce-backend-api architecture

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

- Interactive diagram: https://gitdiagram.com/dangpham112000/go-ecommerce-backend-api
- Repository: https://github.com/dangpham112000/go-ecommerce-backend-api

## Overview

##### Project Type & Purpose
- This project is a backend web application developed in Go. It follows an MVC (Model-View-Controller) pattern as explicitly indicated in the README.
- The project includes features for user registration with an OTP-based verification flow, and it integrates with several external services such as MySQL (for persistence), Redis (for caching), and Kafka (for messaging/streaming).
- There are additional utilities for swagger documentation, logging, and database migration/interaction via sqlc and goose.

##### Main Components
a. Frontend (if any) vs. Backend:
b. Core Backend Layers (as described in README’s MVC flow):
- Users (Frontend clients/APIs)
- Controller Layer (located in internal/controller): Handles HTTP/S requests and transforms them to service calls.
- Service Layer (internal/service): Implements the business logic.
- Repository (Repo) Layer (internal/repo): Manages data access and interactions with the database.
- Models (internal/database models, internal/model, internal/po): Represent data structures and database schemas.
- Databases (MySQL as per SQL files and migration scripts)
c. Additional Infrastructure/Modules:
- Messaging and Events: Kafka components found under cmd/cli/kafka and initialization in internal/initialize/kafka.go.
- Configuration Management: Viper (cmd/cli/viper), config files in the config folder, and pkg/setting.
- API Documentation: Swagger documentation generation under cmd/swag and its docs directory.
- Middleware for authentication, logging, CORS handling, and rate limiting (located under internal/middlewares).
- Utility libraries for JWT handling, cryptography, and random number generation (internal/utils).
- Docker and deployment scripts: Dockerfile, docker-compose.yaml, and related Kafka docker configurations.
- Testing: tests directory with basic unit tests and coverage support.

##### Relationships & Interactions
- The Controllers pass these requests to the Service Layer.
- The Service Layer interacts with the Repository Layer which performs CRUD operations on the underlying MySQL Database.
- There are utility components and middleware sitting in between – e.g., authentication middleware intercepts requests to verify JWT tokens.
- The configuration management and logging systems (pkg/logger) are used across the application.

##### Highlighted Architectural Patterns & Technologies
- MVC architecture with clear separation of responsibilities.
- Layered/Hexagonal architecture: Controllers, Services, Repositories, and Utility layers are clearly demarcated.
- Use of middleware for cross-cutting concerns.
- Database migration & code generation using goose and sqlc.
- Microservice integration aspects (Kafka for messaging, Docker for containerized deployments).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Clients
    subgraph "External"
        User["User/Client"]:::external
    end

    %% Request Handling Layer
    subgraph "Request Handling"
        Routers["Routers"]:::controller
        Middleware["Middleware Components"]:::middleware
        Controllers["Controllers (HTTP API)"]:::controller
    end

    %% Business Logic Layer
    subgraph "Business Logic"
        Service["Service Layer (Business Logic)"]:::service
    end

    %% Persistence Layer
    subgraph "Persistence"
        Repository["Repository Layer (Data Access)"]:::repository
        Models["Models"]:::datamodel
        PObjects["P-Objects"]:::datamodel
        DBModels["DB Models"]:::datamodel
        MySQL["MySQL Database & Migrations"]:::database
        ShopSQL["Shop DB SQL"]:::database
        SQLDir["SQL Schema/Queries"]:::database
    end

    %% Messaging & Integration Layer
    subgraph "Messaging & Integration"
        KafkaCLI["Kafka CLI"]:::integration
        KafkaInit["Kafka Initialization"]:::integration
        Redis["Redis Cache"]:::integration
    end

    %% Configuration & Utilities Layer
    subgraph "Configuration & Utilities"
        Viper["Viper Config"]:::config
        ConfigFiles["Config Files"]:::config
        PkgSetting["Pkg Setting"]:::config
        UtilLib["Utility Libraries"]:::util
        LoggerInit["Logger Init"]:::logging
        Logger["Logger"]:::logging
    end

    %% API Documentation
    subgraph "API Documentation"
        Swagger["Swagger Docs"]:::documentation
    end

    %% Deployment & Containerization
    subgraph "Deployment"
        DockerfileNode["Dockerfile"]:::deployment
        DockerCompose["docker-compose.yaml"]:::deployment
        KafkaDockerfile["kafka.Dockerfile"]:::deployment
        KafkaCompose["kafka.docker-compose.yaml"]:::deployment
    end

    %% Connections for Request Flow
    User -->|"API_Request"| Routers
    Routers -->|"routes"| Middleware
    Middleware -->|"processes"| Controllers
    Controllers -->|"calls"| Service
    Service -->|"invokes"| Repository
    Repository -->|"queries"| Models
    Repository -->|"queries"| PObjects
    Repository -->|"queries"| DBModels
    Models -->|"maps_to"| MySQL
    PObjects -->|"maps_to"| MySQL
    DBModels -->|"maps_to"| MySQL
    MySQL -->|"contains"| ShopSQL
    MySQL -->|"contains"| SQLDir

    %% Messaging & Caching Connections
    Service -->|"publishes"| KafkaCLI
    Service -->|"publishes"| KafkaInit
    Service -->|"caches"| Redis

    %% Configuration & Utility Connections
    Controllers -->|"reads"| Viper
    Service -->|"reads"| Viper
    Viper -->|"includes"| ConfigFiles
    ConfigFiles -->|"includes"| PkgSetting
    Service -->|"uses"| UtilLib

    %% Logging Connections (dashed)
    Controllers ---|"logs"| Logger
    Service ---|"logs"| Logger
    Repository ---|"logs"| Logger
    KafkaCLI ---|"logs"| Logger
    KafkaInit ---|"logs"| Logger
    LoggerInit -->|"initializes"| Logger

    %% API Documentation Connection
    User ---|"refers_to"| Swagger

    %% Click Events for Component Mappings
    click Routers "https://github.com/dangpham112000/go-ecommerce-backend-api/tree/main/internal/routers"
    click Controllers "https://github.com/dangpham112000/go-ecommerce-backend-api/tree/main/internal/controller"
    click Middleware "https://github.com/dangpham112000/go-ecommerce-backend-api/tree/main/internal/middlewares"
    click Service "https://github.com/dangpham112000/go-ecommerce-backend-api/tree/main/internal/service"
    click Repository "https://github.com/dangpham112000/go-ecommerce-backend-api/tree/main/internal/repo"
    click Models "https://github.com/dangpham112000/go-ecommerce-backend-api/tree/main/internal/model"
    click PObjects "https://github.com/dangpham112000/go-ecommerce-backend-api/tree/main/internal/po"
    click DBModels "https://github.com/dangpham112000/go-ecommerce-backend-api/blob/main/internal/database/models.go"
    click MySQL "https://github.com/dangpham112000/go-ecommerce-backend-api/tree/main/internal/database"
    click ShopSQL "https://github.com/dangpham112000/go-ecommerce-backend-api/blob/main/db/shopgolangdb.sql"
    click SQLDir "https://github.com/dangpham112000/go-ecommerce-backend-api/tree/main/sql"
    click KafkaCLI "https://github.com/dangpham112000/go-ecommerce-backend-api/blob/main/cmd/cli/kafka/main.kafka.go"
    click KafkaInit "https://github.com/dangpham112000/go-ecommerce-backend-api/blob/main/internal/initialize/kafka.go"
    click Viper "https://github.com/dangpham112000/go-ecommerce-backend-api/blob/main/cmd/cli/viper/main.viper.go"
    click ConfigFiles "https://github.com/dangpham112000/go-ecommerce-backend-api/tree/main/config"
    click PkgSetting "https://github.com/dangpham112000/go-ecommerce-backend-api/tree/main/pkg/setting"
    click Swagger "https://github.com/dangpham112000/go-ecommerce-backend-api/tree/main/cmd/swag/docs"
    click UtilLib "https://github.com/dangpham112000/go-ecommerce-backend-api/tree/main/internal/utils"
    click LoggerInit "https://github.com/dangpham112000/go-ecommerce-backend-api/blob/main/internal/initialize/logger.go"
    click Logger "https://github.com/dangpham112000/go-ecommerce-backend-api/blob/main/pkg/logger/logger.go"
    click DockerfileNode "https://github.com/dangpham112000/go-ecommerce-backend-api/tree/main/Dockerfile"
    click DockerCompose "https://github.com/dangpham112000/go-ecommerce-backend-api/blob/main/docker-compose.yaml"
    click KafkaDockerfile "https://github.com/dangpham112000/go-ecommerce-backend-api/blob/main/kafka.Dockerfile"
    click KafkaCompose "https://github.com/dangpham112000/go-ecommerce-backend-api/blob/main/kafka.docker-compose.yaml"

    %% Class Definitions for Colouring
    classDef external fill:#FFD700,stroke:#333,stroke-width:2px;
    classDef controller fill:#f9c74f,stroke:#333,stroke-width:2px;
    classDef middleware fill:#f8961e,stroke:#333,stroke-width:2px;
    classDef service fill:#90be6d,stroke:#333,stroke-width:2px;
    classDef repository fill:#f94144,stroke:#333,stroke-width:2px;
    classDef datamodel fill:#577590,stroke:#333,stroke-width:2px;
    classDef database fill:#43aa8b,stroke:#333,stroke-width:2px;
    classDef integration fill:#f3722c,stroke:#333,stroke-width:2px;
    classDef config fill:#277da1,stroke:#333,stroke-width:2px;
    classDef util fill:#a5a5a5,stroke:#333,stroke-width:2px;
    classDef logging fill:#4d908e,stroke:#333,stroke-width:2px;
    classDef documentation fill:#f9c74f,stroke:#333,stroke-width:2px;
    classDef deployment fill:#577590,stroke:#333,stroke-width:2px;
```
