# threedotslabs/wild-workouts-go-ddd-example architecture

> Architecture diagram of the GitHub repository threedotslabs/wild-workouts-go-ddd-example, 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/threedotslabs/wild-workouts-go-ddd-example
- Repository: https://github.com/threedotslabs/wild-workouts-go-ddd-example

## Overview

##### Main System Components

a) Frontend Layer:
- Vue.js web application (in /web directory)
- Firebase Authentication integration
- OpenAPI clients for service communication

b) Backend Services:
- Trainer Service (internal/trainer)
- Trainings Service (internal/trainings)
- Users Service (internal/users)

c) Infrastructure:
- Google Cloud Run (for hosting services)
- Firebase/Firestore (for data storage)
- gRPC and HTTP communication layers

##### Architecture Patterns to Highlight

a) Clean Architecture layers (for each service):
- Ports (API layer)
- Application (use cases)
- Domain (business logic)
- Adapters (infrastructure)

b) CQRS Pattern:
- Command handlers (command/)
- Query handlers (query/)
- Separate read/write models

##### Component Relationships

a) External Communication:
- Web Frontend → Firebase Auth
- Web Frontend → Backend Services (via OpenAPI)
- Service-to-Service communication (via gRPC)

b) Internal Service Structure:
- HTTP/gRPC Controllers → Application Layer
- Application Layer → Domain Layer
- Domain Layer → Repositories
- Repositories → Firestore

##### Specific Implementation Details

Technical elements:

a) Service Components:
**[Web Frontend] → [Firebase Auth]**
- ↓
**[API Gateway/Load Balancer]**
- ↓
**[HTTP/gRPC Ports] → [Application Services]**
- ↓
**[Domain Models] → [Repositories]**
- ↓
**[Firestore/Database]**

b) Cross-Cutting Concerns:
- Authentication middleware
- Logging decorators
- Metrics collection
- Error handling

##### Additional Elements

Supporting components:
- OpenAPI/Protobuf definitions
- Docker containers
- Cloud Run services
- Firebase services
- Terraform infrastructure

- User Interface Layer
- API Gateway Layer
- Service Layer
- Domain Layer
- Infrastructure Layer

