# javiertelioz/clean-architecture-go architecture

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

- Interactive diagram: https://gitdiagram.com/javiertelioz/clean-architecture-go
- Repository: https://github.com/javiertelioz/clean-architecture-go

## Overview

##### Main components
- Domain Layer:
- Contains the core business logic.
- Key folders: pkg/domain – with subdirectories for entity definitions, contracts (for repositories and services), exceptions, and domain-specific services.
- Application Layer:
- Implements business use cases.
- Folder: pkg/application/use_cases where each use case (e.g., authentication, “hello”, and user management) is encapsulated. This layer orchestrates the application logic and defines the execution path by calling into the domain when needed.
- Infrastructure Layer:
- Provides concrete implementations for contracts defined in the Domain layer.
- Includes controllers (HTTP, GraphQL), the database interactions (ORM, models, migrations), external service providers (such as Slack, crypto, and JWT implementations), and middleware (CORS and JWT authentication).
- Also contains routing definitions and server startup code.
- Presentation Layer:
- Provides user interface aspects such as localization (i18n) and serialization of responses.
- It acts as the outward facet of the application, interfacing directly with incoming HTTP requests.

##### Relationships and interactions
- The outer layer (Presentation & Infrastructure such as controllers and routes) receives HTTP requests (via a gin-gonic server as mentioned in the README) and hands them over to the appropriate controller.
- Controllers extract request parameters, then invoke the corresponding use case from the Application layer.
- Use Cases execute business logic by accessing domain entities, possibly calling domain services or validating business rules.
- To persist data or call external services, the system uses the Infrastructure layer’s implementations – for example, the repository (data access) from pkg/infrastructure/repository and other adapters.

##### Architectural patterns and design principles
- Clean Architecture/Onion Architecture: Emphasize the ringed layout of the software system with concentric layers (infrastructure controllers & presenters on the outside, application and domain in the center).
- DDD: Distinguish the Domain Model with entities, exceptions, and contracts.
- Use of middleware, controllers, and plugins for route handling and API exposure.

##### Relevant technologies, frameworks, and libraries
- Gin-Gonic is used as the HTTP server framework.
- GraphQL is supported as shown in pkg/infrastructure/graphql (with query, mutation, and resolver components).
- Database access is handled via an ORM implementation included in pkg/infrastructure/database along with a postgres-specific repository.
- Additional services include slack integration, JWT-based authentication, and crypto services.
- Testing and mocks are structured under the test directory for unit and integration testing which validate the use cases and controller behavior.

- HTTP Request → Controller (in Infrastructure) → Use Case (Application) → Domain Entity/Service.
- Also, show calls from use cases to infrastructural components (like repositories) and from controllers to routing middleware.
- Adding details such as:
- Configuration and bootstrap components (e.g., in pkg/infrastructure/bootstrap.go) which initialize the server and dependency injection.
- Testing and mocks layers as separate components clearly meant for unit/integration purposes (to emphasize isolation of concerns).

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% Presentation & Infrastructure Layer
    subgraph "Presentation & Infrastructure Layer"
        R["Routes"] 
        C["Controllers"] 
        M["Middleware"]
        B["Bootstrap"]
        I["Internationalization"]
        S["Serializers"]
        D["Database"]
        Repo["Repository"]
        IS["Infrastructure Services"]
        EP["External Providers"]
    end

    %% Application Layer
    subgraph "Application Layer"
        A1["Auth Use Case"]
        A2["Hello Use Case"]
        A3["User Use Case"]
    end

    %% Domain Layer
    subgraph "Domain Layer"
        DE["Domain Entities"]
        DC["Domain Contracts"]
        DS["Domain Services"]
        DX["Domain Exceptions"]
    end

    %% Connections - Infrastructure to Application
    R -->|"forwardsRequest"| C
    C -->|"calls"| A1
    C -->|"calls"| A2
    C -->|"calls"| A3

    %% Connections - Application to Domain & Infrastructure Data
    A1 -->|"executes"| DE
    A1 -->|"executes"| DS
    A1 -->|"dataAccess"| Repo

    A2 -->|"executes"| DE
    A2 -->|"executes"| DS
    A2 -->|"dataAccess"| Repo

    A3 -->|"executes"| DE
    A3 -->|"executes"| DS
    A3 -->|"dataAccess"| Repo

    %% Connections within Infrastructure
    Repo -->|"persistsData"| D
    C -->|"utilizes"| IS
    C -->|"utilizes"| EP
    B -->|"initializes"| R
    B -->|"initializes"| M
    B -->|"initializes"| I
    B -->|"initializes"| S

    %% Styling: assign classes
    class R,C,M,B,I,S,D,Repo,IS,EP infra
    class A1,A2,A3 app
    class DE,DC,DS,DX domain

    %% Class Definitions with Colors
    classDef infra fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef app fill:#ABEBC6,stroke:#1D8348,stroke-width:2px;
    classDef domain fill:#F9E79F,stroke:#B7950B,stroke-width:2px;

    %% Click Events for Infrastructure & Presentation
    click R "https://github.com/javiertelioz/clean-architecture-go/tree/main/pkg/infrastructure/routes"
    click C "https://github.com/javiertelioz/clean-architecture-go/tree/main/pkg/infrastructure/controllers"
    click M "https://github.com/javiertelioz/clean-architecture-go/tree/main/pkg/infrastructure/middleware"
    click B "https://github.com/javiertelioz/clean-architecture-go/blob/main/pkg/infrastructure/bootstrap.go"
    click I "https://github.com/javiertelioz/clean-architecture-go/tree/main/pkg/presentation/i18n"
    click S "https://github.com/javiertelioz/clean-architecture-go/tree/main/pkg/infrastructure/serializers"
    click D "https://github.com/javiertelioz/clean-architecture-go/tree/main/pkg/infrastructure/database"
    click Repo "https://github.com/javiertelioz/clean-architecture-go/tree/main/pkg/infrastructure/repository"
    click IS "https://github.com/javiertelioz/clean-architecture-go/tree/main/pkg/infrastructure/services"
    click EP "https://github.com/javiertelioz/clean-architecture-go/tree/main/pkg/infrastructure/providers"

    %% Click Events for Application Layer
    click A1 "https://github.com/javiertelioz/clean-architecture-go/tree/main/pkg/application/use_cases/auth"
    click A2 "https://github.com/javiertelioz/clean-architecture-go/tree/main/pkg/application/use_cases/hello"
    click A3 "https://github.com/javiertelioz/clean-architecture-go/tree/main/pkg/application/use_cases/user"

    %% Click Events for Domain Layer
    click DE "https://github.com/javiertelioz/clean-architecture-go/tree/main/pkg/domain/entity"
    click DC "https://github.com/javiertelioz/clean-architecture-go/tree/main/pkg/domain/contracts"
    click DS "https://github.com/javiertelioz/clean-architecture-go/tree/main/pkg/domain/services"
    click DX "https://github.com/javiertelioz/clean-architecture-go/tree/main/pkg/domain/exceptions"
```
