# dubjay18/ecom-api architecture

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

- Interactive diagram: https://gitdiagram.com/dubjay18/ecom-api
- Repository: https://github.com/dubjay18/ecom-api

## Overview

##### Main Components

A. External Layer (Client Interface)
- REST API Endpoints (Swagger documented)
- Authentication/Authorization Gateway
- Middleware Layer (logging, auth, admin checks)

B. Application Core
- Handlers (presentation layer)
- Services (business logic layer)
- Repositories (data access layer)
- Domain Models (business entities)

C. Infrastructure
- Database (SQL with migrations)
- JWT Authentication
- File Upload System
- Configuration Management

##### Component Relationships

a) Top Level (External Interface):
- HTTP clients connecting to the API
- Middleware chain (logger → auth → admin)
- To Swagger documentation

b) Core Architecture (Clean Architecture layers):
- Handler Layer: Show separate handlers for Users, Products, and Orders
- Service Layer: Connect corresponding services to handlers
- Repository Layer: Link repositories to services
- Domain Layer: Place domain models at the center

c) Infrastructure Components:
- Database connection handling
- JWT token management
- Image upload functionality
- Configuration management

##### Specific Elements

**[Client] → [Middleware] → [Handlers] → [Services] → [Repositories] → [Database]**
- ↑            ↑             ↑              ↑

- [Auth/JWT]   [Validators]  [Domain Models]  [Migrations]

##### Important Architectural Patterns to Highlight

- Clean Architecture pattern (showing clear separation of concerns)
- Repository pattern for data access
- Middleware chain pattern
- Domain-driven design elements

##### Technical Details

- Gin framework as the HTTP router
- GORM as the ORM layer
- JWT for authentication
- SQL migrations system
- Configuration management via environment variables

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        HTTP[HTTP Clients]
        Swagger["Swagger Documentation"]:::docs
    end

    subgraph Middleware
        Auth["Authentication Gateway"]:::middleware
        Admin["Admin Middleware"]:::middleware
        Logger["Logging Middleware"]:::middleware
        HTTP --> Logger --> Auth --> Admin
    end

    subgraph "Handler Layer"
        UH["User Handler"]:::handler
        PH["Product Handler"]:::handler
        OH["Order Handler"]:::handler
        Admin --> UH & PH & OH
    end

    subgraph "Service Layer"
        US["User Service"]:::service
        PS["Product Service"]:::service
        OS["Order Service"]:::service
        UH --> US
        PH --> PS
        OH --> OS
    end

    subgraph "Domain Layer"
        UD["User Domain"]:::domain
        PD["Product Domain"]:::domain
        OD["Order Domain"]:::domain
        US & PS & OS --> UD & PD & OD
    end

    subgraph "Repository Layer"
        UR["User Repository"]:::repo
        PR["Product Repository"]:::repo
        OR["Order Repository"]:::repo
        US --> UR
        PS --> PR
        OS --> OR
    end

    subgraph Infrastructure
        DB[(Database)]:::infra
        JWT["JWT Manager"]:::infra
        Upload["File Upload System"]:::infra
        Config["Configuration"]:::infra
        Migrations["Database Migrations"]:::infra
        
        UR & PR & OR --> DB
        Auth --> JWT
        PH --> Upload
        DB --> Migrations
    end

    subgraph Utilities
        Pagination["Pagination"]:::util
        Password["Password Handler"]:::util
        Validator["Validation"]:::util
        ErrorHandler["Error Handler"]:::util
        Response["Response Handler"]:::util
    end

    %% Click Events
    click Swagger "https://github.com/Dubjay18/ecom-api/blob/main/docs/swagger.yaml"
    click Auth "https://github.com/Dubjay18/ecom-api/blob/main/internal/middleware/auth.go"
    click Admin "https://github.com/Dubjay18/ecom-api/blob/main/internal/middleware/admin.go"
    click Logger "https://github.com/Dubjay18/ecom-api/blob/main/internal/middleware/logger.go"
    click UD "https://github.com/Dubjay18/ecom-api/blob/main/internal/domain/user_domain.go"
    click PD "https://github.com/Dubjay18/ecom-api/blob/main/internal/domain/product_domain.go"
    click OD "https://github.com/Dubjay18/ecom-api/blob/main/internal/domain/order_domain.go"
    click UH "https://github.com/Dubjay18/ecom-api/blob/main/internal/handler/user_handler.go"
    click PH "https://github.com/Dubjay18/ecom-api/blob/main/internal/handler/product_handler.go"
    click OH "https://github.com/Dubjay18/ecom-api/blob/main/internal/handler/order_handler.go"
    click US "https://github.com/Dubjay18/ecom-api/blob/main/internal/service/user_service.go"
    click PS "https://github.com/Dubjay18/ecom-api/blob/main/internal/service/product_service.go"
    click OS "https://github.com/Dubjay18/ecom-api/blob/main/internal/service/order_service.go"
    click UR "https://github.com/Dubjay18/ecom-api/blob/main/internal/repository/user_repo.go"
    click PR "https://github.com/Dubjay18/ecom-api/blob/main/internal/repository/product_repo.go"
    click OR "https://github.com/Dubjay18/ecom-api/blob/main/internal/repository/order_repo.go"
    click DB "https://github.com/Dubjay18/ecom-api/blob/main/internal/infrastructure/database/db.go"
    click JWT "https://github.com/Dubjay18/ecom-api/blob/main/pkg/jwt/jwt.go"
    click Upload "https://github.com/Dubjay18/ecom-api/blob/main/pkg/upload/image.go"
    click Config "https://github.com/Dubjay18/ecom-api/blob/main/internal/config/config.go"
    click Migrations "https://github.com/Dubjay18/ecom-api/tree/main/migrations"
    click Pagination "https://github.com/Dubjay18/ecom-api/blob/main/internal/util/pagination.go"
    click Password "https://github.com/Dubjay18/ecom-api/blob/main/internal/util/password.go"
    click Validator "https://github.com/Dubjay18/ecom-api/blob/main/internal/util/validator.go"
    click ErrorHandler "https://github.com/Dubjay18/ecom-api/blob/main/pkg/common/errors.go"
    click Response "https://github.com/Dubjay18/ecom-api/blob/main/pkg/common/response/response.go"

    %% Styling
    classDef docs fill:#f9f,stroke:#333,stroke-width:2px
    classDef middleware fill:#b19cd9,stroke:#333,stroke-width:2px
    classDef handler fill:#90caf9,stroke:#333,stroke-width:2px
    classDef service fill:#81c784,stroke:#333,stroke-width:2px
    classDef domain fill:#fff176,stroke:#333,stroke-width:2px
    classDef repo fill:#ffb74d,stroke:#333,stroke-width:2px
    classDef infra fill:#ef5350,stroke:#333,stroke-width:2px
    classDef util fill:#b0bec5,stroke:#333,stroke-width:2px
```
