# afaanbilal/moneyflow architecture

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

## Overview

##### Main Components
- REST API Server: Represent the Actix Web server as the gateway. Note that the project listens on a configurable host/port (set to 0.0.0.0 and port 80 for Docker).
- Controllers: Modules under src/controllers (for authentication, category management, transactions, and user data via the “me” endpoint) should each be viewed as distinct API entry points that handle HTTP requests/responses.
- Business/Database Layer: The controllers call into database modules (inside src/db) to perform CRUD operations and execute SQL queries (note the SQLx queries stored in the .sqlx folder).
- Middleware: The authentication middleware (src/middleware/auth.rs) intercepts requests for endpoints that require JWT validation. This component is essential for maintaining security.
- Database: The underlying relational database, which is configured by the DATABASE_URL environment variable. Include migrations (found in the migrations directory) that set up tables for users, categories, and transactions.
- Authentication and Security: A dedicated component for processing JWT tokens (using a secret provided as an environment variable) and managing the authentication flow.
- Docker Container: Since the project includes a Dockerfile and instructions to run via Docker, include a container representation that encapsulates the entire API server.
- External Clients: Represent clients (web/mobile applications or API consumers) which interact with the REST endpoints.

##### Out the Relationships and Interactions
- External clients send HTTP requests (with or without a JWT token) to the Actix server.
- The Actix routing layer directs requests to specific controllers based on the API endpoints (e.g., /auth, /api/categories, /api/transactions).
- Requests that require authentication are intercepted by the JWT middleware. The middleware verifies token validity before passing the request to the controller.
- Controllers invoke database functions (from modules in src/db) to interact with the database and perform CRUD operations.
- The server, upon completion of a request, returns HTTP responses (including tokens on authentication events in /auth/sign-in).
- Docker encapsulates the server environment and binds the correct ports, ensuring the application is served from an isolated environment.

##### Important Architectural Patterns & Technologies
- MVC-like Separation: Controllers (handling HTTP requests) are logically separated from the business logic (inside the db modules) and middleware (for authentication).
- RESTful API: Emphasis is on well-defined REST endpoints for resources like users, categories, and transactions.
- Containerization: Docker is used to guarantee consistency across environments.
- Environment Configurations: Important environment variables (DATABASE_URL, JWT_SECRET) and the use of migrations for database initialization.
- Rust Ecosystem: Utilizing Actix Web for asynchronous web server capabilities and SQLx for compile-time checked SQL queries.

##### Specific Elements
- Controllers (auth, me, categories, transactions).
- Middleware (“Auth Middleware”) for JWT token verification.
- A “Docker Container” outline encapsulating the API server, demonstrating the deployment and configuration (showing environment variables like DATABASE_URL and JWT_SECRET).
- The direction of the HTTP request flow from the client to the API server.
- The validation step via middleware.
- The invocation of database functions by the controllers.
- The flow of HTTP responses back from the server to the client.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Client
    EC("External Client"):::external

    %% Docker Container encapsulating API Server
    DC("Docker Container"):::container
    click DC "https://github.com/afaanbilal/moneyflow/tree/master/Dockerfile"

    %% API Server (Actix Web)
    API("API Server (Actix Web)"):::api
    click API "https://github.com/afaanbilal/moneyflow/blob/master/src/main.rs"

    %% JWT Middleware Component
    JWT("JWT Middleware Component"):::middleware
    click JWT "https://github.com/afaanbilal/moneyflow/blob/master/src/middleware/auth.rs"

    %% Controllers Subgraph
    subgraph "Controllers Layer"
        AC("Auth Controller"):::controller
        click AC "https://github.com/afaanbilal/moneyflow/blob/master/src/controllers/auth.rs"
        CC("Categories Controller"):::controller
        click CC "https://github.com/afaanbilal/moneyflow/blob/master/src/controllers/categories.rs"
        TC("Transactions Controller"):::controller
        click TC "https://github.com/afaanbilal/moneyflow/blob/master/src/controllers/transactions.rs"
        MC("User/Me Controller"):::controller
        click MC "https://github.com/afaanbilal/moneyflow/blob/master/src/controllers/me.rs"
    end

    %% Database Access Layer Subgraph
    subgraph "Database Access Layer (SQLx)"
        UDM("User DB Module"):::db
        click UDM "https://github.com/afaanbilal/moneyflow/blob/master/src/db/user.rs"
        CDM("Categories DB Module"):::db
        click CDM "https://github.com/afaanbilal/moneyflow/blob/master/src/db/categories.rs"
        TDM("Transactions DB Module"):::db
        click TDM "https://github.com/afaanbilal/moneyflow/blob/master/src/db/transactions.rs"
        SQX("SQLx Query Cache"):::misc
        click SQX "https://github.com/afaanbilal/moneyflow/blob/master/.sqlx"
    end

    %% Relational Database and Migration Scripts
    DB("Relational Database"):::database
    DMS("Database Migration Scripts"):::misc
    click DMS "https://github.com/afaanbilal/moneyflow/tree/master/migrations"

    %% Vertical Flow Connections
    EC -->|"HTTP request"| DC
    DC -->|"Deploys"| API
    API -->|"validates"| JWT
    JWT -->|"routes to"| AC
    JWT -->|"routes to"| CC
    JWT -->|"routes to"| TC
    JWT -->|"routes to"| MC
    API -->|"routes to"| AC
    API -->|"routes to"| CC
    API -->|"routes to"| TC
    API -->|"routes to"| MC

    AC -->|"calls"| UDM
    CC -->|"calls"| CDM
    TC -->|"calls"| TDM
    MC -->|"calls"| UDM

    UDM -->|"CRUD Queries"| DB
    CDM -->|"CRUD Queries"| DB
    TDM -->|"CRUD Queries"| DB

    DB -->|"set up via"| DMS

    SQX --- UDM
    SQX --- CDM
    SQX --- TDM

    %% Styles
    classDef external fill:#cce5ff,stroke:#004085,stroke-width:2px,stroke-dasharray: 5 5;
    classDef container fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef api fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef middleware fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef controller fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
    classDef db fill:#e2e3e5,stroke:#41464b,stroke-width:2px;
    classDef database fill:#ffeeba,stroke:#856404,stroke-width:2px, shape: cylinder;
    classDef misc fill:#f5c6cb,stroke:#721c24,stroke-width:2px;
```
