# x4/nxweb_hg architecture

> Architecture diagram of the GitHub repository x4/nxweb_hg, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/x4/nxweb_hg
- Repository: https://github.com/x4/nxweb_hg

## Overview

##### Main Components

a) Core Server Components:
- Main Thread (Connection Acceptor)
- Network Threads Pool
- Worker Threads Pool
- Event Handler (epoll-based)

b) Protocol Handlers:
- HTTP Server Protocol
- HTTP Client Protocol (for proxy functionality)
- SSL/TLS Handler (GNUTLS integration)

c) Request Processing Pipeline:
- URI Handlers
- Filters System
- Cache System

d) Module System:
- HTTP Proxy Module
- SendFile Module
- Filter Modules (gzip, image, templates, etc.)

##### Important Relationships to Highlight

- Flow from client connection through the main thread to network threads
- How requests can be handled either directly in network threads or dispatched to worker threads
- Demonstrate the filter chain pipeline
- Caching mechanism's interaction with various components
- Proxy module's connection pooling system

##### Technical Details

- epoll-based event system
- Thread pool scaling mechanisms
- Cache hierarchies
- Filter chain sequence
- Protocol handler interactions

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Client Connection Layer"
        Client(("Client"))
        MainThread["Main Thread Acceptor"]:::core
        SSL["SSL/TLS Handler"]:::security
    end

    subgraph "Thread Management Layer"
        NetworkPool["Network Threads Pool"]:::threadpool
        WorkerPool["Worker Threads Pool"]:::threadpool
        EventHandler["Event Handler (epoll)"]:::core
    end

    subgraph "Request Processing Layer"
        HTTPServer["HTTP Server Protocol"]:::protocol
        HTTPClient["HTTP Client Protocol"]:::protocol
        URIHandlers["URI Handlers"]:::handler
        FilterChain["Filter Chain"]:::processing
        Subrequest["Subrequest System"]:::processing
    end

    subgraph "Module Layer"
        ProxyMod["HTTP Proxy Module"]:::module
        SendFileMod["SendFile Module"]:::module
        subgraph "Filter Modules"
            GzipFilter["Gzip Filter"]:::filter
            ImageFilter["Image Filter"]:::filter
            TemplatesFilter["Templates Filter"]:::filter
            FileCacheFilter["File Cache Filter"]:::filter
        end
    end

    subgraph "Storage & Services Layer"
        MemPool["Memory Pool System"]:::memory
        Cache["Cache System"]:::cache
        FileReader["File Reader"]:::io
        Buffer["Buffer Management"]:::memory
        Socket["Socket Handler"]:::io
        ImageMagick["ImageMagick"]:::external
    end

    %% Connections
    Client --> MainThread
    MainThread --> SSL
    SSL --> NetworkPool
    NetworkPool --> EventHandler
    EventHandler --> WorkerPool
    NetworkPool --> HTTPServer
    HTTPServer --> URIHandlers
    URIHandlers --> FilterChain
    FilterChain --> ProxyMod
    FilterChain --> SendFileMod
    FilterChain --> GzipFilter
    FilterChain --> ImageFilter
    FilterChain --> TemplatesFilter
    FilterChain --> FileCacheFilter
    ProxyMod --> HTTPClient
    HTTPClient --> Socket
    SendFileMod --> FileReader
    ImageFilter --> ImageMagick
    FilterChain --> Cache
    WorkerPool --> Subrequest
    Subrequest --> URIHandlers

    %% Click Events
    click EventHandler "https://github.com/X4/nxweb_hg/blob/master/src/lib/nx_event.c"
    click WorkerPool "https://github.com/X4/nxweb_hg/blob/master/src/lib/nx_workers.c"
    click MemPool "https://github.com/X4/nxweb_hg/blob/master/src/lib/nx_pool.c"
    click HTTPServer "https://github.com/X4/nxweb_hg/blob/master/src/lib/nxd_http_server_proto.c"
    click HTTPClient "https://github.com/X4/nxweb_hg/blob/master/src/lib/nxd_http_client_proto.c"
    click SSL "https://github.com/X4/nxweb_hg/blob/master/src/lib/nxd_ssl_socket.c"
    click Cache "https://github.com/X4/nxweb_hg/blob/master/src/lib/cache.c"
    click ProxyMod "https://github.com/X4/nxweb_hg/blob/master/src/lib/modules/http_proxy.c"
    click SendFileMod "https://github.com/X4/nxweb_hg/blob/master/src/lib/modules/sendfile.c"
    click FilterChain "https://github.com/X4/nxweb_hg/tree/master/src/lib/filters/"
    click GzipFilter "https://github.com/X4/nxweb_hg/blob/master/src/lib/filters/gzip_filter.c"
    click ImageFilter "https://github.com/X4/nxweb_hg/blob/master/src/lib/filters/image_filter.c"
    click TemplatesFilter "https://github.com/X4/nxweb_hg/blob/master/src/lib/filters/templates_filter.c"
    click FileCacheFilter "https://github.com/X4/nxweb_hg/blob/master/src/lib/filters/file_cache_filter.c"
    click Socket "https://github.com/X4/nxweb_hg/blob/master/src/lib/nxd_socket.c"
    click Buffer "https://github.com/X4/nxweb_hg/blob/master/src/lib/nx_buffer.c"
    click FileReader "https://github.com/X4/nxweb_hg/blob/master/src/lib/nx_file_reader.c"
    click Subrequest "https://github.com/X4/nxweb_hg/blob/master/src/lib/http_subrequest.c"

    %% Styles
    classDef core fill:#ff6b6b
    classDef security fill:#4ecdc4
    classDef threadpool fill:#45b7d1,stroke:#333
    classDef protocol fill:#96ceb4
    classDef handler fill:#ffeead
    classDef processing fill:#d4a5a5
    classDef module fill:#ffad60
    classDef filter fill:#a8e6cf
    classDef memory fill:#3d84a8
    classDef cache fill:#f8b195
    classDef io fill:#6c5b7b
    classDef external fill:#c6c6c6
```