This system design should reflect the application's evolution through the various stages described in the README's articles, showing both the microservices architecture and the DDD implementation patterns.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Styles and classes
    classDef frontend fill:#3498db,color:white
    classDef api fill:#2ecc71,color:white
    classDef service fill:#f1c40f,color:black
    classDef domain fill:#e74c3c,color:white
    classDef infra fill:#9b59b6,color:white
    classDef external fill:#95a5a6,color:white
    classDef common fill:#34495e,color:white

    %% Frontend Layer
    subgraph Frontend
        WebApp["Vue.js Web Application"]:::frontend
        Login["Login Component"]:::frontend
        Calendar["Calendar Component"]:::frontend
        Schedule["Schedule Training"]:::frontend
        TrainingList["Training List"]:::frontend
        FirebaseAuth["Firebase Auth"]:::frontend
    end
    click WebApp "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/tree/master/web/src"
    click Login "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/blob/master/web/src/pages/Login.vue"
    click Calendar "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/blob/master/web/src/pages/Calendar.vue"
    click Schedule "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/blob/master/web/src/pages/ScheduleTraining.vue"
    click TrainingList "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/blob/master/web/src/pages/TrainingsList.vue"
    click FirebaseAuth "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/blob/master/web/src/firebase.js"

    %% API Gateway Layer
    subgraph APIGateway
        OpenAPI["OpenAPI Specs"]:::api
        Protobuf["gRPC Protobuf"]:::api
        AuthMiddleware["Auth Middleware"]:::api
    end
    click OpenAPI "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/tree/master/api/openapi"
    click Protobuf "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/tree/master/api/protobuf"

    %% Trainer Service
    subgraph TrainerService
        direction TB
        subgraph TrainerPorts
            THTTPHandlers["HTTP Handlers"]:::service
            TGRPCHandlers["gRPC Handlers"]:::service
        end
        subgraph TrainerApplication
            TCommands["Commands
            - CancelTraining
            - MakeHoursAvailable
            - MakeHoursUnavailable
            - ScheduleTraining"]:::service
            TQueries["Queries
            - AvailableHours
            - HourAvailability"]:::service
        end
        subgraph TrainerDomain
            THourAggregate["Hour Aggregate
            - Availability Rules
            - Schedule Validation
            - Business Logic"]:::domain
        end
        subgraph TrainerInfra
            TFirestore["Firestore Repository"]:::infra
            TMySQL["MySQL Repository"]:::infra
            TMemory["Memory Repository"]:::infra
        end
    end
    click TrainerPorts "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/tree/master/internal/trainer/ports"
    click TrainerApplication "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/tree/master/internal/trainer/app"
    click THourAggregate "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/tree/master/internal/trainer/domain/hour"
    click TrainerInfra "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/tree/master/internal/trainer/adapters"

    %% Trainings Service
    subgraph TrainingsService
        direction TB
        subgraph TrainingsPorts
            TrHTTPHandlers["HTTP Handlers"]:::service
            TrOpenAPI["OpenAPI Handlers"]:::service
        end
        subgraph TrainingsApplication
            TrCommands["Commands
            - ApproveTraining
            - RejectTraining
            - RequestReschedule
            - CancelTraining
            - ScheduleTraining"]:::service
            TrQueries["Queries
            - AllTrainings
            - TrainingsForUser"]:::service
        end
        subgraph TrainingsDomain
            TrAggregate["Training Aggregate
            - Cancellation Policies
            - Rescheduling Rules
            - Business Logic"]:::domain
            TrUser["User Value Object"]:::domain
        end
        subgraph TrainingsInfra
            TrFirestore["Firestore Repository"]:::infra
            TrGRPC["gRPC Clients"]:::infra
        end
    end
    click TrainingsPorts "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/tree/master/internal/trainings/ports"
    click TrainingsApplication "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/tree/master/internal/trainings/app"
    click TrAggregate "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/tree/master/internal/trainings/domain/training"
    click TrainingsInfra "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/tree/master/internal/trainings/adapters"

    %% Users Service
    subgraph UsersService
        direction TB
        subgraph UsersPorts
            UHTTPHandlers["HTTP Handlers"]:::service
            UGRPCHandlers["gRPC Handlers"]:::service
        end
        UsersApp["User Management Operations"]:::service
        UsersInfra["Firestore & Auth Integration"]:::infra
    end
    click UsersService "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/tree/master/internal/users"
    click UHTTPHandlers "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/blob/master/internal/users/http.go"
    click UGRPCHandlers "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/blob/master/internal/users/grpc.go"

    %% Common Infrastructure
    subgraph CommonInfra
        Auth["Authentication"]:::common
        ErrorHandling["Error Handling"]:::common
        Logging["Logging"]:::common
        Metrics["Metrics"]:::common
        CQRS["CQRS Decorators"]:::common
    end
    click Auth "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/tree/master/internal/common/auth"
    click ErrorHandling "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/tree/master/internal/common/errors"
    click Logging "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/tree/master/internal/common/logs"
    click Metrics "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/tree/master/internal/common/metrics"
    click CQRS "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/tree/master/internal/common/decorator"

    %% External Services
    subgraph Infrastructure
        CloudRun["Google Cloud Run"]:::external
        Firebase["Firebase"]:::external
        CloudBuild["Cloud Build"]:::external
        IAM["IAM"]:::external
    end
    click Infrastructure "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/tree/master/terraform"
    click CloudRun "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/blob/master/terraform/cloud-run.tf"
    click Firebase "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/blob/master/terraform/firebase.tf"
    click IAM "https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/blob/master/terraform/iam.tf"

    %% Connections
    WebApp --> FirebaseAuth
    WebApp --> OpenAPI
    FirebaseAuth --> AuthMiddleware
    OpenAPI --> THTTPHandlers & TrHTTPHandlers & UHTTPHandlers
    Protobuf --> TGRPCHandlers & UGRPCHandlers

    THTTPHandlers & TGRPCHandlers --> TCommands & TQueries
    TCommands & TQueries --> THourAggregate
    THourAggregate --> TFirestore & TMySQL & TMemory

    TrHTTPHandlers & TrOpenAPI --> TrCommands & TrQueries
    TrCommands & TrQueries --> TrAggregate
    TrAggregate --> TrFirestore
    TrGRPC --> TGRPCHandlers & UGRPCHandlers

    UHTTPHandlers & UGRPCHandlers --> UsersApp
    UsersApp --> UsersInfra

    %% Common Infrastructure Connections
    Auth --> AuthMiddleware
    ErrorHandling --> TrainerService & TrainingsService & UsersService
    Logging --> TrainerService & TrainingsService & UsersService
    Metrics --> TrainerService & TrainingsService & UsersService
    CQRS --> TrainerService & TrainingsService & UsersService
```
