# mcvyp/rust-webserver architecture

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

- Interactive diagram: https://gitdiagram.com/mcvyp/rust-webserver
- Repository: https://github.com/mcvyp/rust-webserver

## Overview

##### Project type and purpose
- This is a small, single-binary web server written in Rust.
- It provides static file serving (e.g., “index.html” and “404.html”) over HTTP using a thread pool for concurrency.

##### File structure
- Top-level files:
- Cargo.toml / Cargo.lock — Rust package metadata and dependencies (threadpool, num_cpus)
- README.md — very brief description (“A basic web server with Rust”)
- index.html, 404.html — static assets served by the server
- src directory:
- src/bin/main.rs — entry point; starts the listener and thread pool
- src/lib.rs — core library: contains listener, connection handler, routing logic, thread pool wrapper
- There are no separate frontend/backend directories; this is a pure backend service.

##### README for additional insights
- It confirms the project is a “basic web server.”
- No extra architecture notes are provided.

##### Main components
a. External Web Client (Browser / HTTP Client)
b. TCP Listener (accepts incoming HTTP connections)
c. Thread Pool Manager (manages a fixed pool of worker threads)
d. Request Handler (parses HTTP requests, matches routes)
e. Static File Storage (filesystem containing index.html, 404.html)
f. Rust Standard Library & External Crates (threadpool, num_cpus)

##### Relationships and data flow
- Client → (HTTP request) → TCP Listener
- TCP Listener → dispatch → Thread Pool Manager
- Thread Pool Manager → worker thread → Request Handler
- Request Handler → filesystem read → Static File Storage
- Static File Storage → file data → Request Handler
- Request Handler → (HTTP response) → Client

##### Architectural patterns & design principles
- Concurrency via a fixed-size thread pool (minimize thread-creation overhead)
- Separation of concerns:
- main.rs only bootstraps listener and pool
- lib.rs contains pure logic (handler, routing, pool abstraction)
- Dependency injection: the thread pool is configured using system CPU count (via num_cpus)

##### Relevant technologies & libraries
- Rust’s std::net for TCP
- threadpool crate for concurrency
- num_cpus to size the pool
- std::fs for file I/O

## Diagram (Mermaid)

```mermaid
flowchart TD
    Client["Web Client (Browser)"]:::network

    subgraph "Rust WebServer"
        subgraph "Entry Point & TCP Listener"
            Listener["TCP Listener"]:::network
        end
        subgraph "Core Library"
            ThreadPool["Thread Pool Manager<br/>uses threadpool, num_cpus"]:::compute
            Handler["Request Handler<br/>Parsing & Routing"]:::compute
        end
        FileStore["Static File Storage"]:::storage
    end

    Metadata["Package Metadata & External Crates"]:::metadata
    Documentation["Documentation"]:::metadata

    Client -->|"HTTP Request"| Listener
    Listener -->|"Dispatch"| ThreadPool
    ThreadPool -->|"Run Task"| Handler
    Handler -->|"Read File"| FileStore
    FileStore -->|"File Data"| Handler
    Handler -->|"HTTP Response"| Client
    ThreadPool -.->|"depends on crates"| Metadata

    click Listener "https://github.com/mcvyp/rust-webserver/blob/main/src/bin/main.rs"
    click ThreadPool "https://github.com/mcvyp/rust-webserver/blob/main/src/lib.rs"
    click Handler "https://github.com/mcvyp/rust-webserver/blob/main/src/lib.rs"
    click FileStore "https://github.com/mcvyp/rust-webserver/blob/main//index.html"
    click FileStore "https://github.com/mcvyp/rust-webserver/blob/main//404.html"
    click Metadata "https://github.com/mcvyp/rust-webserver/blob/main/Cargo.toml"
    click Metadata "https://github.com/mcvyp/rust-webserver/blob/main/Cargo.lock"
    click Documentation "https://github.com/mcvyp/rust-webserver/blob/main/README.md"

    classDef network fill:#add8e6,stroke:#000
    classDef compute fill:#90ee90,stroke:#000
    classDef storage fill:#ffff99,stroke:#000
    classDef metadata fill:#dda0dd,stroke:#000
```
