# codevalley/friday-api architecture

> Architecture diagram of the GitHub repository codevalley/friday-api, 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/codevalley/friday-api
- Repository: https://github.com/codevalley/friday-api

## Overview

##### Main Components
- API / Presentation Layer:
– Represented by the “routers” directory (specifically routers/v1 for API versioning). These handle the incoming REST endpoints (e.g., ActivityRouter, AuthRouter, DocumentRouter, etc.) and interface directly with clients.
- Service / Business Logic Layer:
– Found under “services” (e.g., ActivityService, DocumentService, OpenAIService, RateLimiter, etc.). This layer orchestrates the operations and implements the API’s core business rules.
- Domain Layer:
– Located in the “domain” directory with core entities like activity, moment, note, document, task, timeline, topic, and user. This is your business model that encapsulates validation, rules, and business types.
- Data Access Layer:
– Implemented through the “repositories” and “orm” directories. Here, SQLAlchemy models (in orm/) and repository classes (in repositories/) manage database operations for MySQL.
- Infrastructure Layer:
– Includes subcomponents such as the “infrastructure/queue” (with workers like activity_worker, note_worker, task_worker, and run_worker), which are managed via Redis (see “configs/redis” and “infrastructure/storage” for different storage mechanisms like local, S3, and mock). This also covers external integrations (e.g., OpenAI service).
- Configuration and Utility Components:
– “configs” directory encapsulates various configurations (Database, Redis, OpenAPI, Logging, etc.).
– Utility modules (in “utils”) handle error processing, retry logic, logging, security, and validation.
- External Systems:
– MySQL for persistent data storage.
– Redis for queuing and caching.
– OpenAI (via services/OpenAIService) for external API integration.
– Deployment Tools – Dockerfiles, docker-compose, and devcontainer setups for environment management.

##### Relationships and Flow
- Service layer interacting with the domain layer, creating and enforcing business rules and validations.
- Connection from the domain/service layer to the data access layer (repositories calling the ORM) for database CRUD operations with MySQL.
- A side branch where the service layer enqueues tasks/activities (data processing, note enrichment, etc.) that are managed by the queue workers using Redis.
- Configuration components underpinning the entire structure (configs for environment, logging, and OpenAPI) that support multiple layers.

##### Architectural Patterns & Design Principles
- Layered/MVC-like Separation: Clearly separate API (routers), business logic (services), domain modeling, and data access layers.
- Asynchronous Task Handling: Emphasize the use of background workers and queue-based processing with Redis.
- Domain-Driven Design: The domain layer encapsulating key business logic and validation.
- Extensibility: Note the use of interfaces (e.g., in domain/ports) and well-organized configurations to allow easier future integration of additional services or storage solutions.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Client
    Client["Client"]:::client

    %% API Layer
    subgraph "API Layer"
        API["API Endpoints (routers/v1)"]:::api
    end

    %% Service Layer
    subgraph "Service Layer"
        Service["Business Logic (services)"]:::service
        External["External Service: OpenAI"]:::ext
    end

    %% Domain Layer
    subgraph "Domain Layer"
        Domain["Domain Models (domain)"]:::domain
    end

    %% Data Access Layer
    subgraph "Data Access Layer"
        Repos["Repositories (repositories)"]:::repository
        ORM["ORM (orm)"]:::repository
        MySQL[(MySQL Database)]:::mysql
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        RedisQueue["Redis Queue (infrastructure/queue)"]:::infrastructure
        Storage["Storage Services (infrastructure/storage)"]:::infrastructure
    end

    %% Supporting Components
    subgraph "Supporting Components"
        Config["Configuration (configs)"]:::config
        Utilities["Utilities (utils)"]:::config
    end

    %% Connections
    Client -->|"HTTPRequest"| API
    API -->|"APICall"| Service
    Service -->|"ExecutesLogic"| Domain
    Service -->|"CallsRepos"| Repos
    Repos -->|"UsesORM"| ORM
    ORM -->|"CRUDOperations"| MySQL
    Service -->|"EnqueuesTasks"| RedisQueue
    Service -->|"AccessStorage"| Storage
    Service -->|"Integrates"| External

    %% Supporting interactions (dotted lines)
    Config -.->|"supports"| API
    Config -.->|"supports"| Service
    Config -.->|"supports"| Domain
    Config -.->|"supports"| Repos
    Config -.->|"supports"| RedisQueue
    Utilities -.->|"assists"| Service

    %% Click Events
    click API "https://github.com/codevalley/friday-api/tree/main/routers/v1"
    click Service "https://github.com/codevalley/friday-api/tree/main/services"
    click Domain "https://github.com/codevalley/friday-api/tree/main/domain"
    click ORM "https://github.com/codevalley/friday-api/tree/main/orm"
    click Repos "https://github.com/codevalley/friday-api/tree/main/repositories"
    click RedisQueue "https://github.com/codevalley/friday-api/tree/main/infrastructure/queue"
    click Storage "https://github.com/codevalley/friday-api/tree/main/infrastructure/storage"
    click Config "https://github.com/codevalley/friday-api/tree/main/configs"
    click Utilities "https://github.com/codevalley/friday-api/tree/main/utils"

    %% Styles
    classDef client fill:#f9d5e5,stroke:#333,stroke-width:2px;
    classDef api fill:#d0e6f7,stroke:#333,stroke-width:2px;
    classDef service fill:#ffebcc,stroke:#333,stroke-width:2px;
    classDef domain fill:#d5e8d4,stroke:#333,stroke-width:2px;
    classDef repository fill:#ffe6e6,stroke:#333,stroke-width:2px;
    classDef mysql fill:#ffcccc,stroke:#333,stroke-width:2px;
    classDef infrastructure fill:#e4d7f7,stroke:#333,stroke-width:2px;
    classDef config fill:#f7d0d0,stroke:#333,stroke-width:2px;
    classDef ext fill:#deeedd,stroke:#333,stroke-width:2px;
```
