# fabiodeandrade/microservice1-listner architecture

> Architecture diagram of the GitHub repository fabiodeandrade/microservice1-listner, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/fabiodeandrade/microservice1-listner
- Repository: https://github.com/fabiodeandrade/microservice1-listner

## Overview

##### Project type and purpose
- This is a backend microservice built in TypeScript using Express.
- Its purpose is to consume messages from a RabbitMQ queue, persist them into MongoDB, and expose a REST API for clients to retrieve the stored data.

##### File structure
- src/main.ts: entry point that wires everything together.
- src/domains: domain services for message consumption (rabbitmq.domain.ts) and persistence (mongoose.domain.ts).
- src/infra: low-level infrastructure adapters (queue-consume infra, environment constants).
- src/interfaces: application interfaces, DTOs, controllers, routes, services, and dependency container.
- Docker artifacts (dockerfile, docker-compose.yaml) define RabbitMQ and MongoDB containers plus the app.
- CI workflow (.github/workflows/ci.yaml) automates linting/build/testing.

##### README for additional insights
- Follows Domain-Driven Design (DDD) to decouple business logic (domains) from infrastructure.
- Uses Docker Compose for dev/test with RabbitMQ & MongoDB.
- Exposes health check (`/`) and two GET routes (`/get/products`, `/get/product/:id`).

##### Main components & their interactions
a. Components
- External Systems:
- RabbitMQ broker
- MongoDB database
- Infrastructure Layer:
- Queue Consumer Infra (reads from RabbitMQ)
- Mongoose Infra (writes/reads to MongoDB)
- Env Constants Loader
- Domain Layer:
- message-consumer domain (parses & validates incoming messages)
- persist domain (transforms domain objects to persistence entities)
- Application Layer (Interfaces):
- REST API (Express Server)
– Controllers: product.controller
– Routes: main.router
– Services: product.service (fetches from domains)
- Dependency Container: wires infra ↔ domain ↔ interfaces
- Application Entry: main.ts (bootstraps Express, configures DI container, starts consumer)

b. Interactions and data flow
1. On startup, main.ts loads env constants, initializes DI container.
2. RabbitMQ consumer adapter connects to queue and subscribes.
3. Incoming message → message-consumer domain → persist domain → mongoose adapter → MongoDB.
4. Express server starts; client calls GET endpoints → controller → service → persist domain (read) → mongoose adapter → MongoDB → response.

c. Architectural patterns & design principles
- Layered / Hexagonal (Ports & Adapters) architecture separating domain from infra.
- Dependency Injection container to provide loose coupling and easy swapping of adapters.
- Domain-Driven Design: business logic in src/domains, persistence & queue encapsulated behind domain interfaces.

d. Key technologies & libraries
- TypeScript, Express, RabbitMQ client library, Mongoose, Docker Compose, ts-node (or compiled JS), GitHub Actions for CI.

##### Elements
- Clear labels: RabbitMQ Exchange/Queue, MessageConsumer Domain, Persist Domain, ProductController, ProductService.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Docker Configuration
    subgraph "Docker & CI"
        DC["docker-compose.yaml"]:::external
        DF["dockerfile"]:::external
        CI["CI Workflow (.github/workflows/ci.yaml)"]:::infra
    end

    %% External Services
    subgraph "External Services"
        Rabbit["RabbitMQ Broker"]:::external
        MongoDB["MongoDB Database"]:::external
        Client["API Client"]:::external
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        Env["Env Loader"]:::infra
        QCInfra["Queue Consume Infra"]:::infra
        MongooseInfra["Mongoose Infra"]:::infra
    end

    %% Domain Layer
    subgraph "Domain Layer"
        MsgConsumer["Message-Consumer Domain"]:::domain
        PersistDomain["Persist Domain"]:::domain
    end

    %% Application Layer
    subgraph "Application Layer"
        Main["main.ts"]:::app
        DI["DI Container"]:::app
        AppServer["Express Server"]:::app
        Router["Main Router"]:::app
        Controller["ProductController"]:::app
        Service["ProductService"]:::app
        DTO["Mongo DTOs"]:::app
        MInterface["Mongoose Interface"]:::app
        RInterface["RabbitMQ Interface"]:::app
        CInterface["ProductController Interface"]:::app
    end

    %% Relationships
    Main -->|"boot & wire"| DI
    DI -->|"provides adapter"| QCInfra
    DI -->|"provides adapter"| MongooseInfra
    DI --> MsgConsumer
    DI --> PersistDomain
    DI --> AppServer

    %% Queue to DB pipeline
    Rabbit -->|"consume message"| QCInfra
    QCInfra --> MsgConsumer
    MsgConsumer --> PersistDomain
    PersistDomain --> MongooseInfra
    MongooseInfra -->|"write"| MongoDB

    %% REST API pipeline
    Client -->|"HTTP GET"| AppServer
    AppServer --> Router
    Router --> Controller
    Controller --> Service
    Service --> PersistDomain
    PersistDomain --> MongooseInfra
    MongooseInfra -->|"read"| MongoDB
    Service --> Controller
    Controller --> AppServer
    AppServer --> Client

    %% Infrastructure support
    Env --> Main

    %% Click Events
    click DC "https://github.com/fabiodeandrade/microservice1-listner/blob/main/docker-compose.yaml"
    click DF "https://github.com/fabiodeandrade/microservice1-listner/tree/main/dockerfile"
    click CI "https://github.com/fabiodeandrade/microservice1-listner/blob/main/.github/workflows/ci.yaml"
    click QCInfra "https://github.com/fabiodeandrade/microservice1-listner/blob/main/src/infra/queue-consume/quue-consume.infra.ts"
    click Env "https://github.com/fabiodeandrade/microservice1-listner/blob/main/src/infra/constants/env.ts"
    click MsgConsumer "https://github.com/fabiodeandrade/microservice1-listner/blob/main/src/domains/message-consumer/rabbitmq.domain.ts"
    click PersistDomain "https://github.com/fabiodeandrade/microservice1-listner/blob/main/src/domains/persist/mongoose.domain.ts"
    click AppServer "https://github.com/fabiodeandrade/microservice1-listner/blob/main/src/interfaces/rest/app/server.api.ts"
    click Controller "https://github.com/fabiodeandrade/microservice1-listner/blob/main/src/interfaces/rest/controllers/product.controller.ts"
    click Router "https://github.com/fabiodeandrade/microservice1-listner/blob/main/src/interfaces/rest/routes/main.router.ts"
    click Service "https://github.com/fabiodeandrade/microservice1-listner/blob/main/src/interfaces/rest/services/product.service.ts"
    click DTO "https://github.com/fabiodeandrade/microservice1-listner/blob/main/src/interfaces/dto/mongo.dto.ts"
    click MInterface "https://github.com/fabiodeandrade/microservice1-listner/blob/main/src/interfaces/implements/mongoose.interface.ts"
    click RInterface "https://github.com/fabiodeandrade/microservice1-listner/blob/main/src/interfaces/implements/rabbitmq.interface.ts"
    click CInterface "https://github.com/fabiodeandrade/microservice1-listner/blob/main/src/interfaces/implements/productController.interface.ts"
    click DI "https://github.com/fabiodeandrade/microservice1-listner/blob/main/src/interfaces/shared/container.dependency.ts"
    click Main "https://github.com/fabiodeandrade/microservice1-listner/blob/main/src/main.ts"

    %% Styles
    classDef external fill:#cccccc,stroke:#333,stroke-width:1px
    classDef infra fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef domain fill:#90EE90,stroke:#333,stroke-width:1px
    classDef app fill:#FFCC99,stroke:#333,stroke-width:1px
```
