# mtvpls/moontvplus architecture

> Architecture diagram of the GitHub repository mtvpls/moontvplus, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mtvpls/moontvplus
- Repository: https://github.com/mtvpls/moontvplus

## Overview

#### Purpose and Entrypoints
MoonTVPlus is a self-hosted, configurable media aggregation platform: video discovery/playback, live TV, private libraries, music, manga, books, downloads, and TV clients. It ships without sources; administrators configure source APIs and integrations.
- Next.js App Router UI/API: `src/app/page.tsx`, `src/app/layout.tsx`, `src/app/api`
- Node startup and optional persistent services: `server.js`, `start.js`, `server/watch-room-standalone-server.js`
- Android TV wrapper/remote receiver: `apps/android-tv/app/src/main/java/com/moontvplus/tv/MainActivity.java`, `apps/android-tv/app/src/main/java/com/moontvplus/tv/LocalRemoteServer.java`

#### Web Application and Client Modes
The primary runtime is Next.js 14/TypeScript, serving responsive browser, TV-oriented, and feature-specific route trees. Shared layout/providers establish site configuration, theme, session refresh, and watch-room state.
- General browsing/playback: `src/app/search/page.tsx`, `src/app/play/page.tsx`
- TV-specific UX and remote controls: `src/app/tv/layout.tsx`, `src/components/tv/TVLayout.tsx`, `src/components/tv/TVRemoteReceiver.tsx`
- Specialized content surfaces: `src/app/music/page.tsx`, `src/app/manga/page.tsx`, `src/app/books/page.tsx`

#### API Gateway, Source Execution, and Playback
Route handlers form the backend boundary between clients and configured/upstream media providers. Search/detail endpoints aggregate configured VOD sources; source scripts allow custom source/search/detail/play logic. Proxy endpoints mediate HLS manifests, segments, keys, images, and video streams—an important SSRF/security boundary.
- Search and configured source APIs: `src/app/api/search/route.ts`, `src/app/api/source-search/search/route.ts`, `src/lib/source-script.ts`
- Detail/play parsing: `src/app/api/detail/route.ts`, `src/lib/fetchVideoDetail.ts`, `src/lib/video-parser.ts`
- Streaming proxies and protections: `src/app/api/proxy/m3u8/route.ts`, `src/app/api/proxy/segment/route.ts`, `src/lib/server/ssrf.ts`

#### Identity, Authorization, and Administration
Authentication supports password login, registration, refresh tokens, QR/device flows, and OIDC; middleware enforces request access. The admin API owns mutable instance configuration, source definitions, users, migration, and external-service setup.
- Auth/session boundary: `src/middleware.ts`, `src/lib/auth.ts`, `src/lib/middleware-auth.ts`
- Login/device/OIDC flows: `src/app/api/login/route.ts`, `src/app/api/auth/devices/route.ts`, `src/app/api/auth/oidc/login/route.ts`
- Administration/configuration: `src/app/admin/page.tsx`, `src/app/api/admin/config/route.ts`, `src/app/api/admin/source/route.ts`

#### Persistence and State
A storage abstraction supports Cloudflare D1/SQLite, PostgreSQL, Redis, Kvrocks, and Upstash. It persists users, configuration, favorites, playback records, library metadata, and feature state; SQL migrations define relational variants. Browser IndexedDB is used for local video/cache concerns.
- Database selection/adapters: `src/lib/db.ts`, `src/lib/d1-adapter.ts`, `src/lib/postgres-adapter.ts`
- Redis-family implementations: `src/lib/redis.db.ts`, `src/lib/kvrocks.db.ts`, `src/lib/upstash.db.ts`
- Schema evolution: `migrations/001_initial_schema.sql`, `migrations/postgres/001_initial_schema.sql`

#### Integrations and Media Domains
Domain adapters connect external metadata/services and normalize them behind API routes: TMDB, Douban, Bangumi, danmaku backend, Emby/OpenList/Xiaoya libraries, cloud disks, Telegram, and web push. Caches reduce upstream dependency pressure.
- Metadata and danmaku: `src/lib/tmdb.client.ts`, `src/lib/douban.client.ts`, `src/lib/danmaku/api.ts`
- Private-library integrations: `src/lib/emby.client.ts`, `src/lib/openlist.client.ts`, `src/lib/xiaoya.client.ts`
- Cloud disk playback/session resolution: `src/lib/netdisk/source.ts`, `src/lib/netdisk/quark.client.ts`, `src/app/api/netdisk/quark/play/route.ts`

