# anubhav126/reverse-proxy architecture

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

- Interactive diagram: https://gitdiagram.com/anubhav126/reverse-proxy
- Repository: https://github.com/anubhav126/reverse-proxy

## Overview

##### Main Components
- Primary Process (Master): This node is responsible for setting up the core HTTP server, spawning worker processes, and handling inter-process communication.
- Worker Processes: These are the child processes that deal with individual HTTP requests, performing routing based on configuration rules, proxying requests to upstream servers, and validating messages.
- Configuration Module: Includes configuration files (config.yaml and the schema-related files in both src/ and dist/ directories) that determine routing rules and proxy targets.
- External Dependencies: Include Node.js core modules (such as cluster and HTTP), as well as external libraries listed in package.json.
- Upstream Servers: Although not directly implemented in the code, these represent external services to which the load balancer proxies incoming requests.

##### Relationships and Interactions
- The Primary Process initializes the system, loads the configuration, and then spawns multiple Worker Processes using Node’s clustering capabilities.
- Worker Processes then engage in routing and proxying requests to the appropriate Upstream Servers based on the configuration rules.

##### Architectural Patterns and Design Principles
- Multi-Process Architecture using Node.js clustering to improve resource usage.
- Reverse Proxy Design: Directing incoming requests to the appropriate upstream server.
- Configuration-Driven Routing: Emphasis on schema validation for both server and configuration settings to ensure robustness.
- Use of TypeScript to ensure type safety, shown by the presence of tsconfig.json and .ts files.

##### Relevant Technologies/Frameworks
- Node.js (built-in cluster, HTTP modules)
- TypeScript for type safety
- JSON/Schema validation libraries (inferred from config-schema and server-schema files)
- Possibly external HTTP proxy libraries if implemented

##### Elements
- Incoming HTTP requests first hitting the Primary Process.
- The distribution of requests from the Primary Process to each Worker Process (round-robin).
- Worker Processes forwarding the requests to appropriate Upstream Servers.
- Callout sections for the schema validation steps (configuration and server schema) and note where error handling comes into play.

## Diagram (Mermaid)

```mermaid
graph TD
    A["HTTP Client"]
    B["Primary Process (Master)"]
    C["Worker Process"]
    D["Upstream Servers"]
    J["External Dependencies (package.json)"]
    
    A -->|"sends_HTTP_request"| B
    B -->|"routes_request_roundrobin"| C
    C -->|"proxies_request"| D
    B -->|"uses"| J
    C -->|"uses"| J

    subgraph "Configuration Module"
        F1["Configuration File (config.yaml)"]
        F2["Configuration Implementation (config.ts)"]
        F3["Config Schema (config-schema.ts)"]
        F4["Server Schema (server-schema.ts)"]
    end

    B -->|"loads_config"| F2
    C -->|"loads_config"| F2
    F2 -->|"validates_with"| F3
    F2 -->|"validates_with"| F4

    click B "https://github.com/anubhav126/reverse-proxy/blob/main/src/index.ts"
    click C "https://github.com/anubhav126/reverse-proxy/blob/main/src/server.ts"
    click F1 "https://github.com/anubhav126/reverse-proxy/blob/main/config.yaml"
    click F2 "https://github.com/anubhav126/reverse-proxy/blob/main/src/config.ts"
    click F3 "https://github.com/anubhav126/reverse-proxy/blob/main/src/config-schema.ts"
    click F4 "https://github.com/anubhav126/reverse-proxy/blob/main/src/server-schema.ts"
    click J "https://github.com/anubhav126/reverse-proxy/blob/main/package.json"

    classDef client fill:#FFC0CB,stroke:#CC0000,stroke-width:2px;
    classDef primary fill:#ADD8E6,stroke:#00008B,stroke-width:2px;
    classDef worker fill:#90EE90,stroke:#006400,stroke-width:2px;
    classDef upstream fill:#FFA500,stroke:#FF8C00,stroke-width:2px;
    classDef config fill:#FFFFE0,stroke:#CCCC00,stroke-width:2px;
    classDef external fill:#D3D3D3,stroke:#A9A9A9,stroke-width:2px;

    class A client;
    class B primary;
    class C worker;
    class D upstream;
    class F1,F2,F3,F4 config;
    class J external;
```
