# thangchung/go-coffeeshop architecture

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

- Interactive diagram: https://gitdiagram.com/thangchung/go-coffeeshop
- Repository: https://github.com/thangchung/go-coffeeshop

## Overview

##### Main components
- Microservices: The project is organized as multiple services, each with its own directory under cmd (e.g., barista, counter, kitchen, product, proxy, and web). Each service implements a specific business capability.
- API Gateway: There is a gRPC gateway service that sits at the front for client requests.
- Frontend: The “web” service (with assets under cmd/web/app) acts as the user interface.
- Database: There is a Postgres database used by multiple services, and migration files exist under the “db/migrations” folder. Additionally, sqlc is used for generating database access code.
- Messaging: There is integration with RabbitMQ (with publisher and consumer code under pkg/rabbitmq) to support asynchronous communication between services (enabling a choreography saga pattern).
- Infrastructure & Deployment: Deployment artifacts for Nomad, Consul, and Terraform are provided under build directories and in files like docker-compose.yaml. There are also Dockerfiles for each service.
- Shared Kernel: Components common to all services (e.g., event types, shared domain code) appear under internal/pkg/shared_kernel.
- Configuration & Utilities: Global configuration management and logging utilities exist under pkg/config and pkg/logger.

##### Relationships and interactions
- The API Gateway is the front door for client requests which then directs calls to services such as product and counter.
- Services communicate with each other via gRPC and through events via RabbitMQ. For example, the counter service listens for events from the barista and kitchen services, while service interdependencies are managed using DDD patterns.
- Each microservice encapsulates its own domain logic and interfaces with its persistence layer (Postgres) via their respective internal/infras package directories.
- The microservices coordinate using a choreography saga approach—each service emits events (published to RabbitMQ) that are then consumed by other services.
- Deployment artifacts define how these services are run in a production or development Nomad/Consul environment.

##### Architectural patterns and design principles
- Domain-Driven Design (DDD): Notice the usage of “internal” directories to segment business logic, domain models, and infrastructure code.
- Dependency Injection: Code generation with Google Wire indicates an emphasis on wired dependency management.
- Event-Driven Architecture: The use of RabbitMQ for inter-service communication and dependency on “pkg/event” makes event flow central to the design.
- Microservices: Separation of the application into multiple, independently deployable services, each with its own configuration, REST/gRPC interface, and persistence layer.
- Containerization and orchestration: Use of Docker, Nomad, Consul, and Terraform specifies how the system is packaged and deployed.

- Clear labels for each major component (each microservice, the API gateway, the database, RabbitMQ messaging, and deployment infrastructure components like Nomad, Consul, Vault).
- Representation of the DDD layered structure, if desired, by highlighting the separation between domain logic, application layers, and infrastructure within each service.
- Notes on how dependency injection (using Google Wire) aggregates internal wiring of services and how source code generation (via sqlc for DB queries) plays a role.

## Diagram (Mermaid)

