# kitae0522/thread-be-rs architecture

> Architecture diagram of the GitHub repository kitae0522/thread-be-rs, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kitae0522/thread-be-rs
- Repository: https://github.com/kitae0522/thread-be-rs

## Overview

##### Main Components
- API Layer: Represent the controller layer (folder: src/api) which defines endpoints and routes. This layer includes various handlers (e.g., auth_handlers, thread_handlers, user_handlers, etc.), middleware (auth and logging), and the server startup logic (src/api/server.rs) along with state management (state.rs).
- Configuration Module: Capture the environment and application settings (folder: src/config), including the environment variable configurations.
- Domain Layer: Illustrate the data transfer objects (DTOs) and database models (folder: src/domain) that represent the core business entities (thread, user, follow, votes) as well as their representations.
- Repository Layer: Outline the data access layer (folder: src/repository) that abstracts database operations against various data models, including views and SQL migrations.
- Service Layer: Emphasize the business logic encapsulated in the services (folder: src/services) which orchestrates operations across the domain and repository layers.
- Utility Functions: Indicate helper functions (folder: src/utils) for things like crypto and cursor management.
- Error Management: Mark the custom error handling (src/error.rs) component.
- Database Migrations: Notate the migration SQL files (folder: migrations) which represent the schema and dummy data setup.

##### Relationships and Interactions
- That the API Layer (handlers and routes) processes incoming HTTP requests, which first pass through Middleware (like JWT authentication and logging).
- After validation, controller handlers delegate requests to the Service Layer, which contains business logic.
- Services interact with the Repository Layer for persistent CRUD operations on data stored in the database.
- The Domain Layer is used to represent and structure data both on the API input/output and repository interactions.
- Configuration and utility modules support all layers by providing environment data and reusable helper functions.
- Database interactions are further guided by the SQL migrations for setting up and evolving the database schema.

##### Architectural Patterns and Design Principles
- The project clearly follows an MVC-like or layered architecture, separating concerns into controllers (API), services (business logic), and repositories (data access).
- Middleware separation ensures concerns like authentication and logging are centrally managed.
- Use of DTOs and domain models suggests the project embraces clean architecture principles, making the logic modular and testable.

##### Relevant Technologies and Frameworks
- Rust as the programming language.
- Axum for the HTTP server and routing.
- Sqlx for database handling and asynchronous operations.
- Docker Compose for containerized deployments (as indicated by the docker-compose.yml file).
- Git and associated hooks (from the .setup_git folder) for enforcing code quality standards.

## Diagram (Mermaid)

