# greedwizard/ddd-fastapi-kafka-chat architecture

> Architecture diagram of the GitHub repository greedwizard/ddd-fastapi-kafka-chat, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/greedwizard/ddd-fastapi-kafka-chat
- Repository: https://github.com/greedwizard/ddd-fastapi-kafka-chat

## Overview

##### Project Type & Purpose
- The README explains that this is a chat application built using FastAPI (as inferred from the “app/application/api” structure) and Kafka, following a Domain-Driven Design (DDD) approach with MongoDB for persistence. Although there are remnants of Django commands in the README, the directory structure (especially the separation into api, domain, infra, and logic folders) clearly indicates a DDD structure, which suggests a fast, asynchronous API endpoint handling chat messages with event processing capabilities.

##### Analyzing the File Structure
- The “app/application/api” directory contains the main entry points for the API requests. Files like “main.py” indicate the FastAPI endpoints.
- The “app/domain” hierarchy holds entities (business objects like chat messages), events (domain events that likely trigger side effects such as Kafka notifications), exceptions, and value objects. This is the heart of the business logic.
- The “app/infra” folder implements repository patterns for persisting messages, with separate adapters for in-memory and MongoDB persistence. This abstraction separates the domain from the underlying data storage technology.
- The “app/logic” section handles commands, event handlers, and a mediator pattern that helps orchestrate interactions between the domain and infrastructure. This likely decouples the actual work (the “what to do”) from the API layer.
- Additional folders for “app/settings” and “app/tests” provide configuration management and test suites respectively.
- Supplementary files like Dockerfile, docker_compose YAML files, .env.example, and Makefile indicate a containerized deployment and standard operations via Make commands.

##### Key Architectural Components and Their Relationships
a. API Layer:
– Components: FastAPI “main.py”, message handlers, and schemas.
– Role: Accepts HTTP requests, validates input (via schemas), and dispatches commands to the application’s logic.

b. Domain Layer:
– Components: Entities, domain events, value objects, and business exceptions.
– Role: Encapsulates the core business rules and data definitions. Domain events (like “messages” events) are triggered to communicate state changes.

c. Infra Layer:
– Components: Repository implementations (both for MongoDB and in-memory storage) and any related converters.
– Role: Acts as an abstraction layer between the domain and external systems. Here, the domain is decoupled from persistence details.

d. Logic Layer:
– Components: Command handlers, event handlers, a mediator to route interactions, and logic-specific exceptions.
– Role: Orchestrates the flow between the API and the domain/infrastructure. This layer implements the command pattern, handling requests by passing them through domain logic and triggering events.

e. Configuration & Deployment:
– Components: Settings configurations, Dockerfile, docker-compose YAML files, and Makefile commands.
– Role: Provides environment-specific settings and facilitates container orchestration, deployment, and testing.

##### Important Architectural Patterns/Design Principles
- Domain-Driven Design (DDD): Clear separation of business logic (domain) from technical concerns (infrastructure), with an emphasis on entities, value objects, and events.
- Command Pattern/Mediator: Allows different parts of the system (API, logic, domain) to interact without tight coupling.
- Repository Pattern: Enables interchangeable persistence implementations (MongoDB vs. in-memory).
- Containerized Deployment: Use of Docker/Docker Compose and a Makefile to manage and interact with different application environments.