#### Stateful/Background Features and Deployment
Watch rooms use Socket.IO/WebSocket infrastructure either embedded in Node or externalized for serverless deployments. Cron, offline downloads, subscriptions, notifications, and migrations are server-side operational flows. Docker is the full Node deployment path; Vercel, Cloudflare Workers/D1, and EdgeOne use platform-specific configuration/build adapters.
- Watch-room coordination: `src/lib/watch-room-server.ts`, `src/lib/watch-room-socket.ts`, `server/watch-room-standalone-server.js`
- Background operational routes/services: `src/app/api/cron/[password]/route.ts`, `src/lib/offline-downloader.ts`, `src/lib/notification-dispatch.ts`
- Deployment boundaries: `Dockerfile`, `wrangler.toml`, `open-next.config.ts`, `edgeone.json`

Main entry points: [`src/app/api`](https://github.com/mtvpls/moontvplus/tree/main/src/app/api) (API route handlers), [`src/lib/db.ts`](https://github.com/mtvpls/moontvplus/blob/main/src/lib/db.ts) (Storage abstraction), [`src/app/layout.tsx`](https://github.com/mtvpls/moontvplus/blob/main/src/app/layout.tsx) (App Router UI), [`server.js`](https://github.com/mtvpls/moontvplus/blob/main/server.js) (Node runtime)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_clients["Client runtimes"]
  node_browser(("Browser clients<br/>web client<br/>[page.tsx]"))
  node_android_tv["Android TV wrapper<br/>Android client<br/>[MainActivity.java]"]
end

subgraph group_web["Next.js application"]
  node_next_ui["App Router UI<br/>Next.js frontend<br/>[layout.tsx]"]
  node_tv_shell["TV shell and remote<br/>TV UI<br/>[layout.tsx]"]
  node_api_gateway{{"API route handlers<br/>Next.js API"}}
  node_auth["Auth and access control<br/>auth middleware<br/>[middleware.ts]"]
  node_admin["Administration<br/>admin UI/API<br/>[route.ts]"]
  node_source_execution["Configured source execution<br/>source script engine<br/>[source-script.ts]"]
  node_playback["Detail and playback parser<br/>media resolver<br/>[video-parser.ts]"]
  node_stream_proxy{{"Secure stream proxy<br/>HLS/media proxy<br/>[route.ts]"}}
end

subgraph group_services["Stateful services"]
  node_node_runtime["Node runtime<br/>startup server<br/>[server.js]"]
  node_watch_rooms["Watch-room coordination<br/>Socket.IO service"]
  node_operations["Background operations<br/>server operations"]
end

subgraph group_data["Storage and integrations"]
  node_storage[("Storage abstraction<br/>database adapter<br/>[db.ts]")]
  node_sql_schema["Relational migrations<br/>SQL schema"]
  node_metadata["Metadata and danmaku<br/>external adapters<br/>[tmdb.client.ts]"]
  node_private_libraries["Private library adapters<br/>library integrations<br/>[emby.client.ts]"]
  node_netdisk["Cloud disk resolution<br/>netdisk integration<br/>[source.ts]"]
end

subgraph group_deploy["Deployment targets"]
  node_deployment["Platform deployment adapters<br/>deployment configuration"]
end

node_browser -->|"renders UI"| node_next_ui
node_android_tv -->|"hosts TV UX"| node_tv_shell
node_next_ui -->|"enters TV mode"| node_tv_shell
node_next_ui -->|"calls"| node_api_gateway
node_tv_shell -->|"uses APIs"| node_api_gateway
node_api_gateway -->|"access checks"| node_auth
node_api_gateway -->|"admin endpoints"| node_admin
node_admin -->|"updates configuration"| node_storage
node_api_gateway -->|"searches configured sources"| node_source_execution
node_api_gateway -->|"resolves detail/playback"| node_playback
node_playback -->|"routes media through"| node_stream_proxy
node_source_execution -->|"enriches content"| node_metadata
node_api_gateway -->|"accesses libraries"| node_private_libraries
node_api_gateway -->|"resolves cloud playback"| node_netdisk
node_api_gateway -->|"reads and writes state"| node_storage
node_storage -.->|"relational variants"| node_sql_schema
node_node_runtime -->|"serves"| node_api_gateway
node_node_runtime -.->|"may embed"| node_watch_rooms
node_next_ui -->|"synchronizes rooms"| node_watch_rooms
node_operations -->|"persists job state"| node_storage
node_deployment -->|"Node deployment"| node_node_runtime
node_deployment -.->|"platform storage options"| node_storage

click node_browser "https://github.com/mtvpls/moontvplus/blob/main/src/app/page.tsx"
click node_android_tv "https://github.com/mtvpls/moontvplus/blob/main/apps/android-tv/app/src/gecko/java/com/moontvplus/tv/MainActivity.java"
click node_next_ui "https://github.com/mtvpls/moontvplus/blob/main/src/app/layout.tsx"
click node_tv_shell "https://github.com/mtvpls/moontvplus/blob/main/src/app/tv/layout.tsx"
click node_api_gateway "https://github.com/mtvpls/moontvplus/tree/main/src/app/api"
click node_auth "https://github.com/mtvpls/moontvplus/blob/main/src/middleware.ts"
click node_admin "https://github.com/mtvpls/moontvplus/blob/main/src/app/api/admin/config/route.ts"
click node_source_execution "https://github.com/mtvpls/moontvplus/blob/main/src/lib/source-script.ts"
click node_playback "https://github.com/mtvpls/moontvplus/blob/main/src/lib/video-parser.ts"
click node_stream_proxy "https://github.com/mtvpls/moontvplus/blob/main/src/app/api/proxy/m3u8/route.ts"
click node_node_runtime "https://github.com/mtvpls/moontvplus/blob/main/server.js"
click node_watch_rooms "https://github.com/mtvpls/moontvplus/blob/main/src/lib/watch-room-server.ts"
click node_operations "https://github.com/mtvpls/moontvplus/blob/main/src/lib/offline-downloader.ts"
click node_storage "https://github.com/mtvpls/moontvplus/blob/main/src/lib/db.ts"
click node_sql_schema "https://github.com/mtvpls/moontvplus/blob/main/migrations/001_initial_schema.sql"
click node_metadata "https://github.com/mtvpls/moontvplus/blob/main/src/lib/tmdb.client.ts"
click node_private_libraries "https://github.com/mtvpls/moontvplus/blob/main/src/lib/emby.client.ts"
click node_netdisk "https://github.com/mtvpls/moontvplus/blob/main/src/lib/netdisk/source.ts"
click node_deployment "https://github.com/mtvpls/moontvplus/tree/main/Dockerfile"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_browser,node_android_tv toneBlue
class node_next_ui,node_tv_shell,node_api_gateway,node_auth,node_admin,node_source_execution,node_playback,node_stream_proxy toneAmber
class node_node_runtime,node_watch_rooms,node_operations toneMint
class node_storage,node_sql_schema,node_metadata,node_private_libraries,node_netdisk toneRose
class node_deployment toneIndigo
```

## Components

### Client runtimes

- [Browser clients](https://github.com/mtvpls/moontvplus/blob/main/src/app/page.tsx): web client · Responsive, TV, and domain-specific interfaces. · `src/app/page.tsx`
- [Android TV wrapper](https://github.com/mtvpls/moontvplus/blob/main/apps/android-tv/app/src/gecko/java/com/moontvplus/tv/MainActivity.java): Android client · Hosts the TV UI and local remote receiver. · `apps/android-tv/app/src/gecko/java/com/moontvplus/tv/MainActivity.java`

### Next.js application

- [App Router UI](https://github.com/mtvpls/moontvplus/blob/main/src/app/layout.tsx): Next.js frontend · Provides browsing, playback, music, manga, books, and TV routes. · `src/app/layout.tsx`
- [TV shell and remote](https://github.com/mtvpls/moontvplus/blob/main/src/app/tv/layout.tsx): TV UI · `src/app/tv/layout.tsx`
- [API route handlers](https://github.com/mtvpls/moontvplus/tree/main/src/app/api): Next.js API · Backend boundary for clients and media domains. · `src/app/api`
- [Auth and access control](https://github.com/mtvpls/moontvplus/blob/main/src/middleware.ts): auth middleware · Enforces sessions and supports password, device, and OIDC flows. · `src/middleware.ts`
- [Administration](https://github.com/mtvpls/moontvplus/blob/main/src/app/api/admin/config/route.ts): admin UI/API · Manages instance configuration, sources, users, and integrations. · `src/app/api/admin/config/route.ts`
- [Configured source execution](https://github.com/mtvpls/moontvplus/blob/main/src/lib/source-script.ts): source script engine · Runs custom search, detail, and playback logic. · `src/lib/source-script.ts`
- [Detail and playback parser](https://github.com/mtvpls/moontvplus/blob/main/src/lib/video-parser.ts): media resolver · `src/lib/video-parser.ts`
- [Secure stream proxy](https://github.com/mtvpls/moontvplus/blob/main/src/app/api/proxy/m3u8/route.ts): HLS/media proxy · Proxies manifests, segments, keys, images, and streams with SSRF protection. · `src/app/api/proxy/m3u8/route.ts`

### Stateful services

- [Node runtime](https://github.com/mtvpls/moontvplus/blob/main/server.js): startup server · Starts Next.js and optional persistent services. · `server.js`
- [Watch-room coordination](https://github.com/mtvpls/moontvplus/blob/main/src/lib/watch-room-server.ts): Socket.IO service · Can run embedded or as a standalone server. · `src/lib/watch-room-server.ts`
- [Background operations](https://github.com/mtvpls/moontvplus/blob/main/src/lib/offline-downloader.ts): server operations · Runs cron work, offline downloads, and notifications. · `src/lib/offline-downloader.ts`

### Storage and integrations

- [Storage abstraction](https://github.com/mtvpls/moontvplus/blob/main/src/lib/db.ts): database adapter · Persists users, configuration, libraries, favorites, playback, and feature state. · `src/lib/db.ts`
- [Relational migrations](https://github.com/mtvpls/moontvplus/blob/main/migrations/001_initial_schema.sql): SQL schema · `migrations/001_initial_schema.sql`
- [Metadata and danmaku](https://github.com/mtvpls/moontvplus/blob/main/src/lib/tmdb.client.ts): external adapters · Normalizes external metadata and comment services. · `src/lib/tmdb.client.ts`
- [Private library adapters](https://github.com/mtvpls/moontvplus/blob/main/src/lib/emby.client.ts): library integrations · Connects Emby, OpenList, and Xiaoya libraries. · `src/lib/emby.client.ts`
- [Cloud disk resolution](https://github.com/mtvpls/moontvplus/blob/main/src/lib/netdisk/source.ts): netdisk integration · Resolves cloud-disk sessions and playable media. · `src/lib/netdisk/source.ts`

### Deployment targets

- [Platform deployment adapters](https://github.com/mtvpls/moontvplus/tree/main/Dockerfile): deployment configuration · Supports Docker, Workers/D1, Vercel, and EdgeOne deployments. · `Dockerfile`

## Connections

- Browser clients → App Router UI: renders UI
- Android TV wrapper → TV shell and remote: hosts TV UX
- App Router UI → TV shell and remote: enters TV mode
- App Router UI → API route handlers: calls
- TV shell and remote → API route handlers: uses APIs
- API route handlers → Auth and access control: access checks
- API route handlers → Administration: admin endpoints
- Administration → Storage abstraction: updates configuration
- API route handlers → Configured source execution: searches configured sources
- API route handlers → Detail and playback parser: resolves detail/playback
- Detail and playback parser → Secure stream proxy: routes media through
- Configured source execution → Metadata and danmaku: enriches content
- API route handlers → Private library adapters: accesses libraries
- API route handlers → Cloud disk resolution: resolves cloud playback
- API route handlers → Storage abstraction: reads and writes state
- Storage abstraction → Relational migrations: relational variants
- Node runtime → API route handlers: serves
- Node runtime → Watch-room coordination: may embed
- App Router UI → Watch-room coordination: synchronizes rooms
- Background operations → Storage abstraction: persists job state
- Platform deployment adapters → Node runtime: Node deployment
- Platform deployment adapters → Storage abstraction: platform storage options
