# hr3lxphr6j/bililive-go architecture

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

- Interactive diagram: https://gitdiagram.com/hr3lxphr6j/bililive-go
- Repository: https://github.com/hr3lxphr6j/bililive-go

## Overview

##### Main Components
- Live Recorder Core: This is the heart of the application. It includes the main command (src/cmd/bililive/bililive.go) that coordinates the live recording process.
- Live Providers Integration: Represented by the modules under src/live (with subfolders for each supported streaming platform like bilibili, douyu, huya, etc.). These components handle platform-specific details.
- Listeners & Event Handling: Under src/listeners and src/pkg/events, these modules monitor events (e.g., errors, status updates) and dispatch notifications or trigger actions.
- Recorder Module: Under src/recorders, which manages the actual recording process by interfacing with external tools such as ffmpeg.
- Configuration Management: Modules in src/configs (and the external config.yml/config.docker.yml files) handle runtime settings, including cookies and recording parameters.
- Web Server & API: Found in src/servers and the web application code in src/webapp. These parts expose functionalities via HTTP endpoints for configuration management, file access, and status updates.
- Metrics and Logging: src/metrics, src/log, and related packages capture and export monitoring data, supporting integration with Prometheus for metrics collection and Grafana for visualization.
- Utility Libraries: Generic helper functions (src/pkg/utils, src/pkg/counter, src/pkg/reader, etc.) that support the main workflow.

##### Relationships and Interactions
- The Application Server (HTTP server) interacts with the Webapp (React-based front end) to present configuration UIs, monitor recording statuses, and provide access to recorded videos.
- The Recorder Core coordinates with:
– Live Provider Modules: Each module (e.g., src/live/bilibili, src/live/douyu) implements platform-specific logic.
– Listener Modules that capture live events, errors, and notifications.
– The Recorder Module that uses ffmpeg (external dependency) to record streams.
- Configuration is centrally managed and read from config files, which can be updated either manually or via the web interface.
- Metrics and logs from the recorder modules, listeners, and server are sent out for monitoring. External systems like Prometheus and Grafana (supported via Docker-Compose configurations) retrieve, display, and alert on these metrics.
- Docker utilization: Dockerfiles, docker-compose.yml, and related scripts indicate containerization for the service and its dependencies (e.g., the Grafana dashboard and Prometheus).

##### Important Architectural Patterns and Technologies
- Modular Design: The clear separation in the file structure (live modules, listeners, recorders, web server, etc.) suggests a modular architecture that can be extended with additional streaming platforms.
- Separation of Concerns: Frontend (src/webapp) is distinct from the backend server (src/servers) and the business logic (other src subfolders).
- Event-driven Patterns: Listeners and event dispatchers (in src/pkg/events) indicate an event-driven mechanism guiding interactions between modules.
- Containerization and CI/CD: With Dockerfiles, docker-compose, GitHub workflows, Travis configuration, and Makefiles, the project is designed for automated builds, testing, and deployment.
- Dependency on External Tools: ffmpeg is the key external dependency for handling media streams.

##### Specific Elements
- Web Interface (React front end with components and API endpoints)
- Application Server (Golang backend handling HTTP requests and managing configuration)
- Live Recorder Engine that contains sub-components:
- Live Providers Modules (e.g., Bilibili, Douyu, etc.)
- Listener/Event Dispatcher Subsystem
- Recorder Component (integrating with ffmpeg)
- Utility/Support Components (Configuration, Metrics, Logging)
- Deployment & External Dependencies (Docker container environment, ffmpeg, Prometheus, Grafana)
- Data flow from the web UI to the backend server/API.
- Event/command flow from the recorder engine to the live providers.
- Interactions between components (e.g., configuration being read by the recorder and updated by the web UI).
- Communication with external tools (e.g., how recorded streams are processed by ffmpeg and metrics forwarded to Prometheus).
- Core business logic modules (engine, listeners, recorders)
- UI components (frontend webapp)
- External services and dependencies

##### Final Advice
- Modularity and separation of concerns so that new live platforms or enhancements in the web interface can be integrated into the system without major changes to the core architecture.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Frontend Layer
    subgraph "Frontend Layer"
        Web["Web Interface (React Frontend)"]:::frontend
    end

    %% Backend Layer
    subgraph "Backend Layer"
        Server["Application Server & API (Golang)"]:::backend
        
        subgraph "Live Recorder Engine"
            Core["Live Recorder Core"]:::core
            Providers["Live Providers Integration"]:::providers
            Listeners["Listeners & Event Handling"]:::listeners
            Recorder["Recorder Module (ffmpeg)"]:::recorder
        end

        Config["Configuration Management"]:::config
        Metrics["Metrics & Logging"]:::metrics
        Utils["Utility Libraries"]:::utils
    end

    %% Deployment & External Dependencies
    subgraph "Deployment & External"
        Docker["Docker Container Environment"]:::external
        Prometheus["Prometheus (Monitoring)"]:::external
        Grafana["Grafana (Dashboard)"]:::external
        FFMPEG["ffmpeg (Media Processing)"]:::external
    end

    %% Connections between components
    Web -->|"callsAPI"| Server
    Server -->|"reads/writes"| Config
    Server -->|"sendsMetrics"| Metrics
    Server -->|"initiates"| Core

    Core -->|"integrates"| Providers
    Core -->|"triggers"| Listeners
    Core -->|"controls"| Recorder

    Recorder -->|"uses"| FFMPEG
    Metrics -->|"exportedTo"| Prometheus
    Prometheus -->|"visualizedBy"| Grafana

    Docker -->|"runs"| Server
    Docker -->|"runs"| Core
    Docker -->|"runs"| FFMPEG

    Web ---|"updatesConfig"| Config

    %% Click Events for components from file mapping
    click Core "https://github.com/hr3lxphr6j/bililive-go/blob/master/src/cmd/bililive/bililive.go"
    click Providers "https://github.com/hr3lxphr6j/bililive-go/tree/master/src/live"
    click Listeners "https://github.com/hr3lxphr6j/bililive-go/tree/master/src/listeners"
    click Recorder "https://github.com/hr3lxphr6j/bililive-go/tree/master/src/recorders"
    click Config "https://github.com/hr3lxphr6j/bililive-go/tree/master/src/configs"
    click Server "https://github.com/hr3lxphr6j/bililive-go/tree/master/src/servers"
    click Web "https://github.com/hr3lxphr6j/bililive-go/tree/master/src/webapp"
    click Metrics "https://github.com/hr3lxphr6j/bililive-go/tree/master/src/metrics"
    click Utils "https://github.com/hr3lxphr6j/bililive-go/tree/master/src/pkg"

    %% Styles
    classDef frontend fill:#ffcccc,stroke:#333,stroke-width:2px;
    classDef backend fill:#fff2cc,stroke:#333,stroke-width:2px;
    classDef core fill:#cce5ff,stroke:#333,stroke-width:2px;
    classDef providers fill:#d5f5e3,stroke:#333,stroke-width:2px;
    classDef listeners fill:#f9e79f,stroke:#333,stroke-width:2px;
    classDef recorder fill:#fadbd8,stroke:#333,stroke-width:2px;
    classDef config fill:#d6eaf8,stroke:#333,stroke-width:2px;
    classDef metrics fill:#e8daef,stroke:#333,stroke-width:2px;
    classDef utils fill:#fcf3cf,stroke:#333,stroke-width:2px;
    classDef external fill:#d7dbdd,stroke:#333,stroke-width:2px;
```
