# jayy-lmao/loosely-coupled-monolith-rust architecture

> Architecture diagram of the GitHub repository jayy-lmao/loosely-coupled-monolith-rust, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jayy-lmao/loosely-coupled-monolith-rust
- Repository: https://github.com/jayy-lmao/loosely-coupled-monolith-rust

## Overview

##### Top Level (Entry Point)
- It accepting HTTP requests (POST /sales)

##### Service Layer (Middle)
- Sales Service (sales crate)
- Shipping Service (shipping crate)
- Billing Service (implied by billing_contracts)
- Each service should have its own database context (shown as smaller attached databases)

##### Event Communication Layer
- PlacedOrder events (sales_contracts)
- OrderBilled events (billing_contracts)
- CreateShippingLabel events (shipping_contracts)

- Bidirectional communication between services and the event registry

Specific Elements to Include:

##### Components
- [API Gateway] → [Event Registry]
- ↓
- [Sales Service] ⇢ [Event: OrderPlaced] ⇢ [Shipping Service]
- ↓                                        ↓
- [Sales DB Context]               [Shipping DB Context]

##### Event Flow
- How OrderPlaced events trigger shipping label creation
- Loose coupling through the event registry pattern

##### Important Details
- A note about the registry pattern implementation
- Contract boundaries between services
- Test boundaries (sales_test crate)

- Blue: API Gateway
- Green: Sales Service
- Orange: Shipping Service
- Purple: Billing Service
- Red: Event Registry
- Gray: Databases
- Yellow: Events/Contracts

**Symbols:**
**→ Direct API Call**
**⇢ Event Publication**
**⇠ Event Subscription**
**□ Service**
**○ Database**
**◇ Event**

Level of Detail:
- Main service boundaries
- Event contracts but not individual message structures
- Database contexts without internal schema details
- Main endpoints but not individual functions

Additional Notes:

## Diagram (Mermaid)

```mermaid
graph TB
    %% Styles
    classDef gateway fill:#3498db,stroke:#2980b9,color:white
    classDef service fill:#2ecc71,stroke:#27ae60,color:white
    classDef database fill:#95a5a6,stroke:#7f8c8d,color:white
    classDef event fill:#f1c40f,stroke:#f39c12,color:black
    classDef registry fill:#e74c3c,stroke:#c0392b,color:white
    classDef endpoint fill:#9b59b6,stroke:#8e44ad,color:white

    %% API Gateway Layer
    Gateway["API Gateway"]:::gateway
    click Gateway "https://github.com/jayy-lmao/loosely-coupled-monolith-rust/blob/main/api/src/main.rs"

    %% Event Registry
    Registry["Event Registry"]:::registry
    click Registry "https://github.com/jayy-lmao/loosely-coupled-monolith-rust/blob/main/common/src/event_registry.rs"

    %% Services Layer
    SalesService["Sales Service"]:::service
    click SalesService "https://github.com/jayy-lmao/loosely-coupled-monolith-rust/blob/main/sales/src/lib.rs"
    ShippingService["Shipping Service"]:::service
    click ShippingService "https://github.com/jayy-lmao/loosely-coupled-monolith-rust/blob/main/shipping/src/lib.rs"
    BillingService["Billing Service"]:::service

    %% Database Contexts
    SalesDB[("Sales DB")]:::database
    click SalesDB "https://github.com/jayy-lmao/loosely-coupled-monolith-rust/blob/main/sales/src/sales_db_context.rs"
    ShippingDB[("Shipping DB")]:::database
    click ShippingDB "https://github.com/jayy-lmao/loosely-coupled-monolith-rust/blob/main/shipping/src/shipping_db_context.rs"

    %% Events/Contracts
    OrderPlaced["Order Placed Event"]:::event
    click OrderPlaced "https://github.com/jayy-lmao/loosely-coupled-monolith-rust/blob/main/sales_contracts/src/order_placed.rs"
    OrderBilled["Order Billed Event"]:::event
    click OrderBilled "https://github.com/jayy-lmao/loosely-coupled-monolith-rust/blob/main/billing_contracts/src/order_billed.rs"
    CreateLabel["Create Shipping Label Event"]:::event
    click CreateLabel "https://github.com/jayy-lmao/loosely-coupled-monolith-rust/blob/main/shipping_contracts/src/create_shipping_label.rs"

    %% Endpoints
    SalesEndpoint["Sales Endpoints"]:::endpoint
    click SalesEndpoint "https://github.com/jayy-lmao/loosely-coupled-monolith-rust/blob/main/sales/src/endpoints.rs"
    ShippingEndpoint["Shipping Endpoints"]:::endpoint
    click ShippingEndpoint "https://github.com/jayy-lmao/loosely-coupled-monolith-rust/blob/main/shipping/src/endpoints.rs"

    %% Order Management
    OrderMgmt["Order Management"]:::service
    click OrderMgmt "https://github.com/jayy-lmao/loosely-coupled-monolith-rust/blob/main/sales/src/order.rs"

    %% Testing
    TestSuite["Sales Test Suite"]:::service
    click TestSuite "https://github.com/jayy-lmao/loosely-coupled-monolith-rust/blob/main/sales_test/src/test_create_order.rs"

    %% Relationships
    Gateway --> Registry
    Gateway --> SalesEndpoint
    Gateway --> ShippingEndpoint
    
    SalesService --> SalesDB
    SalesService --> OrderMgmt
    SalesService -.-> OrderPlaced
    
    ShippingService --> ShippingDB
    ShippingService -.-> CreateLabel
    
    BillingService -.-> OrderBilled
    
    Registry --> SalesService
    Registry --> ShippingService
    Registry --> BillingService
    
    OrderPlaced -.-> ShippingService
    OrderBilled -.-> SalesService
    CreateLabel -.-> ShippingService
    
    TestSuite -.-> SalesService

    %% Legend
    subgraph Legend
        L1["Service"]:::service
        L2[("Database")]:::database
        L3["Event"]:::event
        L4["— Direct Connection"]
        L5["--> Event Flow"]
    end
```
