# chinachu/mirakurun architecture

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

- Interactive diagram: https://gitdiagram.com/chinachu/mirakurun
- Repository: https://github.com/chinachu/mirakurun

## Overview

##### Project type and purpose
- Mirakurun is a full-stack, Node.js-based RESTful API server with an integrated Web UI.
- Its primary purpose is to expose Japanese digital TV tuner functionality (tuner management, MPEG-2 TS parsing/filtering, EPG parsing, stream broadcasting) over HTTP (OpenAPI/Swagger), plus provide an admin React UI and CLI tools.

##### File structure
- Top-level directories:
- bin/ – command-line entrypoints (epgdump, cli) and postinstall scripts
- config/ – YAML configuration (channels, server, tuners)
- docker/ – Dockerfile, docker-compose.yml, container init scripts
- doc/ – user/deployment/configuration guides
- src/
– src/Mirakurun/ – core domain logic (Channel, EPG, TSDecoder, Job scheduling, Server abstraction, DB, logging, RPC)
– src/Mirakurun/api/ – HTTP route handlers (channels, services, programs, jobs, IPTV endpoints, config endpoints)
– src/server.ts – Express (or Koa) HTTP server bootstrap
– src/client.ts, src/remote.ts – built-in client library
– src/ui/ – React-based Web UI (components, entry HTML, Webpack config)
- test/ – unit/functional tests
- configuration/build files: package.json, tsconfig.json, tslint.json, webpack.config.js, azure-pipelines.yml

