# mamangrust/payment-gateway-axum architecture

> Architecture diagram of the GitHub repository mamangrust/payment-gateway-axum, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mamangrust/payment-gateway-axum
- Repository: https://github.com/mamangrust/payment-gateway-axum

## Overview

##### Main Components

A. API Layer (Axum Web Server)
- Entry point through main.rs
- Handler layer (/src/handler/*)
- Middleware layer (/src/middleware/*)

B. Business Logic Layer
- Services layer (/src/services/*)
- Abstract traits (/src/abstract_trait/*)
- Domain models
- Request models (/src/domain/request/*)
- Response models (/src/domain/response/*)

C. Data Access Layer
- Repository layer (/src/repository/*)
- Entities (/src/entities/*)
- Database migrations (/migration/*)

D. Cross-Cutting Concerns
- Configuration (/src/config/*)
- Utils (/src/utils/*)
- State management (/src/state.rs)

- [Client/External Systems]
- ↓
- [API Layer (Axum)]
- ↓
- [Business Logic Layer]
- ↓
- [Data Access Layer]
- ↓
- [Database]

##### Component Relationships

- HTTP requests flow through middleware (JWT authentication) to handlers
- Handlers communicate with services
- Services implement abstract traits
- Services use repositories for data access
- Repositories work with entities
- All components can access utils and config

##### Specific Features to Highlight

Financial Operations:
- Top-up flow
- Transfer flow
- Withdrawal flow
- Balance (Saldo) management
- User management
- Authentication/Authorization

##### Technical Details

Framework & Libraries:
- Axum (Web Framework)
- Sea-ORM (Database ORM)
- JWT Authentication
- Database Migrations

##### Additional Elements

Supporting components:
- Error handling system (/src/utils/errors.rs)
- Dependency injection system (/src/utils/di.rs)
- Logging system (/src/utils/log_tracing.rs)
- Payment validation (/src/utils/payment_method_validator.rs)

##### Testing Architecture

A separate section for:
- Integration tests
- Unit tests
- Repository tests
- Service tests

##### Clear boundaries between components

Remember to:
- Notes for critical components
- Both synchronous and asynchronous flows
- Security boundaries
- Error handling paths

This architecture follows a typical clean architecture pattern with clear separation of concerns, making it important to show the independence of business logic from external concerns while maintaining clear dependency rules flowing inward from controllers through use cases to entities.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph External
        Client[Client]:::external
    end

    subgraph API_Layer["API Layer (Axum)"]
        Main["Main Entry Point"]:::api
        Handlers["Handlers"]:::api
        AuthHandler["Authentication Handler"]:::api
        JWTMiddleware["JWT Middleware"]:::api
    end

    subgraph Business_Logic["Business Logic Layer"]
        Services["Services"]:::business
        AbstractTraits["Abstract Traits"]:::business
        DomainModels["Domain Models"]:::business
        subgraph RequestResponse["Models"]
            RequestModels["Request Models"]:::business
            ResponseModels["Response Models"]:::business
        end
    end

    subgraph Data_Access["Data Access Layer"]
        Repositories["Repositories"]:::data
        Entities["Entities"]:::data
        Migrations["Database Migrations"]:::data
    end

    subgraph CrossCutting["Cross-Cutting Concerns"]
        Config["Configuration"]:::infra
        Utils["Utilities"]:::infra
        State["State Management"]:::infra
        ErrorHandling["Error Handling"]:::infra
        DI["Dependency Injection"]:::infra
        Logging["Logging System"]:::infra
    end

    subgraph Financial_Ops["Financial Operations"]
        direction TB
        TopupOp["Top-up Flow"]:::financial
        TransferOp["Transfer Flow"]:::financial
        WithdrawOp["Withdrawal Flow"]:::financial
    end

    subgraph Testing["Testing Architecture"]
        IntegrationTests["Integration Tests"]:::test
        UnitTests["Unit Tests"]:::test
    end

    Database[(Database)]:::database

    %% Relationships
    Client --> Main
    Main --> JWTMiddleware
    JWTMiddleware --> Handlers
    Handlers --> Services
    Services --> AbstractTraits
    Services --> Repositories
    Repositories --> Entities
    Entities --> Database

    %% Cross-cutting relationships
    Config -.-> Services
    Utils -.-> Services
    State -.-> Services
    ErrorHandling -.-> Services
    DI -.-> Services
    Logging -.-> Services

    %% Financial Operations Flow
    Handlers --> Financial_Ops
    Financial_Ops --> Services

    %% Testing relationships
    IntegrationTests -.-> API_Layer
    UnitTests -.-> Business_Logic

    %% Click events
    click Main "https://github.com/MamangRust/payment-gateway-axum/blob/main/src/main.rs"
    click Handlers "https://github.com/MamangRust/payment-gateway-axum/tree/main/src/handler/"
    click AuthHandler "https://github.com/MamangRust/payment-gateway-axum/blob/main/src/handler/auth.rs"
    click JWTMiddleware "https://github.com/MamangRust/payment-gateway-axum/blob/main/src/middleware/jwt.rs"
    click Services "https://github.com/MamangRust/payment-gateway-axum/tree/main/src/services/"
    click AbstractTraits "https://github.com/MamangRust/payment-gateway-axum/tree/main/src/abstract_trait/"
    click RequestModels "https://github.com/MamangRust/payment-gateway-axum/tree/main/src/domain/request/"
    click ResponseModels "https://github.com/MamangRust/payment-gateway-axum/tree/main/src/domain/response/"
    click Repositories "https://github.com/MamangRust/payment-gateway-axum/tree/main/src/repository/"
    click Entities "https://github.com/MamangRust/payment-gateway-axum/tree/main/src/entities/"
    click Migrations "https://github.com/MamangRust/payment-gateway-axum/tree/main/migration/src/"
    click Config "https://github.com/MamangRust/payment-gateway-axum/tree/main/src/config/"
    click Utils "https://github.com/MamangRust/payment-gateway-axum/tree/main/src/utils/"
    click State "https://github.com/MamangRust/payment-gateway-axum/blob/main/src/state.rs"
    click ErrorHandling "https://github.com/MamangRust/payment-gateway-axum/blob/main/src/utils/errors.rs"
    click DI "https://github.com/MamangRust/payment-gateway-axum/blob/main/src/utils/di.rs"
    click Logging "https://github.com/MamangRust/payment-gateway-axum/blob/main/src/utils/log_tracing.rs"
    click IntegrationTests "https://github.com/MamangRust/payment-gateway-axum/tree/main/tests/integration_test/"
    click UnitTests "https://github.com/MamangRust/payment-gateway-axum/tree/main/tests/unit_test/"

    %% Styles
    classDef external fill:#gray,stroke:#333,stroke-width:2px
    classDef api fill:#3498db,stroke:#333,stroke-width:1px
    classDef business fill:#2ecc71,stroke:#333,stroke-width:1px
    classDef data fill:#e67e22,stroke:#333,stroke-width:1px
    classDef infra fill:#95a5a6,stroke:#333,stroke-width:1px
    classDef financial fill:#9b59b6,stroke:#333,stroke-width:1px
    classDef test fill:#f1c40f,stroke:#333,stroke-width:1px
    classDef database fill:#34495e,stroke:#333,stroke-width:1px
```
