# lus/pasty architecture

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

- Interactive diagram: https://gitdiagram.com/lus/pasty
- Repository: https://github.com/lus/pasty

## Overview

##### Project type and purpose
- Pasty is a full-stack web application: a lightweight pastebin service.
- Its purpose is to provide a REST API (v1/v2) and a static web frontend for creating, retrieving, modifying, and automatically deleting code/text pastes, with pluggable storage backends and optional reporting webhooks.

##### File structure
- Top-level “cmd/” contains two CLI entrypoints:
- cmd/pasty: the main web server binary
- cmd/transfer: a data migrator/transfer tool
- “internal/web” holds HTTP routing, controllers, and logger.
- “internal/storage” contains the storage driver interface and implementations (file, Postgres, MongoDB, S3). Migrations live under “postgres”.
- “internal/report” implements the report-webhook logic.
- “internal/config” and “internal/env” handle environment variables and configuration.
- “internal/utils” and “internal/shared” include shared helpers (ID generation, paste data model).
- The “web” directory contains the static SPA: HTML, CSS/SCSS, and JS modules.
- Dev containers: Dockerfile and docker-compose.dev.yml define Docker-based deployment and environment.

##### Extract insights from the README
- The service exposes HTTP endpoints for paste creation, retrieval, modification, deletion, and hastebin compatibility.
- It supports optional auto-delete (scheduled cleanup), rate-limiting, and report-webhook notifications.
- Multiple storage backends can be selected via `PASTY_STORAGE_TYPE`.
- Configuration is entirely via environment variables with sensible defaults.

##### Main components
a. Client tier
- Browser or CLI clients
- Static Web App served from `/web/assets` (HTML/CSS/JS SPA)
- Third-party clients/tools calling the REST API (`/v1`, `/v2`)
b. API Server tier
- HTTP server (net/http) in cmd/pasty
- Router & middleware (rate-limiting, logging)
- Controllers: v1 (incl. hastebin support) and v2
- AutoDelete scheduler (background goroutine)
- Report dispatcher (calls external webhook)
- Configuration loader (env/config modules)
c. Storage tier (pluggable)
- File system driver (local)
- Postgres driver + SQL migrations
- MongoDB driver
- S3 driver
- ID/generation and modification token logic
d. External services
- PostgreSQL database
- MongoDB cluster
- S3-compatible object store
- Optional report webhook endpoint
e. Utility binaries
- `pasty` CLI (same code as API server)
- `transfer` CLI (data migration tool)

##### Relationships and interactions
- Clients → HTTP(S) → API Server
- SPA static files served by the server
- AJAX calls from SPA to `/api/v1/pastes`, `/api/v2/pastes`, `/documents` endpoints
- API Server ↔ Storage driver
- Read/write paste data and metadata
- Use ID generation and modification tokens
- AutoDelete scheduler → Storage driver
- Periodic cleanup of expired pastes
- Report controller → External webhook
- POST JSON with Authorization token on each report
- Transfer CLI → Source Storage → Target Storage
- Reads data via shared paste interface, writes to new backend
- Docker Compose dev setup ties together the API server container, database containers, and exposes ports.

