# nightfruit/reflux architecture

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

- Interactive diagram: https://gitdiagram.com/nightfruit/reflux
- Repository: https://github.com/nightfruit/reflux

## Overview

##### Project Type & Purpose
- This is a backend service built with NestJS (as indicated by files like app.module.ts, nest-cli.json, etc.) that functions as a Stremio addon. The service provides endpoints (catalog, manifest, meta, stream) that interface with the Rede Canais website.
- The purpose is to expose content (movies, shows, etc.) via a well-defined API for Stremio, making the integration seamless.

##### Analyzing the File Structure
- Top-Level Files: Configuration files (.editorconfig, .env.example, .gitignore, etc.) and NestJS-specific configuration (nest-cli.json, tsconfig.json).
- Public Directory: Contains static assets (e.g. banner image) often used for branding or for hosting non-dynamic assets.
- Scripts Directory: Contains auxiliary scripts (proxy.js, workers.js) possibly for handling proxying and background jobs.
- src Directory: The core of the application, comprising several important subdirectories:
- config: Handles environment configurations and service logic (env.module.ts, env.service.ts, env.ts).
- exceptions: Contains custom exception handling (too-many-requests.exception.ts).
- modules: Contains the “cdn” module responsible for handling data from the content delivery network aspects—the services here (indexing, listing, media, source) suggest integration with external content sources.
- routes: Contains individual directories for API routes/controllers for catalog, manifest, meta, and stream. Each route has its own controller, module, and service for maintaining separation of concerns.
- utils: Provides utility helpers (for encryption, hashing, proxies, string manipulation) that support broader functionality across the application.

a. Main Components:
- API Controllers (Catalog, Manifest, Meta, Stream): These represent the entry points for the addon, handling incoming requests from Stremio.
- Service Layer: Each route has a corresponding service (e.g. catalog.service.ts) that encapsulates the business logic.
- Core Modules: The ‘cdn’ module and its services represent the integration with external content sources (Rede Canais).
- Configuration Module: Centralizes environment and runtime configurations.
- Exception Handling: The custom exception module ensures that special cases (like too many requests) are managed gracefully.
- Utility Functions: Shared logic that supports various parts of the app (e.g., encryption or proxy handling).

b. Relationships & Interactions:
- The Stremio client interacts with the addon endpoints (manifest, catalog, etc.) – incoming HTTP requests hit the corresponding controllers.
- Each controller invokes its associated service which might further delegate tasks to utility functions or CDN services.
- The CDN module (and its array of services) interact with external sources, pulling in content data.
- The configuration service is injected into modules that need access to environment variables, affecting how external APIs are accessed.
- Exception handling is layered across controllers and services to catch issues, such as rate limiting.

c. Architectural Patterns & Design Principles:
- Modular Design: Separation of configuration, exception handling, API routes, and CDN services follows NestJS best practices.
- Layered Architecture: Controllers, services, and utility modules ensure a clear separation between presentation, business logic, and supporting functionalities.
- Dependency Injection: Seen in the core NestJS style, making testing easier and improving maintainability.
- Clear API Gateway/Integration Patterns: Since this is a Stremio addon, treat the endpoints as a gateway into the business logic that fetches and formats data from Rede Canais.

##### Final Summary
- Lay out the API Routes (catalog, manifest, meta, stream) centrally.
- Routes to underlying services that may call upon the CDN module and utility functions.
- To annotate and label every component and dependency clearly, so the overall data flow and interactions are fully understood.

## Diagram (Mermaid)