```mermaid
graph TD
    Client["Client Request"]:::client
    Main["Main (src/main.rs)"]:::main
    Client --> Main

    %% API Layer Components
    Srv["Server Startup (src/api/server.rs)"]:::api
    St["State Management (src/api/state.rs)"]:::api
    Mdw["Middleware (src/api/middleware)"]:::api
    Hdl["Handlers (src/api/handlers)"]:::api
    Rt["Routes (src/api/routes)"]:::api

    Main --> Srv
    Srv --> St
    St --> Mdw
    Mdw --> Hdl
    Mdw --> Rt

    %% Transition to Service Layer
    Mdw -->|"processes"| SL["Service Layer (src/services)"]:::services

    subgraph "Service Layer"
        S1["Thread Service (src/services/thread_service.rs)"]:::services
        S2["User Service (src/services/user_service.rs)"]:::services
        S3["Follow Service (src/services/follow_service.rs)"]:::services
        S4["Votes Service (src/services/votes_service.rs)"]:::services
        S1 --> S2
        S2 --> S3
        S3 --> S4
    end
    SL --> S1

    %% Transition to Repository Layer
    S4 --> RL["Repository Layer (src/repository)"]:::repo

    subgraph "Repository Layer"
        R1["Thread Repo (src/repository/thread_repo.rs)"]:::repo
        R2["User Repo (src/repository/user_repo.rs)"]:::repo
        R3["Follow Repo (src/repository/follow_repo.rs)"]:::repo
        R4["Votes Repo (src/repository/votes_repo.rs)"]:::repo
        R5["Views Repo (src/repository/views_repo.rs)"]:::repo
        R1 --> R2
        R2 --> R3
        R3 --> R4
        R4 --> R5
    end
    RL --> R1

    %% Database and Migrations
    R5 --> DB["Database"]:::database
    DB --> DM["Database Migrations (migrations)"]:::migrations

    %% Supporting Components (dashed connectors)
    Srv -.-> Cfg["Configuration (src/config)"]:::config
    Srv -.-> Util["Utility Functions (src/utils)"]:::util
    Hdl -.-> Dom["Domain Layer (src/domain)"]:::domain
    S1 -.-> Dom
    Mdw -.-> EM["Error Management (src/error.rs)"]:::error

    %% Click Events
    click Main "https://github.com/kitae0522/thread-be-rs/blob/main/src/main.rs"
    click Srv "https://github.com/kitae0522/thread-be-rs/blob/main/src/api/server.rs"
    click St "https://github.com/kitae0522/thread-be-rs/blob/main/src/api/state.rs"
    click Hdl "https://github.com/kitae0522/thread-be-rs/tree/main/src/api/handlers/"
    click Rt "https://github.com/kitae0522/thread-be-rs/tree/main/src/api/routes/"
    click Mdw "https://github.com/kitae0522/thread-be-rs/tree/main/src/api/middleware/"
    click Cfg "https://github.com/kitae0522/thread-be-rs/blob/main/src/config/env.rs"
    click Dom "https://github.com/kitae0522/thread-be-rs/tree/main/src/domain"
    click R1 "https://github.com/kitae0522/thread-be-rs/blob/main/src/repository/thread_repo.rs"
    click R2 "https://github.com/kitae0522/thread-be-rs/blob/main/src/repository/user_repo.rs"
    click R3 "https://github.com/kitae0522/thread-be-rs/blob/main/src/repository/follow_repo.rs"
    click R4 "https://github.com/kitae0522/thread-be-rs/blob/main/src/repository/votes_repo.rs"
    click R5 "https://github.com/kitae0522/thread-be-rs/blob/main/src/repository/views_repo.rs"
    click S1 "https://github.com/kitae0522/thread-be-rs/blob/main/src/services/thread_service.rs"
    click S2 "https://github.com/kitae0522/thread-be-rs/blob/main/src/services/user_service.rs"
    click S3 "https://github.com/kitae0522/thread-be-rs/blob/main/src/services/follow_service.rs"
    click S4 "https://github.com/kitae0522/thread-be-rs/blob/main/src/services/votes_service.rs"
    click Util "https://github.com/kitae0522/thread-be-rs/tree/main/src/utils"
    click EM "https://github.com/kitae0522/thread-be-rs/blob/main/src/error.rs"
    click DM "https://github.com/kitae0522/thread-be-rs/tree/main/migrations"

    %% Styles
    classDef client fill:#f4e842,stroke:#333,stroke-width:2px;
    classDef main fill:#A9CCE3,stroke:#1B4F72,stroke-width:2px;
    classDef api fill:#85C1E9,stroke:#1B4F72,stroke-width:2px;
    classDef services fill:#82E0AA,stroke:#196F3D,stroke-width:2px;
    classDef repo fill:#F5B041,stroke:#B9770E,stroke-width:2px;
    classDef domain fill:#AED6F1,stroke:#1F618D,stroke-width:2px;
    classDef config fill:#BB8FCE,stroke:#8E44AD,stroke-width:2px;
    classDef util fill:#D5DBDB,stroke:#808B96,stroke-width:2px;
    classDef error fill:#F1948A,stroke:#922B21,stroke-width:2px;
    classDef migrations fill:#BDC3C7,stroke:#7D7D7D,stroke-width:2px;
    classDef database fill:#D1F2EB,stroke:#117864,stroke-width:2px;
```
