# gs89alberto/image-processor architecture

> Architecture diagram of the GitHub repository gs89alberto/image-processor, 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/gs89alberto/image-processor
- Repository: https://github.com/gs89alberto/image-processor

## Overview

##### Main Components
- Tasks Service – Handles the creation of image processing tasks through REST endpoints (e.g., POST /tasks and GET /tasks/:taskId). Within the tasks service, show layers such as:
- HTTP Interface/Controllers (routing, validators)
- Application Services (TaskService, business logic)
- Domain Entities/Repositories (e.g., Task entity, ITaskRepository)
- Infrastructure including Kafka integration (TaskCreatedProducer, TaskProcessedConsumer), MongoDB repository (MongoTaskRepository), and the webserver.
- Application Services (ImageService)
- Domain layer (Image entity, IImageRepository)
- Infrastructure components that include Kafka consumers (e.g., TaskCreatedConsumer that listens for tasks), and MongoDB repositories (MongoImageRepository).
- Common Package – Contains shared configuration (for DB, Kafka, etc.), DTOs, middleware (error handling, rate limiting, validations), constants, and utilities which are used by both microservices.
- External Systems – Include MongoDB for persistence, Kafka (with Zookeeper where applicable) for asynchronous messaging, and Docker Compose (for orchestration of these services).

##### Illustrating Interactions and Relationships
- The Tasks Service receives REST API requests from clients.
- After creating a task, the Tasks Service produces a Kafka message signaling that a task has been created.
- The Images Service consumes this event via its Kafka consumer (TaskCreatedConsumer) and then processes the image (transformation, storage).
- Once the processing is complete, if there is any event-driven update needed, an event (or callback) can update the Tasks Service.
- That both microservices share a common configuration and utility layer (represented by the common package) which acts cross-cutting concerns like logging, configuration, and DTO definitions.

##### Emphasizing Architectural Patterns
- Use of the Hexagonal Architecture pattern in both services – separate the domain logic from infrastructure details such as handling HTTP requests or interacting with Kafka/MongoDB.
- Clearly distinguish between layers:
- Domain Layer: Entities and business rules.
- Application Layer: Service logic and use cases.
- Infrastructure Layer: Controllers, persistence (MongoDB repositories), Kafka producers/consumers, and web server setup.
- Asynchronous event-driven communication layer using Kafka, showing producers and consumers from both microservices.

## Diagram (Mermaid)

