# elct9620/clean-architecture-in-go-2025 architecture

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

- Interactive diagram: https://gitdiagram.com/elct9620/clean-architecture-in-go-2025
- Repository: https://github.com/elct9620/clean-architecture-in-go-2025

## Overview

This project is a cleanly structured example of Clean Architecture implemented in Go. It’s designed as a sample ordering system that supports both gRPC and HTTP interfaces. The architecture follows the classic separation of concerns by dividing the application into multiple layers such as API (delivery), use cases (business logic), domain entities, repository (data access), and even includes separate entry points for different protocols.

##### Main Components
- Entry Points (cmd):
- Two main executables: one for gRPC (cmd/grpc) and one for HTTP (cmd/http). Each has its own main.go file and uses dependency injection (via Wire) to bootstrap the application.
- API Layer (internal/api):
- gRPC API (internal/api/grpc) that exposes endpoints like LookupOrder and PlaceOrder.
- REST API (internal/api/rest) that similarly provides equivalent endpoints and includes configuration such as an OpenAPI specification.
- Domain (internal/entity):
- Domain entities encapsulate business data, for example, orders and tokens, with separate subdirectories.
- Use Cases (internal/usecase):
- Contains the business logic around order lookup and placement, integrating with the other layers via repository interfaces.
- Repository Layer (internal/repository):
- Contains multiple implementations including in-memory, bolt (key-value store), LRU caching, and SQLite. This abstraction allows flexibility in choosing the data source.
- Validator (internal/validator):
- Provides domain-specific validation logic.
- Database Scripts (db):
- SQL scripts (schema, orders, tokens) that define the database schema and sample data for persistence.
- Auxiliary/Testing Components:
- Feature files in the “features” directory imply behavior-driven testing.
- Files like godogs_test.go and http_test.go for testing.
- The “internal/testability” directory suggests infrastructure to facilitate integration or acceptance testing.
- Protocol Buffers (pkg/orderspb):
- Contains protobuf definitions and generated code for gRPC communications.
- Dependency Injection:
- Use of Wire (wire.go and wire_gen.go in various directories) indicates that dependency injection is employed to configure and wire up components.
- Configuration & Tooling:
- Files such as sqlc.yaml, Makefile, and GitHub workflows indicate CI/CD and code generation steps supporting the project lifecycle.

##### Relationships and Interactions
- The entry points (gRPC and HTTP main functions) initialize the respective API servers.
- The API layer (both gRPC and REST) receives client requests and passes them to the use case layer, which encapsulates the core business logic.
- The use cases interact with the repository layer to persist and retrieve data from either in-memory databases, SQLite, or other storage implementations.
- The entities represent the fundamental domain models (orders, tokens) used throughout the business logic and persistence layers.
- The validator is applied before the use cases perform operations to ensure that incoming data meets business requirements.
- External interfaces (API) communicate via clearly defined protocol contracts, with gRPC using the protocol buffers defined in pkg/orderspb, and REST exposing an OpenAPI-defined interface.

##### Architectural Patterns and Design Principles
- Clean Architecture: Separation of concerns, with distinct layers for delivery, business logic, domain entities, and repository abstraction.
- Dependency Injection: Configured via Wire, ensuring that dependencies are managed and injected in a centralized manner.
- Protocol Abstraction: The API layer is abstracted in such a way that both gRPC and REST interfaces can use the same underlying business logic.
- Extensibility: Multiple repository implementations signal that the application can run in different environments (testing, production) with minimal changes.

##### Relevant Technologies & Frameworks
- Go as the main language.
- gRPC and Protocol Buffers for one of the communication methods.
- REST/HTTP with OpenAPI for API documentation and client communication.
- Wire for dependency injection.
- Various storage backends (SQLite, BoltDB, in-memory solutions).
- CI tools via GitHub workflows.
- Makefile and SQL scripts to manage builds, tests, and database initialization.

