How greedwizard/ddd-fastapi-kafka-chat works
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.