# khannedy/golang-clean-architecture architecture

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

- Interactive diagram: https://gitdiagram.com/khannedy/golang-clean-architecture
- Repository: https://github.com/khannedy/golang-clean-architecture

## Overview

##### Project Type Analysis
This is a Clean Architecture implementation in Go with both synchronous (HTTP) and asynchronous (Kafka) communication patterns. It's a full-stack backend application with web and worker components.

##### Main Components

A. External Layer (Purple):
- HTTP Clients
- Kafka Producers/Consumers
- Database (MySQL)

B. Delivery Layer (Blue):
- HTTP Controllers (user, contact, address)
- Message Consumers
- Middleware (auth)

C. Use Case Layer (Green):
- Business Logic Components (user, contact, address)

D. Repository Layer (Yellow):
- Data Access Layer
- Database Operations

E. Gateway Layer (Orange):
- Message Producers
- External System Integration

- [External Systems] ↔ [Delivery Layer] ↔ [Use Case Layer] ↔ [Repository/Gateway Layer] ↔ [Infrastructure]

##### Component Details

For each layer, show:
- Models/DTOs (internal/model)
- Entities (internal/entity)
- Events (internal/model/*_event.go)
- Converters (internal/model/converter)

##### Infrastructure Components

- MySQL Database
- Apache Kafka
- HTTP Server (Fiber)
- GORM Configuration

##### Specific Implementation Notes

- Clean architecture flow numbered 1-11 as mentioned in the README
- Separation between synchronous (HTTP) and asynchronous (Kafka) operations
- Database migration components
- How the configuration (config.json) affects all layers

##### Additional Details

- All used symbols
- Component types

## Diagram (Mermaid)

```mermaid
graph TB
    %% Styles and Classes
    classDef external fill:#E6E6FA,stroke:#000
    classDef delivery fill:#ADD8E6,stroke:#000
    classDef usecase fill:#90EE90,stroke:#000
    classDef repository fill:#FFE4B5,stroke:#000
    classDef gateway fill:#FFA07A,stroke:#000
    classDef infrastructure fill:#D3D3D3,stroke:#000

    %% External Systems
    Client["HTTP Clients"]:::external
    KafkaExt["Apache Kafka"]:::external
    DB[(MySQL Database)]:::external

    %% Entry Points
    WebServer["Web Server"]:::delivery
    Worker["Worker"]:::delivery
    click WebServer "https://github.com/khannedy/golang-clean-architecture/blob/main/cmd/web/main.go"
    click Worker "https://github.com/khannedy/golang-clean-architecture/blob/main/cmd/worker/main.go"

    %% Delivery Layer
    subgraph Delivery
        %% HTTP Controllers
        UserCtrl["User Controller"]:::delivery
        ContactCtrl["Contact Controller"]:::delivery
        AddressCtrl["Address Controller"]:::delivery
        click UserCtrl "https://github.com/khannedy/golang-clean-architecture/blob/main/internal/delivery/http/user_controller.go"
        click ContactCtrl "https://github.com/khannedy/golang-clean-architecture/blob/main/internal/delivery/http/contact_controller.go"
        click AddressCtrl "https://github.com/khannedy/golang-clean-architecture/blob/main/internal/delivery/http/address_controller.go"

        %% Message Consumers
        UserConsumer["User Consumer"]:::delivery
        ContactConsumer["Contact Consumer"]:::delivery
        AddressConsumer["Address Consumer"]:::delivery
        click UserConsumer "https://github.com/khannedy/golang-clean-architecture/blob/main/internal/delivery/messaging/user_consumer.go"
        click ContactConsumer "https://github.com/khannedy/golang-clean-architecture/blob/main/internal/delivery/messaging/contact_consumer.go"
        click AddressConsumer "https://github.com/khannedy/golang-clean-architecture/blob/main/internal/delivery/messaging/address_consumer.go"

        %% Middleware
        AuthMiddleware["Auth Middleware"]:::delivery
        Routes["Route Configuration"]:::delivery
        click AuthMiddleware "https://github.com/khannedy/golang-clean-architecture/blob/main/internal/delivery/http/middleware/auth_middleware.go"
        click Routes "https://github.com/khannedy/golang-clean-architecture/blob/main/internal/delivery/http/route/route.go"
    end

    %% Use Case Layer
    subgraph UseCase
        UserUC["User UseCase"]:::usecase
        ContactUC["Contact UseCase"]:::usecase
        AddressUC["Address UseCase"]:::usecase
        click UserUC "https://github.com/khannedy/golang-clean-architecture/blob/main/internal/usecase/user_usecase.go"
        click ContactUC "https://github.com/khannedy/golang-clean-architecture/blob/main/internal/usecase/contact_usecase.go"
        click AddressUC "https://github.com/khannedy/golang-clean-architecture/blob/main/internal/usecase/address_usecase.go"
    end

    %% Repository Layer
    subgraph Repository
        UserRepo["User Repository"]:::repository
        ContactRepo["Contact Repository"]:::repository
        AddressRepo["Address Repository"]:::repository
        click UserRepo "https://github.com/khannedy/golang-clean-architecture/blob/main/internal/repository/user_repository.go"
        click ContactRepo "https://github.com/khannedy/golang-clean-architecture/blob/main/internal/repository/contact_repository.go"
        click AddressRepo "https://github.com/khannedy/golang-clean-architecture/blob/main/internal/repository/address_repository.go"
    end

    %% Gateway Layer
    subgraph Gateway
        UserProd["User Producer"]:::gateway
        ContactProd["Contact Producer"]:::gateway
        AddressProd["Address Producer"]:::gateway
        click UserProd "https://github.com/khannedy/golang-clean-architecture/blob/main/internal/gateway/messaging/user_producer.go"
        click ContactProd "https://github.com/khannedy/golang-clean-architecture/blob/main/internal/gateway/messaging/contact_producer.go"
        click AddressProd "https://github.com/khannedy/golang-clean-architecture/blob/main/internal/gateway/messaging/address_producer.go"
    end

    %% Infrastructure Components
    subgraph Infrastructure
        DBMigrations["Database Migrations"]:::infrastructure
        KafkaConfig["Kafka Config"]:::infrastructure
        DBConfig["Database Config"]:::infrastructure
        HTTPConfig["HTTP Server Config"]:::infrastructure
        ValidatorConfig["Validation Config"]:::infrastructure
        LogConfig["Logging Config"]:::infrastructure
        click DBMigrations "https://github.com/khannedy/golang-clean-architecture/tree/main/db/migrations/"
        click KafkaConfig "https://github.com/khannedy/golang-clean-architecture/blob/main/internal/config/kafka.go"
        click DBConfig "https://github.com/khannedy/golang-clean-architecture/blob/main/internal/config/gorm.go"
        click HTTPConfig "https://github.com/khannedy/golang-clean-architecture/blob/main/internal/config/fiber.go"
        click ValidatorConfig "https://github.com/khannedy/golang-clean-architecture/blob/main/internal/config/validator.go"
        click LogConfig "https://github.com/khannedy/golang-clean-architecture/blob/main/internal/config/logrus.go"
    end

    %% Connections
    Client --> WebServer
    WebServer --> |HTTP| UserCtrl & ContactCtrl & AddressCtrl
    KafkaExt --> Worker
    Worker --> UserConsumer & ContactConsumer & AddressConsumer

    UserCtrl & ContactCtrl & AddressCtrl --> UserUC & ContactUC & AddressUC
    UserConsumer & ContactConsumer & AddressConsumer --> UserUC & ContactUC & AddressUC

    UserUC --> UserRepo
    ContactUC --> ContactRepo
    AddressUC --> AddressRepo

    UserUC --> UserProd
    ContactUC --> ContactProd
    AddressUC --> AddressProd

    UserRepo & ContactRepo & AddressRepo --> DB
    UserProd & ContactProd & AddressProd --> KafkaExt

    %% Infrastructure Connections
    Infrastructure --> Delivery & UseCase & Repository & Gateway

    %% Legend
    subgraph Legend
        L1["External Systems"]:::external
        L2["Delivery Layer"]:::delivery
        L3["Use Case Layer"]:::usecase
        L4["Repository Layer"]:::repository
        L5["Gateway Layer"]:::gateway
        L6["Infrastructure"]:::infrastructure
    end
```