```mermaid
flowchart TD
    StremioClient["Stremio Client"]:::stremio

    subgraph "API Layer"
        CatalogCtrl["Catalog Controller"]:::controller
        ManifestCtrl["Manifest Controller"]:::controller
        MetaCtrl["Meta Controller"]:::controller
        StreamCtrl["Stream Controller"]:::controller
    end

    subgraph "Service Layer"
        CatalogSvc["Catalog Service"]:::service
        ManifestSvc["Manifest Service"]:::service
        MetaSvc["Meta Service"]:::service
        StreamSvc["Stream Service"]:::service
    end

    subgraph "CDN Module"
        CDN_Module["CDN Module"]:::cdn
        IndexService["Indexing Service"]:::cdn
        ListService["List Service"]:::cdn
        MediaService["Media Service"]:::cdn
        SourceService["Source Service"]:::cdn
    end

    subgraph "Cross Cutting Concerns"
        subgraph "Configuration"
            EnvModule["Environment Module"]:::config
            EnvService["Environment Service"]:::config
            EnvConfig["Environment Configuration"]:::config
        end
        subgraph "Exception Handling"
            ExceptionHandler["Too Many Requests Exception"]:::exception
        end
        subgraph "Utilities"
            EncryptUtil["Encryption Utility"]:::utility
            HashUtil["Hashing Utility"]:::utility
            ProxyUtil["Proxy Utility"]:::utility
            StringUtil["String Manipulation Utility"]:::utility
        end
    end

    subgraph "Auxiliary Scripts"
        ProxyScript["Proxy Script"]:::auxiliary
        WorkersScript["Workers Script"]:::auxiliary
    end

    ExternalData["Rede Canais Website"]:::external

    %% Connections from Stremio Client to API Controllers
    StremioClient -->|"HTTP_Request"| CatalogCtrl
    StremioClient -->|"HTTP_Request"| ManifestCtrl
    StremioClient -->|"HTTP_Request"| MetaCtrl
    StremioClient -->|"HTTP_Request"| StreamCtrl

    %% Controllers call their respective Services
    CatalogCtrl -->|"ServiceCall"| CatalogSvc
    ManifestCtrl -->|"ServiceCall"| ManifestSvc
    MetaCtrl -->|"ServiceCall"| MetaSvc
    StreamCtrl -->|"ServiceCall"| StreamSvc

    %% API Controllers utilize Exception Handling
    CatalogCtrl -.->|"Exception"| ExceptionHandler
    ManifestCtrl -.->|"Exception"| ExceptionHandler
    MetaCtrl -.->|"Exception"| ExceptionHandler
    StreamCtrl -.->|"Exception"| ExceptionHandler

    %% Services depend on Configuration
    CatalogSvc -->|"Config"| EnvModule
    ManifestSvc -->|"Config"| EnvModule
    MetaSvc -->|"Config"| EnvModule
    StreamSvc -->|"Config"| EnvModule

    %% Services use Utility Functions
    CatalogSvc -->|"UtilityCall"| EncryptUtil
    CatalogSvc -->|"UtilityCall"| HashUtil
    ManifestSvc -->|"UtilityCall"| ProxyUtil
    MetaSvc -->|"UtilityCall"| StringUtil
    StreamSvc -->|"UtilityCall"| EncryptUtil

    %% Services call CDN Module for data fetching
    CatalogSvc -->|"CDN_Call"| CDN_Module
    ManifestSvc -->|"CDN_Call"| CDN_Module
    MetaSvc -->|"CDN_Call"| CDN_Module
    StreamSvc -->|"CDN_Call"| CDN_Module

    %% CDN Module delegates to its internal services
    CDN_Module -->|"Indexing"| IndexService
    CDN_Module -->|"Listing"| ListService
    CDN_Module -->|"Media"| MediaService
    CDN_Module -->|"Source"| SourceService

    %% CDN Module fetches data from external source
    CDN_Module -->|"External_API"| ExternalData

    %% Auxiliary Scripts support background processing and proxy functions
    ProxyScript -->|"Handles_Proxy"| ProxyUtil
    WorkersScript -->|"Background_Jobs"| CDN_Module

    %% Styling Classes
    classDef stremio fill:#B3E5FC,stroke:#0288D1,stroke-width:2px;
    classDef controller fill:#BBDEFB,stroke:#1976D2,stroke-width:2px;
    classDef service fill:#C8E6C9,stroke:#388E3C,stroke-width:2px;
    classDef cdn fill:#FFE0B2,stroke:#F57C00,stroke-width:2px;
    classDef config fill:#E1BEE7,stroke:#8E24AA,stroke-width:2px;
    classDef exception fill:#FFCDD2,stroke:#C62828,stroke-width:2px;
    classDef utility fill:#FFF9C4,stroke:#F9A825,stroke-width:2px;
    classDef auxiliary fill:#D1C4E9,stroke:#673AB7,stroke-width:2px;
    classDef external fill:#CFD8DC,stroke:#546E7A,stroke-width:2px;

    %% Click Events for API Controllers
    click CatalogCtrl "https://github.com/nightfruit/reflux/blob/main/src/routes/catalog/catalog.controller.ts"
    click ManifestCtrl "https://github.com/nightfruit/reflux/blob/main/src/routes/manifest/manifest.controller.ts"
    click MetaCtrl "https://github.com/nightfruit/reflux/blob/main/src/routes/meta/meta.controller.ts"
    click StreamCtrl "https://github.com/nightfruit/reflux/blob/main/src/routes/stream/stream.controller.ts"

    %% Click Events for API Services
    click CatalogSvc "https://github.com/nightfruit/reflux/blob/main/src/routes/catalog/catalog.service.ts"
    click ManifestSvc "https://github.com/nightfruit/reflux/blob/main/src/routes/manifest/manifest.service.ts"
    click MetaSvc "https://github.com/nightfruit/reflux/blob/main/src/routes/meta/meta.service.ts"
    click StreamSvc "https://github.com/nightfruit/reflux/blob/main/src/routes/stream/stream.service.ts"

    %% Click Events for CDN Module & Services
    click CDN_Module "https://github.com/nightfruit/reflux/blob/main/src/modules/cdn/cdn.module.ts"
    click IndexService "https://github.com/nightfruit/reflux/blob/main/src/modules/cdn/services/indexing.service.ts"
    click ListService "https://github.com/nightfruit/reflux/blob/main/src/modules/cdn/services/list.service.ts"
    click MediaService "https://github.com/nightfruit/reflux/blob/main/src/modules/cdn/services/media.service.ts"
    click SourceService "https://github.com/nightfruit/reflux/blob/main/src/modules/cdn/services/source.service.ts"

    %% Click Events for Configuration Module
    click EnvModule "https://github.com/nightfruit/reflux/blob/main/src/config/env.module.ts"
    click EnvService "https://github.com/nightfruit/reflux/blob/main/src/config/env.service.ts"
    click EnvConfig "https://github.com/nightfruit/reflux/blob/main/src/config/env.ts"

    %% Click Event for Exception Handling
    click ExceptionHandler "https://github.com/nightfruit/reflux/blob/main/src/exceptions/too-many-requests.exception.ts"

    %% Click Events for Utility Functions
    click EncryptUtil "https://github.com/nightfruit/reflux/blob/main/src/utils/encryption.ts"
    click HashUtil "https://github.com/nightfruit/reflux/blob/main/src/utils/hashing.ts"
    click ProxyUtil "https://github.com/nightfruit/reflux/blob/main/src/utils/proxy.ts"
    click StringUtil "https://github.com/nightfruit/reflux/blob/main/src/utils/strings.ts"

    %% Click Events for Auxiliary Scripts
    click ProxyScript "https://github.com/nightfruit/reflux/blob/main/scripts/proxy.js"
    click WorkersScript "https://github.com/nightfruit/reflux/blob/main/scripts/workers.js"
```
