# yihaozhadan/http-proxy architecture

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

- Interactive diagram: https://gitdiagram.com/yihaozhadan/http-proxy
- Repository: https://github.com/yihaozhadan/http-proxy

## Overview

##### Main components
- Client Applications: These are the external HTTP clients that send POST requests (to endpoints like /delay and /failure).
- Request Handler: Responsible for parsing incoming HTTP requests and headers.
- Delay Simulator: Logic that implements constant and/or random delay configurations.
- Failure Simulator: Logic that uses the configured success probability (or failure rate) to simulate failures.
- Forwarder/Proxy Module: Forwards HTTP/HTTPS requests to the target URL (configured via environment variables or headers).
- Logger & Error Reporter: Captures detailed error reporting and logs for both successful and simulated-failure paths.
- Target Server: The external service to which the HTTP proxy forwards requests (e.g., https://httpbin.org/post or any custom URL via the “X-Proxy-Url” header).
- Configuration & Environment Components: Utilize environment variables (like TARGET_URL and SUCCESS_PROBABILITY) and dynamic header overrides to configure the behavior of the proxy.

##### Relationships and interactions
- How the Client sends a POST request to the HTTP Proxy.
- The Proxy’s internal decision process splitting between the delay simulation or the failure simulation endpoints.
- How the delay and failure simulation logic intervenes before passing the request to the Forwarder.
- The forwarded request from the proxy to the Target Server and the reverse flow of the response/error back to the client.
- That configuration comes from both static (environment variables) and dynamic (HTTP headers) sources, influencing the processing logic.

##### Architectural patterns and design principles
- Separation of concerns: the logic for delay vs. failure simulations is distinct from the core forwarding mechanism.
- That the system follows a proxy/middleware pattern where incoming requests are processed, potentially modified, and then relayed.
- That this is a microservice-like design with the possibility of containerized deployment (using Docker).

##### Technologies and specific tools
- Rust, as indicated by Cargo.toml, is the language for implementation.
- Docker and docker-compose for containerized deployment.
- Node.js script in the “perf-test” directory (failure-rate.js) which is likely used to simulate load or test failure rates.
- Environment Variable management tools (like .env files) for local development.

- Clearly separate the client, proxy, and target server components.
- For each path (delay simulation and failure simulation), illustrate how the configuration parameters (CUSTOM delays, success probabilities, header overrides) are applied.
- If desired, include a subsection for Docker-based deployment to illustrate how external requests enter via the container and invoke the service.

- Use clear labels for each component (e.g., “HTTP Proxy Service”, “Delay Simulator”, “Failure Simulator”, “Request Forwarder”, “Client”, “Target Server”, “Configuration Module”).
- From the Client to the HTTP Proxy (incoming HTTP POST).
- From the HTTP Proxy’s decision components (delay and failure modules) to the Request Forwarder.
- From the Request Forwarder to the Target Server.
- A separate node for Docker indicating container boundaries, which links into development and deployment practices.

##### Putting it all together
- Inside the HTTP Proxy, split out the relevant modules (request parsing, delay simulation, failure simulation, forwarding, logging/error handling).
- Annotations for configuration settings derived from environment variables and/or HTTP headers—indicating how they alter the behavior for endpoints /delay and /failure.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Components
    Client("Client Applications"):::external
    PerfTest("Load Tester"):::external
    Target("Target Server"):::external

    %% Docker Deployment Components
    DockerDeployment("Docker Deployment"):::docker
    DockerfileNode("Dockerfile"):::docker
    DockerComposeNode("docker-compose.yml"):::docker

    %% Docker Container containing the HTTP Proxy Service and its Internal Modules
    subgraph "Docker Container"
        HTTPProxy("HTTP Proxy Service"):::internal
        subgraph "Internal Modules"
            RequestHandler("Request Handler"):::internal
            DelaySimulator("Delay Simulator"):::internal
            FailureSimulator("Failure Simulator"):::internal
            RequestForwarder("Request Forwarder"):::internal
            Logger("Logger & Error Reporter"):::internal
            ConfigModule("Configuration Module"):::internal
        end
        %% Internal flow within the HTTP Proxy Service
        HTTPProxy --> RequestHandler
        RequestHandler --"/delay"--> DelaySimulator
        RequestHandler --"/failure"--> FailureSimulator
        DelaySimulator --> RequestForwarder
        FailureSimulator --> RequestForwarder
        RequestForwarder --> Logger
        ConfigModule --"config"--> DelaySimulator
        ConfigModule --"config"--> FailureSimulator
        RequestForwarder --"Forward request"--> Target
    end

    %% External interactions
    Client --"POST /delay_or_/failure"--> HTTPProxy
    PerfTest --"simulate load"--> HTTPProxy
    Target --"Response"--> Client

    %% Docker Deployment arrow (deployment relation)
    DockerDeployment --"runs"--> HTTPProxy
    DockerDeployment --- DockerfileNode
    DockerDeployment --- DockerComposeNode

    %% Click Events
    click HTTPProxy "https://github.com/yihaozhadan/http-proxy/blob/main/src/main.rs"
    click RequestHandler "https://github.com/yihaozhadan/http-proxy/blob/main/src/main.rs"
    click DelaySimulator "https://github.com/yihaozhadan/http-proxy/blob/main/src/main.rs"
    click FailureSimulator "https://github.com/yihaozhadan/http-proxy/blob/main/src/main.rs"
    click RequestForwarder "https://github.com/yihaozhadan/http-proxy/blob/main/src/main.rs"
    click Logger "https://github.com/yihaozhadan/http-proxy/blob/main/src/main.rs"
    click ConfigModule "https://github.com/yihaozhadan/http-proxy/blob/main/Cargo.toml"
    click DockerDeployment "https://github.com/yihaozhadan/http-proxy/tree/main/Dockerfile"
    click DockerfileNode "https://github.com/yihaozhadan/http-proxy/tree/main/Dockerfile"
    click DockerComposeNode "https://github.com/yihaozhadan/http-proxy/blob/main/docker-compose.yml"
    click PerfTest "https://github.com/yihaozhadan/http-proxy/blob/main/perf-test/failure-rate.js"

    %% Styles
    classDef external fill:#FCE4D6,stroke:#D35400,stroke-width:2px;
    classDef internal fill:#D6EAF8,stroke:#2874A6,stroke-width:2px;
    classDef docker fill:#D5F5E3,stroke:#28B463,stroke-width:2px;
```
