How cplieger/subflux works
1. What this repo is
Subflux is a single Go service that combines a CLI, an HTTP server, a subtitle-search/download worker, and a subtitle-sync engine. It is designed to manage Sonarr/Radarr-managed media libraries, query third-party subtitle providers, and write synced .srt files back to the media filesystem.
2. Top-level execution model
The main entry points are the Go binaries in main.go, cli.go, and the cmd/wire-codegen helper. At runtime, the same process can act as:
- the long-running web/API server,
- a background scanner/scheduler,
- and a CLI client that talks to a remote instance via SUBFLUX_URL.
The Docker image is a static, nonroot distroless container, so the app is intentionally packaged as one self-contained service.
3. Configuration and dependency wiring
Configuration is loaded, validated, and expanded in internal/config, with schema metadata in internal/config/schema. The repository uses explicit wiring (internal/wiring, plus generated wiring support) to assemble services from config and storage backends. That makes configuration a first-class boundary: load/validate config first, then build the server, providers, search engine, and auth subsystems from it.
4. Persistent state boundary
State is split between a file-backed config and embedded key-value stores. The internal/boltstore package provides the bbolt-backed storage engine, while internal/store/kv and internal/store/storetest define lower-level KV abstractions and contracts. On top of that sit domain-specific stores such as internal/authstore for users/sessions/API keys/passkeys/OIDC state and internal/cache / internal/server/polling caches for runtime behavior.
5. Media discovery and orchestration
The core operational loop lives in internal/search and internal/server/scanning. It consumes media inventory from Sonarr/Radarr history plus scheduled full-library scans, builds target state, and decides what subtitles are missing or need upgrading. The scanner is optimized for bounded, batch-oriented work rather than unbounded background crawling, which is central to the repo’s low-memory design.
6. Sonarr/Radarr integration
internal/arrapi is the dedicated integration layer for Arr APIs. It fetches library/history/notify data and translates Arr-specific responses into Subflux’s internal media model. This is the main ingress for real-world media events: import history changes, wanted items, and notifications flow from Arr into the search/scanning pipeline.
7. Provider architecture
Subtitle sources are isolated under internal/provider/* and registered through internal/provider/registry. Providers include OpenSubtitles, SubDL, SubSource, YIFY Subtitles, BetaSeries, AnimeTosho, Gestdown, HDBits, embedded/ffprobe-based sources, and archive helpers. The provider layer handles HTTP access, retries, provider-specific filters, and download normalization, so the rest of the system treats providers uniformly.
8. Search, scoring, and selection
internal/search performs the main subtitle matching pipeline. It first applies identity checks using stable media identifiers and metadata, then ranks candidates using a 0–100 release-quality score in internal/scorer and internal/search/scoring. This is where the repo’s “find, score, download, upgrade” policy lives: provider results are not accepted directly; they must pass identity gates and quality heuristics before download.
9. Subtitle download and sync pipeline
After a subtitle is selected, internal/search/search_download.go and internal/subsync handle the file-processing path. internal/subsync is a substantial pure-algorithm subsystem: it parses subtitle formats, extracts audio via ffmpeg, performs VAD and alignment, estimates framerate, and time-shifts cues so the final subtitle matches the video. The output boundary is a written subtitle file on the media filesystem, not an in-memory result.
10. Automation, locks, and manual overrides
internal/server/manualops, internal/server/polling, and internal/server/scheduler coordinate recurring work and user-initiated actions. Manual downloads create locks so automation does not overwrite them; the system also clears locks when files are deleted or media is replaced. This separation between automated scans and explicit user actions is important in the architecture diagram.
11. HTTP server, UI, and event flow
internal/server is the main composition layer for HTTP routes, middleware, auth, scans, config, files, coverage, and sync endpoints. The UI is a vanilla TypeScript SPA in internal/server/static-src, embedded into the binary under internal/server/static. The server also exposes SSE/live updates through the event bus in internal/server/events, so the UI can react to scan/search/status changes without polling everything.
12. Authentication and security model
Authentication is handled by internal/authstore and the auth-related handlers under internal/server/authhandlers. Supported mechanisms include local passwords, API keys, passkeys/WebAuthn, OIDC, and TOTP. The repo treats client IP, rate limiting, and audit logs carefully; proxy awareness is configurable via trusted CIDRs so forwarded IPs are only accepted from known reverse proxies.
13. Observability and operational boundaries
Metrics are exposed via internal/metrics and the server’s /metrics endpoint. The repo also includes structured logging, activity tracking, and health/operability support in docs/OPERABILITY.md. Operationally, the service is designed to be self-hosted and low-memory, with explicit tests and workflow files for CI, security scans, and release packaging.
14. Key external dependencies to show in a diagram
The main external systems are:
- Sonarr/Radarr instances via the Arr API layer,
- subtitle provider APIs,
- the media filesystem where subtitle files are written,
- ffmpeg/ffprobe for sync and media analysis,
- and browser clients hitting the embedded web UI/API.
Everything else is internal composition around those boundaries.
Main entry points: wiring.go Wiring · server.go HTTP server · search.go Search engine · registry.go Provider registry
Components 21
Runtime entrypoints
CLI, daemon, and codegen entrypoints.
- Main service · Long-running daemon entrypoint for server and background work.
main.go - CLI client · Command-line entrypoint for local and remote control.
cli.go - Wire codegen · Dependency wiring generation helper.
cmd/wire-codegen/main.go
Core orchestration
Config, wiring, search, and sync pipeline.
- Config model · Loads, validates, expands, and paths config as a first-class boundary.
internal/config/config_load.go - Config schema · Schema and section metadata used to shape and render config.
internal/config/schema/schema.go - Wiring · Assembles services from config, stores, providers, and server components.
internal/wiring/wiring.go - Arr API · Sonarr/Radarr integration for history, wanted items, and notifications.
internal/arrapi/client.go - Search engine · Orchestrates media inventory, provider queries, scoring, and download decisions.
internal/search/search.go - Scoring · Release-quality and identity scoring used to rank candidate subtitles.
internal/scorer/scorer.go
HTTP server
API, auth, UI, events, scanning, and automation.
- HTTP server · Main server composition for routes, middleware, auth, scans, config, files, and sync.
internal/server/server.go - Event bus · SSE/event propagation for scan, search, status, and activity updates.
internal/server/events/bus.go - Scanner · Scheduled and on-demand library scans that build scan items and feed the search loop.
internal/server/scanning/scanning.go - Manual ops · Manual search/download flows and locks that protect explicit user actions.
internal/server/manualops/manualops.go
State and stores
Embedded storage, auth state, caches, and KV contracts.
- Auth store · Users, sessions, API keys, passkeys, OIDC state, and sweeper logic.
internal/authstore/store.go - Bolt store · bbolt-backed storage engine and maintenance for persistent state.
internal/boltstore/store.go - KV contracts · Low-level KV interfaces, codecs, keys, and contracts shared by stores.
internal/store/kv/kv.go
Provider layer
Subtitle provider integrations and registry.
- Provider registry · Registers provider implementations and normalizes provider behavior.
internal/provider/registry.go - OpenSubtitles · Representative HTTP-backed provider with auth/search/download logic.
internal/provider/opensubtitles/opensubtitles.go - SubDL · Representative provider implementation for subtitle search and download.
internal/provider/subdl/subdl.go
Media sync
Subtitle parsing, alignment, and ffmpeg-driven syncing.
- Subsync · Parsing, ffmpeg audio extraction, VAD, framerate estimation, and cue shifting.
internal/subsync/subsync.go - FFmpeg bridge · FFmpeg/ffprobe wrappers used for extraction and probing.
internal/subsync/ffmpeg/ffmpeg.go
Connections 23
- Main service → Config model: load/validate · Bootstraps runtime configuration before any services start.
- Main service → Wiring: compose · Builds the service graph for daemon mode.
- CLI client → Config model: read config · CLI resolves local or remote settings and targets.
- Wire codegen → Wiring: generate · Produces generated dependency wiring support.
- Config model → Config schema: schema · Configuration is constrained and documented by schema metadata.
- Wiring → HTTP server: build · Composition root creates the HTTP server and related subsystems.
- Wiring → Search engine: build · Wires search pipeline from config, stores, and providers.
- Wiring → Provider registry: register · Installs configured provider implementations.
- Wiring → Auth store: build · Creates auth/session state backed by embedded storage.
- Wiring → Bolt store: open · Opens the embedded key-value store backend.
- Bolt store → KV contracts: implements · Persistent storage is built on shared KV abstractions and codecs.
- HTTP server → Event bus: publish · Server emits lifecycle and status updates over the event bus.
- HTTP server → Scanner: run scans · HTTP and scheduler-triggered scanning flows enter the scanner.
- HTTP server → Manual ops: manual actions · User-initiated search/download operations go through manual ops.
- HTTP server → Auth store: authenticate · Auth handlers consult persistent identity/session state.
- HTTP server → Arr API: arr routes · Server and scan flows consume Sonarr/Radarr data through Arr integration.
- Arr API → Search engine: media events · Arr history and wanted-state data feed the search pipeline.
- Search engine → Provider registry: query providers · Search fans out to registered subtitle providers uniformly.
- Search engine → Scoring: rank · Candidate subtitles are filtered and scored before download.
- Search engine → Subsync: sync subtitle · Downloaded subtitles are aligned and written back to the media filesystem.
- Subsync → FFmpeg bridge: probe/extract · Alignment depends on ffmpeg/ffprobe audio and media analysis.
- Provider registry → OpenSubtitles: provider · Representative provider implementation behind the registry.
- Provider registry → SubDL: provider · Representative provider implementation behind the registry.