##### Insights from README
- Features emphasize REST API, process management, real-time EPG, multi-stream from a single tuning, IPTV server (M3U8/XMLTV), Swagger UI, Docker support.
- No monolithic GUI: UI is a separate React frontend served by the same Node.js process.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        direction TB
        BrowserUI["Browser Web UI"]:::frontend
        CLI_Tool["CLI Tool\n(bin/cli.sh)"]:::frontend
        EPG_CLI["EPG Dump CLI\n(bin/epgdump.js)"]:::frontend
    end

    %% Backend Layer
    subgraph "Backend Layer"
        direction TB
        API_Server["API Server\n(src/server.ts)"]:::backend
        API_Routes["API Route Handlers\n(src/Mirakurun/api/)"]:::backend
        Core_Modules["Core Domain Modules\n(src/Mirakurun/)"]:::backend
        Config_Loader["Configuration Loader\n(src/Mirakurun/config.ts)"]:::backend
        Job_Scheduler["Job Scheduler\n(src/Mirakurun/Job.ts)"]:::backend
        Job_API["Job Schedules API\n(src/Mirakurun/api/job-schedules.ts)"]:::backend
        Events_Stream["Event Stream Endpoint\n(src/Mirakurun/api/events/stream.ts)"]:::backend
        Client_Lib["Client Library\n(src/client.ts)"]:::backend
        Remote_Lib["Remote Library\n(src/remote.ts)"]:::backend
        EPG_Dump["EPG Dump Tool\n(src/epgdump.ts)"]:::backend
        Logging_Sub["Logging Subsystem\n(src/Mirakurun/log.ts)"]:::backend
        RPC_Sub["RPC Subsystem\n(src/Mirakurun/rpc.ts)"]:::backend
        Swagger_Def["Swagger/OpenAPI Definition\n(api.yml)"]:::optional
        Swagger_UI["Swagger UI\n(src/ui/redoc-ui.html)"]:::optional
    end

    %% Tuner Workers
    subgraph "Tuner Worker Processes"
        direction TB
        Worker_Process["Tuner Worker Process\n(src/Mirakurun/Tuner.ts)"]:::backend
        Device_Module["Tuner Device Module\n(src/Mirakurun/TunerDevice.ts)"]:::backend
    end

    %% Storage & Config
    subgraph "Storage & Config"
        direction TB
        Config_Store["Configuration Store\n(config/*.yml)"]:::storage
        Database["Database Layer\n(src/Mirakurun/db.ts)"]:::storage
    end

    %% External Entities
    subgraph "External Entities"
        direction TB
        Devices["External Tuner Devices"]:::hardware
        IPTV_Consumers["IPTV Consumers"]:::hardware
    end

    %% IPTV Output
    subgraph "IPTV Output"
        direction TB
        IPTV_Playlist["IPTV Playlist Endpoint\n(src/Mirakurun/api/iptv/playlist.ts)"]:::backend
        IPTV_XMLTV["XMLTV Endpoint\n(src/Mirakurun/api/iptv/xmltv.ts)"]:::backend
    end

    %% Connections
    BrowserUI -->|HTTP/REST| API_Server
    CLI_Tool -->|HTTP RPC| API_Server
    EPG_CLI -->|HTTP RPC| API_Server

    API_Server -->|routes| API_Routes
    API_Routes -->|calls| Core_Modules

    API_Server -->|loads| Config_Loader
    Config_Loader -->|reads/writes| Config_Store

    API_Server -->|persists| Database
    API_Server -->|logs| Logging_Sub
    API_Server -->|RPC calls| RPC_Sub

    API_Server -->|uses| Job_Scheduler
    Job_Scheduler -->|schedules| Core_Modules
    API_Server -->|exposes| Job_API
    API_Server -->|streams| Events_Stream

    API_Server -->|serves| Swagger_UI
    Swagger_UI -->|reads| Swagger_Def

    API_Server -->|provides| IPTV_Playlist
    API_Server -->|provides| IPTV_XMLTV
    IPTV_Playlist -->|HLS/TS| IPTV_Consumers
    IPTV_XMLTV -->|XMLTV| IPTV_Consumers

    API_Routes -->|invokes| Worker_Process
    Core_Modules -->|spawn via IPC/STDIO| Worker_Process
    Worker_Process -->|uses| Device_Module
    Device_Module -->|reads TS| Devices

    API_Server -->|bundles| Client_Lib
    API_Server -->|bundles| Remote_Lib
    API_Server -->|internal CLI| EPG_Dump

    %% Click Events
    click BrowserUI "https://github.com/chinachu/mirakurun/tree/master/src/ui/"
    click CLI_Tool "https://github.com/chinachu/mirakurun/blob/master/bin/cli.sh"
    click EPG_CLI "https://github.com/chinachu/mirakurun/blob/master/bin/epgdump.js"
    click API_Server "https://github.com/chinachu/mirakurun/blob/master/src/server.ts"
    click API_Routes "https://github.com/chinachu/mirakurun/tree/master/src/Mirakurun/api/"
    click Core_Modules "https://github.com/chinachu/mirakurun/tree/master/src/Mirakurun/"
    click Config_Loader "https://github.com/chinachu/mirakurun/blob/master/src/Mirakurun/config.ts"
    click Worker_Process "https://github.com/chinachu/mirakurun/blob/master/src/Mirakurun/Tuner.ts"
    click Device_Module "https://github.com/chinachu/mirakurun/blob/master/src/Mirakurun/TunerDevice.ts"
    click Config_Store "https://github.com/chinachu/mirakurun/blob/master/config/channels.yml"
    click Database "https://github.com/chinachu/mirakurun/blob/master/src/Mirakurun/db.ts"
    click Logging_Sub "https://github.com/chinachu/mirakurun/blob/master/src/Mirakurun/log.ts"
    click RPC_Sub "https://github.com/chinachu/mirakurun/blob/master/src/Mirakurun/rpc.ts"
    click IPTV_Playlist "https://github.com/chinachu/mirakurun/blob/master/src/Mirakurun/api/iptv/playlist.ts"
    click IPTV_XMLTV "https://github.com/chinachu/mirakurun/blob/master/src/Mirakurun/api/iptv/xmltv.ts"
    click Swagger_Def "https://github.com/chinachu/mirakurun/blob/master/api.yml"
    click Swagger_UI "https://github.com/chinachu/mirakurun/blob/master/src/ui/redoc-ui.html"
    click Client_Lib "https://github.com/chinachu/mirakurun/blob/master/src/client.ts"
    click Remote_Lib "https://github.com/chinachu/mirakurun/blob/master/src/remote.ts"
    click EPG_Dump "https://github.com/chinachu/mirakurun/blob/master/src/epgdump.ts"

    %% Styles
    classDef frontend fill:#AED6F1,stroke:#1B4F72,color:#1B2631
    classDef backend fill:#D6EAF8,stroke:#154360,color:#1B2631
    classDef storage fill:#ABEBC6,stroke:#186A3B,color:#1B2631
    classDef hardware fill:#F9E79F,stroke:#7D6608,color:#1B2631
    classDef optional stroke-dasharray: 5 5,fill:#F5EEF8,stroke:#7D3C98,color:#1B2631
```