##### Relevant Technologies & Frameworks
- FastAPI for the API endpoints.
- Kafka for message/event brokering (as indicated in the project title).
- MongoDB for persistent storage (evidenced by the repository folder containing “mongo.py”).
- Docker & Docker Compose for containerized deployments.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Config & Deployment
    subgraph "Config & Deployment"
        Settings["Settings Config (config.py)"]:::config
        Deployment["Deployment (Dockerfile, docker-compose, Makefile)"]:::config
    end

    %% API Layer
    subgraph "API Layer"
        API_Main["FastAPI API (main.py)"]:::api
        Msg_Handler["Message Handlers (handlers.py)"]:::api
        API_Schemas["API Schemas (schemas.py)"]:::api
        Msg_Schemas["Message Schemas (messages/schemas.py)"]:::api
    end

    %% Logic Layer
    subgraph "Logic Layer"
        Mediator["Command Mediator (mediator.py)"]:::logic
        Cmd_Handlers["Command Handlers (commands)"]:::logic
        Event_Handlers["Event Handlers (events)"]:::logic
        Logic_Exceptions["Logic Exceptions"]:::logic
    end

    %% Domain Layer
    subgraph "Domain Layer"
        Domain_Entities["Domain Entities"]:::domain
        Domain_Events["Domain Events"]:::domain
        Domain_Exceptions["Domain Exceptions"]:::domain
        Domain_Values["Value Objects"]:::domain
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        Repositories["Messages Repository"]:::infra
    end

    %% External Services
    subgraph "External Services"
        Kafka["Kafka Message Broker"]:::external
        MongoDB["MongoDB Database"]:::external
    end

    %% Relationships between Config & Deployment and API Layer
    Settings -->|"configures"| API_Main
    Deployment -->|"deploys"| API_Main
    Deployment -->|"deploys"| MongoDB
    Deployment -->|"deploys"| Kafka

    %% API to Logic
    API_Main -->|"dispatches"| Mediator
    Msg_Handler -->|"dispatches"| Mediator

    %% Logic interactions
    Mediator -->|"routesCmd"| Cmd_Handlers
    Mediator -->|"triggers"| Domain_Events
    Mediator -->|"callsRepo"| Repositories

    Cmd_Handlers -->|"invokes"| Domain_Entities
    Domain_Events -->|"handledBy"| Event_Handlers

    %% Domain to Infrastructure and External
    Domain_Entities -->|"persists"| Repositories
    Domain_Events -->|"publishes"| Kafka

    %% Repository stores data in MongoDB
    Repositories -->|"storesIn"| MongoDB

    %% Class definitions with colors
    classDef api fill:#cce5ff,stroke:#004085,color:#004085,stroke-width:2px;
    classDef domain fill:#d4edda,stroke:#155724,color:#155724,stroke-width:2px;
    classDef logic fill:#fff3cd,stroke:#856404,color:#856404,stroke-width:2px;
    classDef infra fill:#f8d7da,stroke:#721c24,color:#721c24,stroke-width:2px;
    classDef config fill:#e2e3e5,stroke:#6c757d,color:#6c757d,stroke-width:2px;
    classDef external fill:#e2f0cb,stroke:#3caea3,color:#3caea3,stroke-width:2px;

    %% Click Events for API Layer
    click API_Main "https://github.com/greedwizard/ddd-fastapi-kafka-chat/blob/master/app/application/api/main.py"
    click Msg_Handler "https://github.com/greedwizard/ddd-fastapi-kafka-chat/blob/master/app/application/api/messages/handlers.py"
    click API_Schemas "https://github.com/greedwizard/ddd-fastapi-kafka-chat/blob/master/app/application/api/schemas.py"
    click Msg_Schemas "https://github.com/greedwizard/ddd-fastapi-kafka-chat/blob/master/app/application/api/messages/schemas.py"

    %% Click Events for Domain Layer
    click Domain_Entities "https://github.com/greedwizard/ddd-fastapi-kafka-chat/tree/master/app/domain/entities"
    click Domain_Events "https://github.com/greedwizard/ddd-fastapi-kafka-chat/tree/master/app/domain/events"
    click Domain_Exceptions "https://github.com/greedwizard/ddd-fastapi-kafka-chat/tree/master/app/domain/exceptions"
    click Domain_Values "https://github.com/greedwizard/ddd-fastapi-kafka-chat/tree/master/app/domain/values"

    %% Click Events for Infrastructure Layer
    click Repositories "https://github.com/greedwizard/ddd-fastapi-kafka-chat/tree/master/app/infra/repositories/messages"

    %% Click Events for Logic Layer
    click Mediator "https://github.com/greedwizard/ddd-fastapi-kafka-chat/blob/master/app/logic/mediator.py"
    click Cmd_Handlers "https://github.com/greedwizard/ddd-fastapi-kafka-chat/tree/master/app/logic/commands"
    click Event_Handlers "https://github.com/greedwizard/ddd-fastapi-kafka-chat/blob/master/app/logic/events/base.py"
    click Logic_Exceptions "https://github.com/greedwizard/ddd-fastapi-kafka-chat/tree/master/app/logic/exceptions"

    %% Click Events for Config & Deployment
    click Settings "https://github.com/greedwizard/ddd-fastapi-kafka-chat/blob/master/app/settings/config.py"
    click Deployment "https://github.com/greedwizard/ddd-fastapi-kafka-chat/tree/master/Dockerfile"
```
