# nihalxkumar/zingat architecture

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

- Interactive diagram: https://gitdiagram.com/nihalxkumar/zingat
- Repository: https://github.com/nihalxkumar/zingat

## Overview

##### Project type and purpose
- From the file tree and README, Zingat is a Rust-based pastebin application that offers both:
- A web interface (HTTP server) for end-users to create and view “clips”
- A command-line client (CLI) for programmatic interactions
- Features include password protection, clip expiry, hit counting (batched), and templating for HTML pages.

##### File structure
- src/bin
- httpd.rs → The Rocket-based HTTP server entrypoint
- clipclient.rs → The CLI client entrypoint
- src/lib
- domain → Core business objects (Clip, fields like content, shortcode, expires, password, etc.)
- data → Data-access layer (SQLX queries, models, migrations under “migrations/”)
- service → Application logic (actions, ask abstractions)
- web → Web layer (API handlers, HTTP context, forms, renderer, hit counter)
- templates/ & static/ → Handlebars templates and CSS served by the web server
- migrations/ → SQL scripts for database schema
- Cargo.toml, .env, data.db → Configuration, environment, and SQLite database

##### README for additional insights
- Tech stack: Rust, SQLX, Rocket, Tokio
- Performance optimization: asynchronous hit-count batching on separate Tokio threads
- Two modes of operation: Web and CLI

##### Main components and interactions
a. Components
1. HTTP Server (Rocket + Tokio runtime)
2. CLI Client (tokio + SQLX)
3. Service Layer (business logic)
4. Data Layer (SQLX, SQLite, migrations)
5. Domain Layer (Clip entity, validation)
6. Templates & Static Assets (Handlebars, CSS)
7. Background Worker Threads (batching hit counts)
b. Relationships & Data Flow
- HTTP Server → Web Layer → Service Layer → Data Layer → SQLite DB
- CLI Client → Service Layer → Data Layer → SQLite DB
- Service Layer dispatches clip hits to a background worker (Tokio task) for batching
- Web Layer renders templates/static via the renderer component
c. Architectural patterns
- Layered architecture: Presentation (web/CLI) → Service → Data → Database
- Domain-driven design: strict separation of domain types (Clip, fields)
- Repository pattern in data layer (query.rs, model.rs)
- Asynchronous batching (Tokio tasks) for performance
d. Key technologies
- Rocket – HTTP routing & request handling
- Tokio – async runtime for HTTP server & background workers
- SQLX – compile-time checked SQL and migrations
- Handlebars (via templates/) – HTML rendering
- SQLite (data.db) – persistent storage

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Entrypoints"
        HTTP["HTTP Server\n(Rocket)"]:::entry
        CLI["CLI Client"]:::entry
    end

    subgraph "Web Layer"
        API["API Handlers"]:::web
        Renderer["Renderer"]:::web
    end

    subgraph "Service Layer"
        Service["Service Layer\naction.rs, ask.rs, mod.rs"]:::core
    end

    subgraph "Data Layer"
        Data["Data Layer\nmod.rs, model.rs, query.rs"]:::core
    end

    Domain["Domain Layer\nclip/, maintenance.rs, time.rs"]:::core

    Worker["Background Hit Counter"]:::external
    Templates["Templates"]:::external
    Static["Static Assets"]:::external
    DB["SQLite DB"]:::external

    HTTP -->|"/api/..."/renders| API
    API -->|"calls service"| Service
    CLI -->|"calls service"| Service
    Service -->|"queries"| Data
    Data -->|"reads/writes"| DB
    Service -->|"batches hits"| Worker
    Worker -->|"writes batched hits"| Data
    API -->|"renders via"| Renderer
    Renderer -->|"loads templates"| Templates
    HTTP -->|"serves CSS"| Static

    click HTTP "https://github.com/nihalxkumar/zingat/blob/main/src/bin/httpd.rs"
    click CLI "https://github.com/nihalxkumar/zingat/blob/main/src/bin/clipclient.rs"
    click Service "https://github.com/nihalxkumar/zingat/tree/main/src/lib/service/"
    click Data "https://github.com/nihalxkumar/zingat/tree/main/src/lib/data/"
    click Domain "https://github.com/nihalxkumar/zingat/tree/main/src/lib/domain/"
    click API "https://github.com/nihalxkumar/zingat/blob/main/src/lib/web/api.rs"
    click Renderer "https://github.com/nihalxkumar/zingat/blob/main/src/lib/web/renderer.rs"
    click Worker "https://github.com/nihalxkumar/zingat/blob/main/src/lib/web/hitcounter.rs"
    click Templates "https://github.com/nihalxkumar/zingat/tree/main/templates/"
    click Static "https://github.com/nihalxkumar/zingat/blob/main/static/zingat.css"
    click DB "https://github.com/nihalxkumar/zingat/blob/main/data.db"

    classDef entry fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef core fill:#90EE90,stroke:#000,stroke-width:1px
    classDef web fill:#87CEFA,stroke:#000,stroke-width:1px
    classDef external fill:#FFA500,stroke:#000,stroke-width:1px
```
