# remnawave/backend architecture

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

- Interactive diagram: https://gitdiagram.com/remnawave/backend
- Repository: https://github.com/remnawave/backend

## Overview

##### Project type and purpose
- This is a NestJS-based modular monolith providing the entire backend for the Remnawave panel.
- It exposes a HTTP/REST API, a CLI, scheduled tasks, and background processors. It uses Prisma/​PostgreSQL for persistence, Redis/Bull for job queues, and integrates with external services (Telegram Bot API, Prometheus, webhooks).

##### File structure at a high level
- Top-level:
- Docker & compose files for local, DB, production
- src/main.ts → starts Nest app
- src/modules/* → domain modules (auth, users, hosts, nodes, subscription, system, etc.)
- src/common/* → cross-cutting utilities (config, database, guards, exception filters, helpers)
- src/integration-modules/* → optional adapters (Prometheus metrics, Telegram Bot, webhook)
- src/queue/* → Bull queue definitions, processors, services
- src/scheduler/* → cron-style task enqueuers and interval tasks
- src/bin/* → CLI tools for docs generation and other command-line operations
- libs/contract → shared DTOs, commands, constants and schemas for frontend & backend
- Persistence layer: prisma/schema.prisma + migrations + prisma.service
- Config: @nestjs/config schemas, .env samples
- Build & CI pipelines: GitHub workflows, Dockerfiles

##### README for context
- Confirms that this is the backend service for the Remnawave panel.

##### Main components
A. API Server (NestJS Application)
- HTTP Controller Layer (src/modules/*/…controllers)
- Business & Domain Services (services, converters, builders)
- CQRS Commands & Queries (commands/handlers + queries/handlers)
- Shared modules: config, database (Prisma), exception filters, guards, interceptors
B. Persistence (PostgreSQL via Prisma)
C. Job Queue (Redis + Bull)
- Queue module & service
- Multiple processors (bulk operations, notifications, node-health, record usage, etc.)
D. Scheduler
- Cron/interval tasks that enqueue jobs into Redis queues
E. CLI Tools
- bin/cli and bin/gen-doc for manual invocation
F. Integration Modules (loaded into Nest as modules)
- Prometheus reporter → pushes metrics to Prometheus or exports via Pull endpoint
- Telegram Bot → communicates with Telegram Bot API
- Webhook module → sends HTTP callbacks to configured endpoints
G. External Services
- Telegram Bot API
- Prometheus server
- Webhook targets
- (Optional) Cloudflare, third-party APIs as configured

##### Relationships and data flows
1. Clients (Browser/Panel frontend) → HTTP(S) → API Server (controllers)
2. API Server ↔ Database (PostgreSQL via Prisma service)
3. API Server → Redis queues (via QueueService) → background processors → Database or external calls
4. Scheduler → Redis queues (schedules recurring jobs)
5. CLI → direct internal Nest command handlers → Database or queue
6. Integration modules subscribe to internal events or are invoked by controllers → External Services
7. Processors may emit application events (e.g., user-expired) for other modules to handle

##### Architectural patterns and principles
- Modular Monolith: clear separation by domain modules under src/modules
- CQRS: separate commands vs. queries in libs/contract and modules
- Event-driven: Nest’s EventEmitter for cross-module events; queue for async jobs
- Dependency Injection: NestJS providers for services, repositories, converters
- Layered design: Controllers → Services → Repositories → Database
- Infrastructure abstraction: common database, config, and queue modules

##### Relevant technologies & frameworks
- Node.js, TypeScript, NestJS (modules, controllers, providers, guards, interceptors)
- Prisma ORM + PostgreSQL
- Bull (or BullMQ) + Redis
- GitHub Actions for CI/CD, Docker, docker-compose for local/production
- @nestjs/Config for environment & schemas
- Swagger/OpenAPI (push-openapi-specs workflow)
- Hygen for code scaffolding