```mermaid
graph TD
    %% External Systems
    Client("Client"):::external
    KafkaB("Kafka Broker"):::external
    Mongo("MongoDB"):::external
    DockerCompose("Docker Compose"):::external

    %% Common Package
    Common("Common Package"):::common

    %% Tasks Service
    subgraph "Tasks Service"
        %% Domain Layer
        subgraph "Domain Layer"
            T_AD("Task Entity"):::tasks
            T_ID("ITaskRepository"):::tasks
        end
        %% Application Layer
        subgraph "Application Layer"
            T_S("TaskService"):::tasks
        end
        %% Infrastructure Layer
        subgraph "Infrastructure Layer"
            subgraph "HTTP Interface"
                T_C("TaskController"):::tasks
                T_V("Validators"):::tasks
            end
            subgraph "Kafka Integration"
                T_KP("TaskCreatedProducer"):::tasks
                T_KC("TaskProcessedConsumer"):::tasks
            end
            subgraph "Persistence & Web"
                T_MR("MongoTaskRepository"):::tasks
                T_WS("HTTP Server"):::tasks
                T_R("Routes"):::tasks
            end
        end
    end

    %% Images Service
    subgraph "Images Service"
        %% Domain Layer
        subgraph "Domain Layer"
            I_E("Image Entity"):::images
            I_R("IImageRepository"):::images
        end
        %% Application Layer
        subgraph "Application Layer"
            I_S("ImageService"):::images
        end
        %% Infrastructure Layer
        subgraph "Infrastructure Layer"
            I_KC("TaskCreatedConsumer"):::images
            I_MR("MongoImageRepository"):::images
        end
    end

    %% Connections for Tasks Service
    Client -->|"HTTP_Request"| T_C
    T_C -->|"validate"| T_V
    T_C -->|"calls"| T_S
    T_S -->|"domain_logic"| T_AD
    T_S -->|"uses"| T_ID
    T_S -->|"saves_task"| T_MR
    T_S -->|"publishes_event"| T_KP
    T_KP -->|"sends_event"| KafkaB
    KafkaB -->|"task_processed"| T_KC
    T_KC -->|"updates"| T_S

    %% Webserver connections within Tasks Service
    T_WS -->|"starts"| T_R

    %% Connections for Images Service
    KafkaB -->|"event_consumed"| I_KC
    I_KC -->|"invokes"| I_S
    I_S -->|"processes"| I_E
    I_S -->|"stores_image"| I_MR

    %% Persistence connections to MongoDB
    T_MR -->|"CRUD"| Mongo
    I_MR -->|"CRUD"| Mongo

    %% Connections to Common Package
    T_C ---|"shared_utilities"| Common
    T_V ---|"shared_utilities"| Common
    T_S ---|"shared_logic"| Common
    I_S ---|"shared_logic"| Common

    %% Docker Compose orchestration
    DockerCompose -->|"orchestrates"| KafkaB
    DockerCompose -->|"orchestrates"| Mongo
    DockerCompose -->|"orchestrates"| T_WS
    DockerCompose -->|"orchestrates"| I_S

    %% Click Events for Tasks Service
    click T_C "https://github.com/gs89alberto/image-processor/blob/master/packages/tasks-service/src/infrastructure/interface/controllers/TaskController.ts"
    click T_V "https://github.com/gs89alberto/image-processor/blob/master/packages/tasks-service/src/infrastructure/interface/http/validators/tasks.validatror.ts"
    click T_S "https://github.com/gs89alberto/image-processor/blob/master/packages/tasks-service/src/application/services/TaskService.ts"
    click T_AD "https://github.com/gs89alberto/image-processor/blob/master/packages/tasks-service/src/domain/entities/Task.ts"
    click T_ID "https://github.com/gs89alberto/image-processor/blob/master/packages/tasks-service/src/domain/repositories/ITaskRepository.ts"
    click T_KP "https://github.com/gs89alberto/image-processor/blob/master/packages/tasks-service/src/infrastructure/kafka/TaskCreatedProducer.ts"
    click T_KC "https://github.com/gs89alberto/image-processor/blob/master/packages/tasks-service/src/infrastructure/kafka/TaskProcessedConsumer.ts"
    click T_MR "https://github.com/gs89alberto/image-processor/blob/master/packages/tasks-service/src/infrastructure/repositories/MongoTaskRepository.ts"
    click T_WS "https://github.com/gs89alberto/image-processor/blob/master/packages/tasks-service/src/infrastructure/interface/http/server.ts"
    click T_R "https://github.com/gs89alberto/image-processor/blob/master/packages/tasks-service/src/infrastructure/webserver/routes.ts"

    %% Click Events for Images Service
    click I_S "https://github.com/gs89alberto/image-processor/blob/master/packages/images-service/src/application/services/ImageService.ts"
    click I_E "https://github.com/gs89alberto/image-processor/blob/master/packages/images-service/src/domain/entities/Image.ts"
    click I_R "https://github.com/gs89alberto/image-processor/blob/master/packages/images-service/src/domain/repositories/IImageRepository.ts"
    click I_KC "https://github.com/gs89alberto/image-processor/blob/master/packages/images-service/src/infrastructure/kafka/TaskCreatedConsumer.ts"
    click I_MR "https://github.com/gs89alberto/image-processor/blob/master/packages/images-service/src/infrastructure/repositories/MongoImageRepository.ts"

    %% Click Event for Common Package
    click Common "https://github.com/gs89alberto/image-processor/tree/master/packages/common/src"

    %% Styles
    classDef tasks fill:#fffae6,stroke:#e6b800,stroke-width:2px;
    classDef images fill:#e6fffa,stroke:#00b3b3,stroke-width:2px;
    classDef common fill:#e6e6ff,stroke:#4d4dff,stroke-width:2px;
    classDef external fill:#f2dede,stroke:#a94442,stroke-width:2px;
```