##### Elements
- Use clear labels for all major components (e.g., “gRPC API”, “REST API”, “Business Logic/Use Cases”, “Repositories”, “Domain Entities”, “Database”).
- From API (gRPC/REST) to Use Cases – indicating incoming request processing.
- From Use Cases to Repositories – indicating data access.
- From Repositories to Database – showing persistent storage interactions.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Entry Points"
        E1["gRPC Entrypoint"]:::entryPoint
        E2["HTTP Entrypoint"]:::entryPoint
    end

    subgraph "API Layer"
        A1["gRPC API"]:::apiLayer
        A2["REST API"]:::apiLayer
    end

    V["Validation"]:::validator

    U1["Use Cases (LookupOrder & PlaceOrder)"]:::useCase

    subgraph "Repository Layer"
        R1["In-memory Repository"]:::repository
        R2["BoltDB Repository"]:::repository
        R3["LRU Caching Repository"]:::repository
        R4["SQLite Repository"]:::repository
    end

    D["Domain Entities (Orders & Tokens)"]:::domain

    DB["Database"]:::db

    PP["Protocol Buffers"]:::proto

    DI["Dependency Injection (Wire)"]:::di

    TA["Testing & Auxiliary"]:::testing

    DS["Database Scripts"]:::config

    CT["Configuration & Tooling"]:::config

    %% Connections
    DI --> E1
    DI --> E2

    E1 --> A1
    E2 --> A2

    A1 --> V
    A2 --> V

    V --> U1

    U1 --> R1
    U1 --> R2
    U1 --> R3
    U1 --> R4

    R1 --> DB
    R2 --> DB
    R3 --> DB
    R4 --> DB

    D --- U1
    D --- A1
    D --- A2

    PP --> A1

    TA --- U1
    TA --- A2

    DS --> DB

    CT --- U1
    CT --- DB

    %% Click Events
    click E1 "https://github.com/elct9620/clean-architecture-in-go-2025/blob/main/cmd/grpc/main.go"
    click E2 "https://github.com/elct9620/clean-architecture-in-go-2025/blob/main/cmd/http/main.go"
    click A1 "https://github.com/elct9620/clean-architecture-in-go-2025/blob/main/internal/api/grpc/grpc.go"
    click A2 "https://github.com/elct9620/clean-architecture-in-go-2025/blob/main/internal/api/rest/rest.go"
    click V "https://github.com/elct9620/clean-architecture-in-go-2025/blob/main/internal/validator/validator.go"
    click U1 "https://github.com/elct9620/clean-architecture-in-go-2025/blob/main/internal/usecase/usecase.go"
    click R1 "https://github.com/elct9620/clean-architecture-in-go-2025/blob/main/internal/repository/in_memory_orders.go"
    click R2 "https://github.com/elct9620/clean-architecture-in-go-2025/blob/main/internal/repository/bolt_orders.go"
    click R3 "https://github.com/elct9620/clean-architecture-in-go-2025/blob/main/internal/repository/lru_tokens.go"
    click R4 "https://github.com/elct9620/clean-architecture-in-go-2025/blob/main/internal/repository/sqlite/sqlite_orders.go"
    click D "https://github.com/elct9620/clean-architecture-in-go-2025/tree/main/internal/entity"
    click DB "https://github.com/elct9620/clean-architecture-in-go-2025/blob/main/db/schema.sql"
    click PP "https://github.com/elct9620/clean-architecture-in-go-2025/blob/main/pkg/orderspb/orders.proto"
    click DI "https://github.com/elct9620/clean-architecture-in-go-2025/blob/main/wire.go"
    click TA "https://github.com/elct9620/clean-architecture-in-go-2025/blob/main/features/http.feature"
    click DS "https://github.com/elct9620/clean-architecture-in-go-2025/blob/main/db/schema.sql"
    click CT "https://github.com/elct9620/clean-architecture-in-go-2025/blob/main/sqlc.yaml"

    %% Styles
    classDef entryPoint fill:#F9E79F,stroke:#F39C12,stroke-width:2px;
    classDef apiLayer fill:#AED6F1,stroke:#2471A3,stroke-width:2px;
    classDef validator fill:#FAD7A0,stroke:#D35400,stroke-width:2px;
    classDef useCase fill:#A9DFBF,stroke:#27AE60,stroke-width:2px;
    classDef repository fill:#F5CBA7,stroke:#BA4A00,stroke-width:2px;
    classDef domain fill:#D2B4DE,stroke:#8E44AD,stroke-width:2px;
    classDef db fill:#D6EAF8,stroke:#1B4F72,stroke-width:2px;
    classDef proto fill:#E8DAEF,stroke:#76448A,stroke-width:2px;
    classDef di fill:#FADBD8,stroke:#C0392B,stroke-width:2px;
    classDef testing fill:#FDEBD0,stroke:#CA6F1E,stroke-width:2px;
    classDef config fill:#DCDCDC,stroke:#7F8C8D,stroke-width:2px;
```
