# hypothesis/h architecture

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

- Interactive diagram: https://gitdiagram.com/hypothesis/h
- Repository: https://github.com/hypothesis/h

## Overview

##### Main Components
- Reverse Proxy / Load Balancer: The project comes with nginx configuration (see conf/nginx.conf), which typically sits in front of the WSGI server (Gunicorn) to handle SSL termination, caching, and load balancing.
- Application Server Layer: Show the core “h” application (located in the “h” directory) that is orchestrated by a WSGI server (e.g., Gunicorn as configured in multiple conf files such as gunicorn-dev.conf.py and gunicorn-websocket-dev.conf.py). This layer contains modules for accounts, annotations, administration, APIs, and real-time functionality.
- Database Layer: Add a database component to represent the persistent storage. This is managed by SQL migrations (the alembic.ini file in conf and the “h/migrations” directory) and would typically be hosted on PostgreSQL.
- Background Task/Worker Layer: Include an asynchronous task layer. There are Celery tasks (under h/tasks) and data tasks (under h/data_tasks) that process background jobs like email, indexing, or document processing.
- Search / Indexing Engine: Outline the search subsystem (see h/indexer and h/search directories) which handles search indexing and query processing.
- Notification and Streaming Components: Represent real-time notifications and event streaming handled by modules such as h/streamer and h/realtime.
- Static Assets & Templates: Show where static assets (found in h/static) and Jinja2 templates (in h/templates) reside to be served to users.
- External Services / Integrations: Optionally indicate email services, third-party authentication, and any external integrations (as hinted by various service-related modules in h/services and the API integrations).

##### Relationships and Interactions
- Data Persistence: Illustrate how the Application Server communicates with the Database for CRUD operations, as well as with background tasks; for example, API endpoints initiating background jobs via Celery.
- Event and Notification Flow: Show bi-directional or one-way communication between the Application Server and the Notification/Streaming subsystem (h/streamer), indicating real-time pushes or websockets.
- External Integrations: If relevant, include connections to external systems like email servers or OAuth providers, shown as separate blocks that the Application Server’s service modules interact with.

##### Architectural Patterns or Principles
- Layered Architecture: Distinguish presentation (templates, static assets), business logic (services, application modules in h/), and data persistence (database).
- Separation of Concerns: Show how modules are grouped (e.g., “h/accounts”, “h/services”, “h/cli”, “h/views/api”) and illustrate their interactions.
- Asynchronous Processing: Mark a clear separation between real-time HTTP request processing and background task processing.
- Configuration and Deployment: Consider adding a block for configuration (represented by the conf directory, Dockerfiles, and cookiecutter templates) and deployment (Docker Compose, GitHub workflows).

##### Relevant Technologies, Frameworks, and Tools
- Python (with Pyramid via cookiecutter-pyramid-app)
- Gunicorn as the WSGI server (with various configuration files provided under conf and .cookiecutter/includes/conf)
- Nginx as a reverse proxy (conf/nginx.conf)
- PostgreSQL (implied via alembic.ini and migrations)
- Celery for background tasks
- Docker (with Dockerfile, docker-compose.yml, and multiple docker-compose configurations)
- Static Frontend tooling (Node, Yarn, Webpack/Gulp as hinted by gulpfile.js and frontend.mk)
- Testing frameworks (pytest, tox, and detailed tests directories)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Layer"
        A["User's Browser & API Clients"]:::client
        B["Static Assets & Templates"]:::frontend
    end

    %% Infrastructure - Reverse Proxy
    subgraph "Infrastructure"
        C["Reverse Proxy / Load Balancer (Nginx)"]:::proxy
    end

    %% Application Server Layer
    subgraph "Application Server"
        D1["Core Application (h)"]:::appserver
        D2["WSGI Configurations (Gunicorn)"]:::config
        E["API Endpoints"]:::api
        F["Real-time / Notification Service"]:::realtime
    end

    %% Background Processing
    subgraph "Background Processing"
        G["Background Task Worker (Celery)"]:::worker
        H["Search / Indexing Engine"]:::search
    end

    %% Database & External Integrations
    subgraph "Database & External"
        I["Database (PostgreSQL)"]:::database
        J["External Services / Integrations"]:::external
    end

    %% Configuration flows (integrated with nodes)
    %% Data Flow
    A -->|"HTTP(s)Request"| C
    C -->|"ForwardsRequest"| D1
    D2 -->|"Configures"| D1
    D1 -->|"RendersPages"| B
    D1 -->|"HandlesAPIRequests"| E
    E -->|"CRUDOperations"| I
    D1 -->|"QueuesBackgroundJobs"| G
    E -->|"QueuesTasks"| G
    D1 -->|"TriggersSearchIndexing"| H
    H -->|"ReadsWritesIndex"| I
    D1 -->|"SendsNotifications"| F
    F -->|"PushesEvents"| D1
    J ---|"IntegratesWith"| D1

    %% Click Events
    click B "https://github.com/hypothesis/h/tree/main/h/templates"
    click B "https://github.com/hypothesis/h/tree/main/h/static"
    click C "https://github.com/hypothesis/h/blob/main/conf/nginx.conf"
    click D1 "https://github.com/hypothesis/h/tree/main/h"
    click D2 "https://github.com/hypothesis/h/blob/main/conf/gunicorn-dev.conf.py"
    click D2 "https://github.com/hypothesis/h/blob/main/conf/gunicorn-websocket-dev.conf.py"
    click E "https://github.com/hypothesis/h/tree/main/h/views/api"
    click E "https://github.com/hypothesis/h/tree/main/h/services/bulk_api"
    click I "https://github.com/hypothesis/h/tree/main/h/migrations"
    click I "https://github.com/hypothesis/h/blob/main/conf/alembic.ini"
    click G "https://github.com/hypothesis/h/tree/main/h/tasks"
    click G "https://github.com/hypothesis/h/tree/main/h/data_tasks"
    click H "https://github.com/hypothesis/h/tree/main/h/indexer"
    click H "https://github.com/hypothesis/h/tree/main/h/search"
    click F "https://github.com/hypothesis/h/tree/main/h/streamer"
    click F "https://github.com/hypothesis/h/tree/main/h/realtime"
    click J "https://github.com/hypothesis/h/tree/main/h/services"

    %% Styles
    classDef client fill:#B3E5FC,stroke:#000,stroke-width:1.5px;
    classDef frontend fill:#81C784,stroke:#000,stroke-width:1.5px;
    classDef proxy fill:#FFCC80,stroke:#000,stroke-width:1.5px;
    classDef appserver fill:#AED581,stroke:#000,stroke-width:1.5px;
    classDef config fill:#EF9A9A,stroke:#000,stroke-width:1.5px;
    classDef api fill:#90CAF9,stroke:#000,stroke-width:1.5px;
    classDef realtime fill:#CE93D8,stroke:#000,stroke-width:1.5px;
    classDef worker fill:#F48FB1,stroke:#000,stroke-width:1.5px;
    classDef search fill:#FFF176,stroke:#000,stroke-width:1.5px;
    classDef database fill:#E57373,stroke:#000,stroke-width:1.5px;
    classDef external fill:#B39DDB,stroke:#000,stroke-width:1.5px;
```
