# ismasan/sourced_todo architecture

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

- Interactive diagram: https://gitdiagram.com/ismasan/sourced_todo
- Repository: https://github.com/ismasan/sourced_todo

## Overview

##### Main Components
- Web Application Layer: Represented by files such as app.rb, config.ru, boot.rb, and the UI directories. This layer is built with Sinatra and serves as the server-driven reactive UI with pages, layouts, and UI components.
- Domain Logic / Business Layer: Found in the domains/todos directory, which contains the core logic for managing lists, listings, and Slack dispatching related to TODO items. This layer handles command processing and event generation.
- Background Workers and Reactor Processes: The bin/workers.rb script indicates a dedicated worker process that, likely, processes events asynchronously (i.e., applying CQRS and ensuring eventual consistency).
- External Service Integration: Slack integration is evident from both the README instructions and domains/todos/slack_dispatcher.rb, which sends notifications to a Slack channel when a TODO item is marked done.
- Infrastructure & Deployment: The Dockerfile, docker-compose.yml, Procfile, and environment configuration files (env.example) indicate that the project also supports containerized deployment and process management.

##### Component Relationships and Data Flow
- Commands: Show how user requests (through the Sinatra web UI) trigger commands (e.g., add, update, complete a TODO item).
- Workers/Reactors: From the event store, background workers (bin/workers.rb) consume events to update read models or send external notifications (Slack notifier).
- UI Updates: The server-driven UI (powered by Datastar for streaming updates) reacts to changes in the event store – emphasizing synchronization across multiple client sessions/tabs.
- External Communication: Connections from the domain logic to external systems (Slack), initiated by the slack_dispatcher.

##### Architectural Patterns and Principles
- CQRS and Event Sourcing: Clearly denote the separation between the write model (command handling) and the read model (projections which are updated based on events). Emphasize that every action is first a command then translated into an event.
- Asynchronous / Eventually Consistent Processing: Indicate that the application uses background workers to apply changes asynchronously, ensuring that the UI is eventually updated as the system processes events.
- Domain-Driven Design: The separation into domains and UI layers signifies a clear division of responsibilities (business logic vs. presentation).

##### Relevant Technologies and Frameworks
- Ruby and Sinatra for the web layer.
- Postgres as the event store and event bus.
- Sourced library for handling event sourcing mechanisms.
- Datastar for streaming and reactive UI updates.
- Docker/Docker Compose for containerized deployment.
- Slack for sending notifications on completed TODO items.

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% Web Application Layer
    subgraph "Web Application Layer"
        app["app.rb"]:::ui
        boot["boot.rb"]:::ui
        config["config.ru"]:::ui
        pages["UI Pages (ui/pages)"]:::ui
        layouts["UI Layouts (ui/layouts)"]:::ui
        components["UI Components (ui/components)"]:::ui
    end

    %% Domain Logic / Business Layer
    subgraph "Domain Logic / Business Layer"
        list["List (list.rb)"]:::domain
        listings["Listings (listings.rb)"]:::domain
        slackDisp["Slack Dispatcher (slack_dispatcher.rb)"]:::domain
    end

    %% Event Store and Bus
    es["Event Store (Postgres)"]:::db

    %% Background Workers and Reactor Processes
    worker["Worker Process (bin/workers.rb)"]:::worker

    %% External Service Integration (Slack)
    slackLib["Slack Library (lib/slack.rb)"]:::external

    %% Infrastructure & Deployment
    subgraph "Infrastructure & Deployment"
        dockerfile["Dockerfile"]:::infra
        dockerCompose["docker-compose.yml"]:::infra
        procfile["Procfile"]:::infra
        env["env.example"]:::infra
    end

    %% Connections
    %% User initiates HTTP Request from UI
    pages -->|"HTTP_Request"| app
    %% Web Application handling command and passing to domain logic
    app -->|"Process_Command"| list
    app -->|"Process_Command"| listings
    app -->|"Process_Command"| slackDisp

    %% Domain Logic stores events in event store
    list -->|"Store_Event"| es
    listings -->|"Store_Event"| es
    slackDisp -->|"Store_Event"| es

    %% Event Store: Events are consumed by worker processes
    es -->|"Consume_Event"| worker

    %% Worker updates UI via streaming update
    worker -->|"UI_Streaming_Update"| pages

    %% Domain Slack Dispatcher also triggers external Slack integration
    slackDisp -->|"Trigger_Slack"| slackLib

    %% Worker also sends notifications to Slack
    worker -->|"Send_Slack_Notification"| slackLib

    %% Click Events for Web Application Layer
    click app "https://github.com/ismasan/sourced_todo/blob/main/app.rb"
    click boot "https://github.com/ismasan/sourced_todo/blob/main/boot.rb"
    click config "https://github.com/ismasan/sourced_todo/blob/main/config.ru"
    click pages "https://github.com/ismasan/sourced_todo/tree/main/ui/pages"
    click layouts "https://github.com/ismasan/sourced_todo/tree/main/ui/layouts"
    click components "https://github.com/ismasan/sourced_todo/tree/main/ui/components"

    %% Click Events for Domain Logic / Business Layer
    click list "https://github.com/ismasan/sourced_todo/blob/main/domains/todos/list.rb"
    click listings "https://github.com/ismasan/sourced_todo/blob/main/domains/todos/listings.rb"
    click slackDisp "https://github.com/ismasan/sourced_todo/blob/main/domains/todos/slack_dispatcher.rb"

    %% Click Events for External Service Integration
    click slackLib "https://github.com/ismasan/sourced_todo/blob/main/lib/slack.rb"

    %% Click Event for Event Store and Bus (storage directory)
    click es "https://github.com/ismasan/sourced_todo/tree/main/storage"

    %% Click Event for Background Worker
    click worker "https://github.com/ismasan/sourced_todo/blob/main/bin/workers.rb"

    %% Click Events for Infrastructure & Deployment
    click dockerfile "https://github.com/ismasan/sourced_todo/tree/main/Dockerfile"
    click dockerCompose "https://github.com/ismasan/sourced_todo/blob/main/docker-compose.yml"
    click procfile "https://github.com/ismasan/sourced_todo/tree/main/Procfile"
    click env "https://github.com/ismasan/sourced_todo/blob/main/env.example"

    %% Styles
    classDef ui fill:#D0E6A5,stroke:#333,stroke-width:2px;
    classDef domain fill:#FCF4D9,stroke:#333,stroke-width:2px;
    classDef db fill:#FFEAB2,stroke:#333,stroke-width:2px,stroke-dasharray: 5 5;
    classDef worker fill:#C9E4DE,stroke:#333,stroke-width:2px;
    classDef external fill:#F6BDC0,stroke:#333,stroke-width:2px;
    classDef infra fill:#B5EAD7,stroke:#333,stroke-width:2px;
```