##### Detail is essential
- Piece: controllers, services, repositories, processors, tasks.
- Do not over-abstract—split into as many logical components as needed.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Clients
    Client["Client (Browser/Panel)"]:::external

    %% API Server
    subgraph "API Server (NestJS Application)" 
        direction TB
        MainEntry["main.ts"]:::core
        AppModule["app.module.ts"]:::core

        subgraph "HTTP Layer"
            direction TB
            Controllers["Controllers (src/modules/**/controllers)"]:::core
        end

        subgraph "Domain Modules"
            direction TB
            AuthModule["Auth Module"]:::core
            UsersModule["Users Module"]:::core
            HostsModule["Hosts Module"]:::core
            NodesModule["Nodes Module"]:::core
            SubscriptionModule["Subscription Module"]:::core
            SystemModule["System Module"]:::core
        end

        subgraph "Common Layer"
            direction TB
            Config["Config"]:::core
            DatabaseModule["Prisma Module"]:::persistence
            PrismaService["Prisma Service"]:::persistence
            Guards["Guards & Filters"]:::core
            Helpers["Helpers & Decorators"]:::core
        end
    end

    %% Persistence
    Postgres[(PostgreSQL)]:::persistence
    PrismaSchema["schema.prisma & Migrations"]:::persistence

    %% Queue & Workers
    subgraph "Job Queue (Redis + Bull)"
        direction TB
        Redis[(Redis)]:::queue
        QueueModule["queue.module.ts"]:::queue
        QueueService["queue.service.ts"]:::queue
        QueueEnum["queue.enum.ts"]:::queue
        ProcessorsRoot["processors.root.module.ts"]:::queue
        ProcessorsBootstrap["processors.ts"]:::queue
        Processors["Processors (src/queue/**)"]:::queue
    end

    %% Scheduler
    subgraph "Scheduler"
        direction TB
        SchedulerRoot["scheduler.root.module.ts"]:::queue
        SchedulerBootstrap["scheduler.ts"]:::queue
        SchedulerModule["scheduler.module.ts"]:::queue
        Tasks["Tasks (src/scheduler/**)"]:::queue
    end

    %% CLI Tools
    subgraph "CLI Tools"
        direction TB
        CLIBin["cli.ts"]:::core
        GenDoc["gen-doc.ts"]:::core
    end

    %% Integration Modules
    subgraph "Integration Modules"
        direction TB
        PromReporter["Prometheus Reporter"]:::integration
        TelegramBot["Telegram Bot Module"]:::integration
        WebhookModule["Webhook Module"]:::integration
    end

    %% Shared Contract
    Contract["libs/contract"]:::core

    %% External Services
    subgraph "External Services"
        direction TB
        TelegramAPI["Telegram Bot API"]:::external
        PromServer["Prometheus Server"]:::external
        WebhookTargets["Webhook Targets"]:::external
    end

    %% Connections
    Client -->|"HTTP(S)"| Controllers
    MainEntry --> AppModule
    AppModule --> Controllers
    AppModule --> AuthModule
    AppModule --> UsersModule
    AppModule --> HostsModule
    AppModule --> NodesModule
    AppModule --> SubscriptionModule
    AppModule --> SystemModule
    AppModule --> Config
    AppModule --> DatabaseModule
    DatabaseModule --> PrismaService
    PrismaService --> PrismaSchema
    Controllers -->|uses| DomainModules[Domain Modules]
    DomainModules -->|queries/commands| Contract
    Controllers -->|DTOs & Commands| Contract

    AppModule --> QueueModule
    QueueModule --> QueueService
    QueueService --> Redis
    QueueService --> QueueEnum
    QueueService --> Processors
    Redis --> Processors

    Scheduler -->|enqueues jobs| Redis
    SchedulerBootstrap --> SchedulerModule
    SchedulerModule --> Tasks

    CLIBin -->|invokes| Contract
    GenDoc -->|generates| Contract

    Controllers -->|invokes| PromReporter
    PromReporter -->|pushes/pulls| PromServer
    Controllers -->|invokes| TelegramBot
    TelegramBot -->|calls| TelegramAPI
    Controllers -->|invokes| WebhookModule
    WebhookModule -->|posts| WebhookTargets
    Processors -->|invokes| External[External Calls]:::external

    %% Click Events
    click MainEntry "https://github.com/remnawave/backend/blob/main/src/main.ts"
    click AppModule "https://github.com/remnawave/backend/blob/main/src/app.module.ts"
    click Controllers "https://github.com/remnawave/backend/tree/main/src/modules"
    click AuthModule "https://github.com/remnawave/backend/tree/main/src/modules/auth"
    click UsersModule "https://github.com/remnawave/backend/tree/main/src/modules/users"
    click HostsModule "https://github.com/remnawave/backend/tree/main/src/modules/hosts"
    click NodesModule "https://github.com/remnawave/backend/tree/main/src/modules/nodes"
    click SubscriptionModule "https://github.com/remnawave/backend/tree/main/src/modules/subscription"
    click SystemModule "https://github.com/remnawave/backend/tree/main/src/modules/system"
    click Config "https://github.com/remnawave/backend/tree/main/src/common"
    click DatabaseModule "https://github.com/remnawave/backend/blob/main/src/common/database/prisma.module.ts"
    click PrismaService "https://github.com/remnawave/backend/blob/main/src/common/database/prisma.service.ts"
    click PrismaSchema "https://github.com/remnawave/backend/blob/main/prisma/schema.prisma"
    click QueueModule "https://github.com/remnawave/backend/blob/main/src/queue/queue.module.ts"
    click QueueService "https://github.com/remnawave/backend/blob/main/src/queue/queue.service.ts"
    click QueueEnum "https://github.com/remnawave/backend/blob/main/src/queue/queue.enum.ts"
    click ProcessorsRoot "https://github.com/remnawave/backend/blob/main/src/bin/processors/processors.root.module.ts"
    click ProcessorsBootstrap "https://github.com/remnawave/backend/blob/main/src/bin/processors/processors.ts"
    click Processors "https://github.com/remnawave/backend/tree/main/src/queue"
    click SchedulerRoot "https://github.com/remnawave/backend/blob/main/src/bin/scheduler/scheduler.root.module.ts"
    click SchedulerBootstrap "https://github.com/remnawave/backend/blob/main/src/bin/scheduler/scheduler.ts"
    click SchedulerModule "https://github.com/remnawave/backend/blob/main/src/scheduler/scheduler.module.ts"
    click Tasks "https://github.com/remnawave/backend/tree/main/src/scheduler"
    click CLIBin "https://github.com/remnawave/backend/blob/main/src/bin/cli/cli.ts"
    click GenDoc "https://github.com/remnawave/backend/blob/main/src/bin/gen-doc/gen-doc.ts"
    click PromReporter "https://github.com/remnawave/backend/tree/main/src/integration-modules/prometheus-reporter"
    click TelegramBot "https://github.com/remnawave/backend/tree/main/src/integration-modules/telegram-bot"
    click WebhookModule "https://github.com/remnawave/backend/tree/main/src/integration-modules/webhook-module"
    click Contract "https://github.com/remnawave/backend/tree/main/libs/contract"

    %% Styles
    classDef core fill:#D0E6FF,stroke:#0366D6,stroke-width:1px
    classDef persistence fill:#DFFFE0,stroke:#228B22,stroke-width:1px
    classDef queue fill:#FFEFD5,stroke:#FF8C00,stroke-width:1px
    classDef integration fill:#E6DAF8,stroke:#800080,stroke-width:1px
    classDef external fill:#F0F0F0,stroke:#666666,stroke-width:1px
```
