# aserg-ufmg/micro-livraria architecture

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

- Interactive diagram: https://gitdiagram.com/aserg-ufmg/micro-livraria
- Repository: https://github.com/aserg-ufmg/micro-livraria

## Overview

##### Project Type and Purpose
- This is a didactic, full-stack microservices demo (“Micro-Livraria”) built in Node.js.
- It consists of four services—Frontend, Controller, Inventory, Shipping—and illustrates:
- REST between browser and Controller
- gRPC (Protocol Buffers) between Controller and the two backend services
- Containerization (Docker) and end-to-end testing (Cypress)

##### High-Level Architectural Components
a. User Agent (Browser/Cypress)
b. Frontend Service (services/frontend)
c. Controller Service (services/controller)
d. Inventory Service (services/inventory)
e. Shipping Service (services/shipping)
f. Proto Definitions (proto/*.proto)
g. (Optional) Docker artifacts for service isolation
h. Configuration & Orchestration (package.json scripts, Dockerfiles, potential load-balancer/Kubernetes)

##### Component Interactions and Data Flow
- Browser ←(HTTP REST)→ Controller
- GET /products
- GET /shipping/:cep
- GET /product/:id
- Controller ←(gRPC)→ Inventory
- InventoryService.SearchAllProducts
- InventoryService.SearchProductByID
- Controller ←(gRPC)→ Shipping
- ShippingService.GetShippingRate
- Inventory reads from a local JSON (products.json); Shipping computes rates in-memory.

##### Architectural Patterns & Design Principles
- Microservices: each service is a single responsibility, independently deployable.
- API Gateway / Façade (Controller): translations between REST and gRPC.
- Interface Definition Layer (proto files): central contract for RPC stubs.
- Polyglot communication: REST for external clients, gRPC for high-performance internal calls.
- Containerization: Dockerfiles for isolating services in containers; potential orchestration.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Clients
    subgraph "External Clients"
        direction TB
        Browser["Browser\n(User Agent)"]:::external
        Cypress["Cypress\n(E2E Tester)"]:::external
    end

    %% Controller
    Controller["Controller Service\n(Express, port 3000)"]:::controller

    %% Internal Services
    subgraph "Internal Services"
        direction TB
        Inventory["Inventory Service\n(gRPC, port 50051)"]:::backend
        Shipping["Shipping Service\n(gRPC, port 50052)"]:::backend
    end

    %% Data and Proto
    ProtoDoc["Proto Definitions"]:::proto
    Data["products.json"]:::data

    %% Configuration
    Config["Configuration & Orchestration\n(package.json scripts)"]:::config

    %% Docker
    Docker["Docker Containers"]:::docker

    %% Connections
    Browser -->|"HTTP/REST"| Controller
    Cypress -->|"HTTP/REST"| Controller
    Controller -->|"gRPC: SearchAllProducts\nSearchProductByID"| Inventory
    Controller -->|"gRPC: GetShippingRate"| Shipping
    Inventory -->|"reads"| Data
    ProtoDoc -.-> Controller
    ProtoDoc -.-> Inventory
    ProtoDoc -.-> Shipping
    Config -.-> Controller
    Config -.-> Inventory
    Config -.-> Shipping
    Config -.-> Browser
    Config -.-> Cypress
    Docker -.-> Controller
    Docker -.-> Inventory
    Docker -.-> Shipping
    Docker -.-> Cypress

    %% Click Events
    click Browser "https://github.com/aserg-ufmg/micro-livraria/tree/main/services/frontend/"
    click Controller "https://github.com/aserg-ufmg/micro-livraria/blob/main/services/controller/index.js"
    click Inventory "https://github.com/aserg-ufmg/micro-livraria/blob/main/services/inventory/index.js"
    click Shipping "https://github.com/aserg-ufmg/micro-livraria/blob/main/services/shipping/index.js"
    click Data "https://github.com/aserg-ufmg/micro-livraria/blob/main/services/inventory/products.json"
    click ProtoDoc "https://github.com/aserg-ufmg/micro-livraria/blob/main/proto/inventory.proto"
    click ProtoDoc "https://github.com/aserg-ufmg/micro-livraria/blob/main/proto/shipping.proto"
    click Cypress "https://github.com/aserg-ufmg/micro-livraria/tree/main/cypress/"
    click Config "https://github.com/aserg-ufmg/micro-livraria/blob/main/package.json"

    %% Styles
    classDef external fill:#f0f8ff,stroke:#4682b4,color:#000
    classDef controller fill:#d0f0c0,stroke:#228b22,color:#000
    classDef backend fill:#ffe4b5,stroke:#cd853f,color:#000
    classDef proto fill:#e6e6fa,stroke:#9370db,color:#000
    classDef data fill:#fafad2,stroke:#bdb76b,color:#000
    classDef config fill:#fffacd,stroke:#f4a460,color:#000
    classDef docker fill:#f5f5f5,stroke:#808080,color:#000
```
