# davidmoreno/onion architecture

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

- Interactive diagram: https://gitdiagram.com/davidmoreno/onion
- Repository: https://github.com/davidmoreno/onion

## Overview

##### Main Components

a) Core Library Components (Central Layer):
- HTTP/HTTPS Server Core (`src/onion/onion.c`)
- Request/Response Handlers (`src/onion/handler.c`, `src/onion/request.c`, `src/onion/response.c`)
- URL Router (`src/onion/url.c`)
- Session Management (`src/onion/sessions.c`)
- WebSocket Support (`src/onion/websocket.c`)

b) Network Layer (Bottom Layer):
- Listen Points (`src/onion/listen_point.c`)
- Poller Implementations:
- epoll (Linux)
- libev integration
- libevent integration

c) Security Layer (Cross-cutting):
- SSL/TLS Support (via GnuTLS)
- PAM Authentication
- Random Number Generation

d) Storage Backends (Side Layer):
- SQLite3 Session Storage
- Redis Session Storage
- Memory Session Storage

e) Extension Components (Top Layer):
- Static File Handler
- WebDAV Support
- Template Engine (otemplate)
- Asset Packer (opack)

**[Extensions/Tools Layer]**
- ↕
**[Core Library Layer]**
- ↕
**[Network/IO Layer]**

##### Connection Flows

- Request flow from network layer through handlers
- Session data flow to storage backends
- Template processing flow
- Security integration points

##### Important Details

a) Integration Points:
- C++ Bindings interface
- Template Engine integration
- External library dependencies

b) Threading Models (show as parallel paths):
- Single-threaded
- Multi-threaded per connection
- Thread pool with epoll

##### Specific Implementation Notes

- Optional components (those requiring external libraries)
- Supported platforms (Linux, FreeBSD, Darwin)
- Available session backend options
- Threading model options

##### Key Architectural Patterns to Highlight

- Handler Chain pattern for request processing
- Plugin architecture for handlers
- Event-driven I/O model
- Template processing pipeline

Remember to include version compatibility notes and indicate which components are part of the stable API vs. internal implementation details.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Extensions Layer"
        StaticHandler["Static File Handler"]:::extension
        WebDAV["WebDAV Support"]:::extension
        OTemplate["Template Engine"]:::extension
        OPack["Asset Packer"]:::extension
        CPPBindings["C++ Bindings"]:::extension
    end

    subgraph "Core Layer"
        OnionCore["HTTP/HTTPS Server Core"]:::core
        RequestHandler["Request Handler"]:::core
        ResponseHandler["Response Handler"]:::core
        URLRouter["URL Router"]:::core
        SessionMgr["Session Management"]:::core
        WebSocket["WebSocket Support"]:::core
    end

    subgraph "Network Layer"
        ListenPoints["Listen Points"]:::network
        subgraph "Pollers"
            EPoll["epoll"]:::network
            LibEV["libev"]:::network
            LibEvent["libevent"]:::network
        end
    end

    subgraph "Security Layer"
        SSL["SSL/TLS (GnuTLS)"]:::security
        PAM["PAM Authentication"]:::security
        RNG["Random Number Generator"]:::security
    end

    subgraph "Storage Layer"
        SQLite["SQLite3 Storage"]:::storage
        Redis["Redis Storage"]:::storage
        MemStorage["Memory Storage"]:::storage
    end

    %% Connections
    ListenPoints --> OnionCore
    EPoll & LibEV & LibEvent --> ListenPoints
    OnionCore --> RequestHandler
    RequestHandler --> ResponseHandler
    RequestHandler --> URLRouter
    RequestHandler --> SessionMgr
    RequestHandler --> WebSocket
    SessionMgr --> SQLite & Redis & MemStorage
    SSL --> OnionCore
    PAM --> RequestHandler
    RNG --> OnionCore

    StaticHandler & WebDAV & OTemplate & OPack --> RequestHandler
    CPPBindings --> OnionCore

    %% Styles
    classDef core fill:#a8d1f0
    classDef network fill:#90EE90
    classDef security fill:#FFE5B4
    classDef storage fill:#FFA07A
    classDef extension fill:#DDA0DD

    %% Click events
    click OnionCore "https://github.com/davidmoreno/onion/blob/master/src/onion/onion.c"
    click RequestHandler "https://github.com/davidmoreno/onion/blob/master/src/onion/request.c"
    click ResponseHandler "https://github.com/davidmoreno/onion/blob/master/src/onion/response.c"
    click URLRouter "https://github.com/davidmoreno/onion/blob/master/src/onion/url.c"
    click SessionMgr "https://github.com/davidmoreno/onion/blob/master/src/onion/sessions.c"
    click WebSocket "https://github.com/davidmoreno/onion/blob/master/src/onion/websocket.c"
    click ListenPoints "https://github.com/davidmoreno/onion/blob/master/src/onion/listen_point.c"
    click LibEV "https://github.com/davidmoreno/onion/blob/master/src/onion/poller_libev.c"
    click LibEvent "https://github.com/davidmoreno/onion/blob/master/src/onion/poller_libevent.c"
    click SSL "https://github.com/davidmoreno/onion/blob/master/src/onion/random-gnutls.c"
    click PAM "https://github.com/davidmoreno/onion/blob/master/src/onion/handlers/auth_pam.c"
    click RNG "https://github.com/davidmoreno/onion/blob/master/src/onion/random-default.c"
    click StaticHandler "https://github.com/davidmoreno/onion/blob/master/src/onion/handlers/static.c"
    click WebDAV "https://github.com/davidmoreno/onion/blob/master/src/onion/handlers/webdav.c"
    click OTemplate "https://github.com/davidmoreno/onion/tree/master/tools/otemplate"
    click OPack "https://github.com/davidmoreno/onion/blob/master/tools/opack/opack.c"
    click CPPBindings "https://github.com/davidmoreno/onion/tree/master/src/bindings/cpp"
    click SQLite "https://github.com/davidmoreno/onion/blob/master/src/onion/sessions_sqlite3.c"
    click Redis "https://github.com/davidmoreno/onion/blob/master/src/onion/sessions_redis.c"
    click MemStorage "https://github.com/davidmoreno/onion/blob/master/src/onion/sessions_mem.c"
```
