# mifaly/new_ae_server architecture

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

- Interactive diagram: https://gitdiagram.com/mifaly/new_ae_server
- Repository: https://github.com/mifaly/new_ae_server

## Overview

##### Project type and purpose
- This is a Rust-based RESTful backend service (no frontend) whose primary functions appear to be CRUD operations on “products,” “orders,” and “offers,” backed by a SQL database. It also exposes a shutdown endpoint used by an external scheduled task (see ReadMe.txt).

##### File structure
- Cargo.toml / Cargo.lock: Rust project metadata and dependencies.
- config.json5: runtime configuration (e.g. DB credentials, port).
- src/main.rs: entry point—HTTP server setup.
- src/routes/*.rs: route definitions and request handlers for products, orders, offers.
- src/models.rs and src/types.rs: domain models and shared data types.
- src/tables.sql: initial schema/migrations for the relational database.
- ReadMe.txt: a note to schedule “curl -k https://ae.helper.com:5499/stop/0” on shutdown.

##### README for additional insights
- The only instruction is to schedule a shutdown call to the `/stop/0` endpoint. This indicates an orchestration concern with an external AE Helper service.

a. Main components
- HTTP Server (Rust executable)
- Routing Module (offers, orders, products, stop)
- Business Logic / Models Layer
- SQL Database (PostgreSQL/MySQL)
- Configuration Store (config.json5)
- External Scheduler / Orchestrator (invokes shutdown endpoint)

b. Relationships & interactions
- Clients → HTTP Server: REST calls to `/products`, `/orders`, `/offers`, and `/stop/0`.
- HTTP Server → Routing Module: dispatch to appropriate handler.
- Handlers → Models Layer: transform requests into domain objects.
- Models Layer → Database: SQL queries per tables.sql.
- On shutdown: External Scheduler → HTTP Server `/stop/0` → triggers graceful shutdown.

c. Architectural patterns & design principles
- Layered architecture (API → Routing → Business Logic → Data Access).
- Separation of concerns: route handlers vs. data models vs. DB schema.
- Configuration externalized in JSON5 for env-specific overrides.
- Graceful shutdown endpoint for orchestration.

d. Key technologies
- Rust (actix-web or Rocket or similar)
- Diesel or another ORM (implied by tables.sql + models.rs)
- JSON5 for config parsing
- SQL database (Postgres/MySQL)
- Curl-based scheduler / systemd cron job

- Step 2: Place a big rectangle labeled “Rust HTTP Server” in the center.
- Inside it, subdivide into:
– Router Layer (src/routes/*.rs)
– Business Logic Layer (src/models.rs, src/types.rs)
– Data Access Layer (DB connection using tables.sql schema)
- Step 6: Annotate the `/products`, `/orders`, `/offers` flows from client through router to DB and back.
- Blue for HTTP-related components
- Green for Business Logic/Models
- Orange for external scheduler

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Entities
    Client["Client"]:::external
    ExternalScheduler["Scheduler / systemd cron"]:::scheduler

    %% Configuration and Metadata
    Config["config.json5"]:::config
    CargoMeta["Cargo.toml & Cargo.lock"]:::config

    %% Main Server Component
    subgraph "Rust HTTP Server"
        direction TB
        Main["HTTP Server\n(src/main.rs)"]:::http
        subgraph "Routing Module"
            direction TB
            RoutesMod["mod.rs"]:::http
            OffersRoute["offers.rs"]:::http
            OrdersRoute["orders.rs"]:::http
            ProductsRoute["products.rs"]:::http
        end
        subgraph "Business Logic"
            direction TB
            Models["models.rs"]:::logic
            Types["types.rs"]:::logic
        end
        subgraph "Data Access Layer"
            direction TB
            Schema["tables.sql"]:::dbaccess
        end
    end

    %% Database
    Database["SQL Database"]:::db

    %% Connections
    Client -->|"REST calls"| Main
    Main --> RouterCall[/"Dispatch to Routing Module"/]:::http
    RouterCall --> RoutesMod
    RoutesMod --> OffersRoute
    RoutesMod --> OrdersRoute
    RoutesMod --> ProductsRoute
    OffersRoute --> BusinessCall1[/"Handle Request"/]:::http
    OrdersRoute --> BusinessCall2[/"Handle Request"/]:::http
    ProductsRoute --> BusinessCall3[/"Handle Request"/]:::http
    BusinessCall1 --> Models
    BusinessCall2 --> Models
    BusinessCall3 --> Models
    Models --> Types
    Types --> DataAccessCall[/"Prepare Queries"/]:::logic
    DataAccessCall --> Schema
    Schema -->|"Executes SQL"| Database

    ExternalScheduler -->|"calls /stop/0"| Main

    Config -->|"Loads config at startup"| Main
    CargoMeta -->|"Project metadata & deps"| Main

    %% Click Events
    click Main "https://github.com/mifaly/new_ae_server/blob/main/src/main.rs"
    click RoutesMod "https://github.com/mifaly/new_ae_server/blob/main/src/routes/mod.rs"
    click OffersRoute "https://github.com/mifaly/new_ae_server/blob/main/src/routes/offers.rs"
    click OrdersRoute "https://github.com/mifaly/new_ae_server/blob/main/src/routes/orders.rs"
    click ProductsRoute "https://github.com/mifaly/new_ae_server/blob/main/src/routes/products.rs"
    click Models "https://github.com/mifaly/new_ae_server/blob/main/src/models.rs"
    click Types "https://github.com/mifaly/new_ae_server/blob/main/src/types.rs"
    click Schema "https://github.com/mifaly/new_ae_server/blob/main/src/tables.sql"
    click Config "https://github.com/mifaly/new_ae_server/blob/main/config.json5"
    click ExternalScheduler "https://github.com/mifaly/new_ae_server/blob/main/ReadMe.txt"
    click CargoMeta "https://github.com/mifaly/new_ae_server/blob/main/Cargo.toml"
    click CargoMeta "https://github.com/mifaly/new_ae_server/blob/main/Cargo.lock"

    %% Styles
    classDef external fill:#f0f0f0,stroke:#333,stroke-width:1px
    classDef scheduler fill:#ffe4b5,stroke:#cc9600,stroke-width:2px
    classDef config fill:#fafad2,stroke:#b5a642,stroke-width:1px,stroke-dasharray: 5 5
    classDef http fill:#cce5ff,stroke:#339af0,stroke-width:2px
    classDef logic fill:#d4edda,stroke:#28a745,stroke-width:2px
    classDef dbaccess fill:#e2e3e5,stroke:#6c757d,stroke-width:1px,stroke-dasharray: 2 2
    classDef db fill:#f8d7da,stroke:#dc3545,stroke-width:2px
```