```mermaid
graph TD
    %% External Client
    subgraph "External Client"
        Client("Client Browser"):::client
    end

    %% Service Layer
    subgraph "Service Layer"
        PGW("gRPC API Gateway"):::service
        Bar("Barista Service"):::service
        Cou("Counter Service"):::service
        Kit("Kitchen Service"):::service
        Prod("Product Service"):::service
        Web("Web Frontend"):::frontend
    end

    %% Data Layer
    subgraph "Data Layer"
        DB("Postgres Database"):::database
        MIG("Migration Scripts"):::database
    end

    %% Messaging & Events
    subgraph "Messaging & Events"
        RMQ("RabbitMQ Messaging"):::messaging
    end

    %% Infrastructure & Deployment
    subgraph "Infrastructure & Deployment"
        NOM("Nomad/Consul/Vault"):::infrastructure
        DOCK("Docker & DevContainers"):::infrastructure
    end

    %% Shared Domain & Configuration
    subgraph "Shared & Config"
        Shared("Shared Kernel"):::shared
        Config("Config Utilities"):::shared
        Logger("Logging Utilities"):::shared
    end

    %% Client interactions
    Client -->|"grpcRequest"| PGW

    %% API Gateway routing to services
    PGW -->|"routes"| Prod
    PGW -->|"routes"| Cou
    PGW -->|"routes"| Bar
    PGW -->|"routes"| Kit

    %% Frontend interactions
    Web -.->|"fetchesAssets"| PGW

    %% Database interactions
    Prod -->|"accessesDB"| DB
    Cou -->|"accessesDB"| DB
    Bar -->|"accessesDB"| DB
    Kit -->|"accessesDB"| DB
    MIG -->|"updatesSchema"| DB

    %% Messaging flows
    Bar -->|"publishesEvent"| RMQ
    Kit -->|"publishesEvent"| RMQ
    RMQ -->|"deliversEvent"| Cou

    %% Shared Kernel dependencies
    Bar ---|"dependsOn"| Shared
    Cou ---|"dependsOn"| Shared
    Kit ---|"dependsOn"| Shared
    Prod ---|"dependsOn"| Shared
    PGW ---|"dependsOn"| Shared

    %% Configuration usage
    Bar ---|"usesConfig"| Config
    Cou ---|"usesConfig"| Config
    Kit ---|"usesConfig"| Config
    Prod ---|"usesConfig"| Config
    PGW ---|"usesConfig"| Config

    %% Logging utilities
    Bar ---|"logsWith"| Logger
    Cou ---|"logsWith"| Logger
    Kit ---|"logsWith"| Logger
    Prod ---|"logsWith"| Logger
    PGW ---|"logsWith"| Logger
    Web ---|"logsWith"| Logger

    %% Deployment associations
    Bar ---|"deployedWith"| NOM
    Cou ---|"deployedWith"| NOM
    Kit ---|"deployedWith"| NOM
    Prod ---|"deployedWith"| NOM
    PGW ---|"deployedWith"| NOM
    RMQ ---|"deployedWith"| NOM
    DB ---|"managedBy"| NOM
    MIG ---|"managedBy"| NOM
    Web ---|"deployedWith"| DOCK

    %% Click Events
    click Bar "https://github.com/thangchung/go-coffeeshop/tree/main/cmd/barista"
    click Cou "https://github.com/thangchung/go-coffeeshop/tree/main/cmd/counter"
    click Kit "https://github.com/thangchung/go-coffeeshop/tree/main/cmd/kitchen"
    click Prod "https://github.com/thangchung/go-coffeeshop/tree/main/cmd/product"
    click PGW "https://github.com/thangchung/go-coffeeshop/tree/main/cmd/proxy"
    click Web "https://github.com/thangchung/go-coffeeshop/tree/main/cmd/web"
    click DB "https://github.com/thangchung/go-coffeeshop/tree/main/pkg/postgres"
    click MIG "https://github.com/thangchung/go-coffeeshop/tree/main/db/migrations"
    click RMQ "https://github.com/thangchung/go-coffeeshop/tree/main/pkg/rabbitmq"
    click NOM "https://github.com/thangchung/go-coffeeshop/blob/main/build/nomad/jobs/grpc-gw.nomad.hcl"
    click DOCK "https://github.com/thangchung/go-coffeeshop/blob/main/.devcontainer"
    click Shared "https://github.com/thangchung/go-coffeeshop/tree/main/internal/pkg/shared_kernel"
    click Config "https://github.com/thangchung/go-coffeeshop/tree/main/pkg/config"
    click Logger "https://github.com/thangchung/go-coffeeshop/tree/main/pkg/logger"

    %% Styles
    classDef client fill:#ffcc00,stroke:#333,stroke-width:2px;
    classDef service fill:#a0d8f1,stroke:#333,stroke-width:2px;
    classDef frontend fill:#b3e0a7,stroke:#333,stroke-width:2px;
    classDef database fill:#f9d29d,stroke:#333,stroke-width:2px;
    classDef messaging fill:#FFD700,stroke:#333,stroke-width:2px;
    classDef infrastructure fill:#d3d3d3,stroke:#333,stroke-width:2px;
    classDef shared fill:#ffb6c1,stroke:#333,stroke-width:2px;
```
