# plutack/seedrlike architecture

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

- Interactive diagram: https://gitdiagram.com/plutack/seedrlike
- Repository: https://github.com/plutack/seedrlike

## Overview

##### Project Type and Purpose
- Seedrlike is a full-stack web application with a focus on torrent-based downloads via magnet links. It provides a web interface (using views/templates, Tailwind CSS, and some JavaScript) for initiating and monitoring downloads, and it leverages both RESTful HTTP endpoints and websockets for real‐time updates.
- The design also reveals a separation between the presentation layer (views) and the business logic/APIs (internal/api, internal/core), along with clear persistence via a SQL database (using sqlc and migrations).

##### Analyzing the File Structure
- Top-level Directories:
- cmd – Contains the main entry point of the application (cmd/main.go).
- internal – Contains the bulk of business logic and application components:
▪ api – Implements HTTP handlers (including folders, torrent, websocket, and a healthcheck). It represents the API layer.
▪ core – Contains core functionalities such as client interactions, torrent processing, managing queues, uploads, and websocket communication.
▪ database – Manages migrations and data queries using sqlc with dedicated SQL files.
- views – Holds the user interface templates, assets (CSS/JS), components, and layouts.
- Notable Supporting Files:
- go.mod/go.sum – Define the Go module dependencies.
- sqlc.yaml – Config for generating type-safe database access code.
- tailwind.config.js – For customizing Tailwind CSS.
- Dockerfile/nixpacks.toml – Indicate deployment and containerization configurations.

##### Key Components
a. User Interface/Presentation Layer:
- Views/Templates: HTML templating in the “views” folder with assets like CSS, JS (HTMX extension for websockets).
- Client Browser: Accesses the UI and communicates with the backend via HTTP and websockets.

b. API Layer:
- HTTP Server: Located in internal/api/server.go, responsible for handling incoming requests.
- API Handlers: Specific endpoints (folders, torrent, websocket, health) defined under internal/api/handlers.
- Response Layer: Formatting response objects in internal/api/response.

c. Core Business Logic:
- Torrent Processing: Handled in internal/core/torrent/torrent.go, responsible for interacting with the torrent library (referencing the anacrolix/torrent dependency).
- Websocket Management: Managed in internal/core/websocket/websocket.go for real-time update events.
- Client and Queue/Upload Processing: Found in internal/core/client/client.go, internal/core/queue/queue.go, and internal/core/upload/upload.go.

d. Database & Data Layer:
- Persistence: Code generated (using sqlc) in internal/database/sqlc, along with migration files (SQL seed data and structure in internal/database/migrations).
- SQL Queries: Maintained in dedicated SQL files (e.g., torrent.sql).

##### Relationships and Interactions
- The Client Browser interacts with the Web UI (views) and sends requests (via forms or AJAX/HTMX) to the HTTP server.
- The API Layer receives requests, performing validation and invoking corresponding API handlers.
- API handlers delegate business logic to components within the Core layer (e.g., initiating torrents, managing upload and websocket events).
- The Core layer interacts with the Database layer for persistent storage and uses third-party libraries (such as anacrolix/torrent) to handle torrent-specific logic.
- Websocket components provide a bidirectional communication channel to update the front-end in real time.

##### Architectural Patterns and Technologies
- Layered Architecture: Clearly separated presentation (views), API, core business logic, and data persistence.
- MVC-Like Separation: Although not a strict MVC, the “views” directory represents the view layer, and the API handlers serve as controllers using the core logic as the model.
- Use of Go Modules for dependency management, Tailwind CSS for front-end styling, and SQL migrations (via goose) to manage the database schema.
- Docker supports containerized deployment, and the configuration files (envrc.example, Dockerfile, nixpacks.toml) help facilitate a production-ready environment.

## Diagram (Mermaid)

```mermaid
graph TD
    client["Client Browser"]:::frontend
    ui["Web UI / Views"]:::frontend
    apiServer["API Server"]:::api

    subgraph "API Layer"
        apiHandlers["API Handlers"]:::api
        apiResponse["API Response"]:::api
    end

    subgraph "Core Business Logic"
        torrentProcessing["Torrent Processing"]:::core
        websocketManagement["Websocket Management"]:::core
        clientProcessing["Client Processing"]:::core
        queueManagement["Queue Management"]:::core
        uploadProcessing["Upload Processing"]:::core
    end

    externalLibrary["Torrent Library (anacrolix)"]:::external

    subgraph "Database Persistence"
        database1(("SQLC & Queries")):::db
        migrations(("Migrations")):::db
    end

    client -->|"accesses"| ui
    ui -->|"sendsHTTP/WS"| apiServer
    apiServer -->|"routesTo"| apiHandlers
    apiServer -->|"formats"| apiResponse

    apiHandlers -->|"processes"| torrentProcessing
    apiHandlers -->|"manages"| websocketManagement
    apiHandlers -->|"handles"| clientProcessing
    apiHandlers -->|"queues"| queueManagement
    apiHandlers -->|"uploads"| uploadProcessing

    torrentProcessing -->|"uses"| externalLibrary
    websocketManagement -->|"updates"| client

    torrentProcessing -->|"storesData"| database1
    clientProcessing -->|"storesData"| database1
    queueManagement -->|"storesData"| database1
    uploadProcessing -->|"storesData"| database1

    click ui "https://github.com/plutack/seedrlike/tree/main/views/"
    click apiServer "https://github.com/plutack/seedrlike/blob/main/internal/api/server.go"
    click apiHandlers "https://github.com/plutack/seedrlike/tree/main/internal/api/handlers"
    click apiResponse "https://github.com/plutack/seedrlike/blob/main/internal/api/response/response.go"
    click torrentProcessing "https://github.com/plutack/seedrlike/blob/main/internal/core/torrent/torrent.go"
    click websocketManagement "https://github.com/plutack/seedrlike/blob/main/internal/core/websocket/websocket.go"
    click clientProcessing "https://github.com/plutack/seedrlike/blob/main/internal/core/client/client.go"
    click queueManagement "https://github.com/plutack/seedrlike/blob/main/internal/core/queue/queue.go"
    click uploadProcessing "https://github.com/plutack/seedrlike/blob/main/internal/core/upload/upload.go"
    click database1 "https://github.com/plutack/seedrlike/tree/main/internal/database/sqlc"
    click migrations "https://github.com/plutack/seedrlike/tree/main/internal/database/migrations"

    classDef frontend fill:#ffe6cc,stroke:#333,stroke-width:2px;
    classDef api fill:#cce5ff,stroke:#333,stroke-width:2px;
    classDef core fill:#d5f5e3,stroke:#333,stroke-width:2px;
    classDef db fill:#fadbd8,stroke:#333,stroke-width:2px;
    classDef external fill:#e8daef,stroke:#333,stroke-width:2px;
```