##### Architectural patterns and design principles
- Modular, pluggable storage drivers (interface + implementations)
- Layered architecture: config/env → API logic → storage → external services
- Separation of concerns: web controllers, reporting, auto-cleanup, migration CLI
- Twelve-factor style: all config via environment variables
- Single binary for CLI and server via Go’s `cmd/` pattern

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Client Tier"
        Browser["Browser / SPA"]:::frontend
        CLIClient["CLI Client"]:::frontend
    end

    subgraph "API Server"
        HTTPServer["HTTP Server Entrypoint"]:::backend
        Router["HTTP Routing & Server Setup"]:::backend
        Logger["Logging Middleware"]:::backend
        V1Ctrl["v1 Controllers (Hastebin Support)"]:::backend
        V2Ctrl["v2 Controllers"]:::backend
        AutoDel["AutoDelete Scheduler"]:::backend
        ReportDisp["Report Dispatcher"]:::backend
        Config["Configuration Loader"]:::backend
        Env["Environment Variable Handler"]:::backend
        SharedModel["Shared Paste Data Model"]:::backend
        IDGen["ID Generation Logic"]:::backend
        DriverIntf["Storage Driver Interface"]:::backend
    end

    subgraph "Storage Tier"
        FileDriver["File System Driver"]:::storage
        PgDriver["Postgres Driver"]:::storage
        Migrations["Postgres Migrations"]:::storage
        MongoDriver["MongoDB Driver"]:::storage
        S3Driver["S3 Driver"]:::storage
    end

    subgraph "External Services"
        PostgresDB(("Postgres DB")):::external
        MongoDB(("MongoDB Cluster")):::external
        S3Store(("S3 Object Store")):::external
        Webhook["Webhook Endpoint"]:::external
    end

    subgraph "Utility Binaries"
        TransferCLI["Transfer CLI"]:::utility
    end

    Browser -->|"HTTP GET / SPA"| HTTPServer
    CLIClient -->|"CLI Commands"| HTTPServer
    HTTPServer -->|"initializes"| Router
    Router -->|"uses"| Logger
    Router -->|"routes to"| V1Ctrl
    Router -->|"routes to"| V2Ctrl
    V1Ctrl -->|"reads/writes"| DriverIntf
    V2Ctrl -->|"reads/writes"| DriverIntf
    DriverIntf -->|"implements"| FileDriver
    DriverIntf -->|"implements"| PgDriver
    DriverIntf -->|"implements"| MongoDriver
    DriverIntf -->|"implements"| S3Driver
    PgDriver -->|"runs"| Migrations
    AutoDel -->|"Periodic DELETE"| DriverIntf
    ReportDisp -->|"POST report"| Webhook
    Config -->|"loads config for"| HTTPServer
    Env -->|"provides env vars to"| Config
    V1Ctrl -->|"uses model"| SharedModel
    V2Ctrl -->|"uses model"| SharedModel
    DriverIntf -->|"uses ID gen"| IDGen
    TransferCLI -->|"migrates data via"| DriverIntf

    click Browser "https://github.com/lus/pasty/blob/master/web/index.html"
    click HTTPServer "https://github.com/lus/pasty/blob/master/cmd/pasty/main.go"
    click Router "https://github.com/lus/pasty/blob/master/internal/web/web.go"
    click Logger "https://github.com/lus/pasty/blob/master/internal/web/logger.go"
    click V1Ctrl "https://github.com/lus/pasty/blob/master/internal/web/controllers/v1/pastes.go"
    click V1Ctrl "https://github.com/lus/pasty/blob/master/internal/web/controllers/v1/hastebin_support.go"
    click V1Ctrl "https://github.com/lus/pasty/blob/master/internal/web/controllers/v1/paste_adapter.go"
    click V2Ctrl "https://github.com/lus/pasty/blob/master/internal/web/controllers/v2/pastes.go"
    click ReportDisp "https://github.com/lus/pasty/blob/master/internal/report/report.go"
    click Config "https://github.com/lus/pasty/blob/master/internal/config/config.go"
    click Env "https://github.com/lus/pasty/blob/master/internal/env/env.go"
    click SharedModel "https://github.com/lus/pasty/blob/master/internal/shared/paste.go"
    click SharedModel "https://github.com/lus/pasty/blob/master/internal/shared/storage_type.go"
    click DriverIntf "https://github.com/lus/pasty/blob/master/internal/storage/driver.go"
    click IDGen "https://github.com/lus/pasty/blob/master/internal/storage/id_generation.go"
    click FileDriver "https://github.com/lus/pasty/blob/master/internal/storage/file/file_driver.go"
    click PgDriver "https://github.com/lus/pasty/blob/master/internal/storage/postgres/postgres_driver.go"
    click Migrations "https://github.com/lus/pasty/tree/master/internal/storage/postgres/migrations"
    click MongoDriver "https://github.com/lus/pasty/blob/master/internal/storage/mongodb/mongodb_driver.go"
    click S3Driver "https://github.com/lus/pasty/blob/master/internal/storage/s3/s3_driver.go"
    click TransferCLI "https://github.com/lus/pasty/blob/master/cmd/transfer/main.go"

    classDef frontend fill:#9f6,stroke:#333,stroke-width:1px
    classDef backend fill:#6cf,stroke:#333,stroke-width:1px
    classDef storage fill:#fc6,stroke:#333,stroke-width:1px
    classDef external fill:#c6f,stroke:#333,stroke-width:1px
    classDef utility fill:#ccc,stroke:#333,stroke-width:1px
```
