How mtvpls/moontvplus works
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: api API route handlers · db.ts Storage abstraction · layout.tsx App Router UI · server.js Node runtime
Components 19
Client runtimes
- Browser clients · Responsive, TV, and domain-specific interfaces.
src/app/page.tsx - Android TV wrapper · 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 · Provides browsing, playback, music, manga, books, and TV routes.
src/app/layout.tsx - TV shell and remote
src/app/tv/layout.tsx - API route handlers · Backend boundary for clients and media domains.
src/app/api - Auth and access control · Enforces sessions and supports password, device, and OIDC flows.
src/middleware.ts - Administration · Manages instance configuration, sources, users, and integrations.
src/app/api/admin/config/route.ts - Configured source execution · Runs custom search, detail, and playback logic.
src/lib/source-script.ts - Detail and playback parser
src/lib/video-parser.ts - Secure stream proxy · Proxies manifests, segments, keys, images, and streams with SSRF protection.
src/app/api/proxy/m3u8/route.ts
Stateful services
- Node runtime · Starts Next.js and optional persistent services.
server.js - Watch-room coordination · Can run embedded or as a standalone server.
src/lib/watch-room-server.ts - Background operations · Runs cron work, offline downloads, and notifications.
src/lib/offline-downloader.ts
Storage and integrations
- Storage abstraction · Persists users, configuration, libraries, favorites, playback, and feature state.
src/lib/db.ts - Relational migrations
migrations/001_initial_schema.sql - Metadata and danmaku · Normalizes external metadata and comment services.
src/lib/tmdb.client.ts - Private library adapters · Connects Emby, OpenList, and Xiaoya libraries.
src/lib/emby.client.ts - Cloud disk resolution · Resolves cloud-disk sessions and playable media.
src/lib/netdisk/source.ts
Deployment targets
- Platform deployment adapters · Supports Docker, Workers/D1, Vercel, and EdgeOne deployments.
Dockerfile
Connections 22
- 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