Vellum now speaks two foreign protocols, so the clients people already own can
use it. Both are translation layers over queries that already exist, and both go
through the same grant chokepoint as the web app — a token is a second way to be
a user, never a second set of permissions.
Shared foundation (coordinator, written before dispatch)
- migrations/0012_api_tokens.sql + internal/apitoken. Both protocols need a
credential that is not the session cookie, and letting two agents each invent
a token table would have produced two incompatible ones.
- The table stores two shapes for a real reason: a Jellyfin token is a bearer
value so only its hash is kept, while a Subsonic key must be recoverable
because verifying `t` means computing md5(key + salt) server-side. token_plain
is therefore the one column in the schema holding a usable credential. It is a
generated random secret, never a user password, and it is revocable.
Wave 9 - Jellyfin
- /System/Info/Public, /Users/AuthenticateByName, /Users/Me, /Users/{id},
/Items, /Users/{id}/Items, /Items/{id}, /Items/{id}/Images/Primary,
/Items/{id}/PlaybackInfo, /Videos/{id}/stream, /Audio/{id}/stream,
/Sessions/Playing{,/Progress,/Stopped}.
- Auth accepts X-Emby-Token, X-MediaBrowser-Token, ?api_key=, and
Authorization: MediaBrowser Token="...". The ?api_key= form is the one that
matters: <video> elements cannot set headers.
- Honest about the one thing Vellum cannot do. PlaybackInfo reports
SupportsDirectPlay/SupportsDirectStream true and SupportsTranscoding FALSE,
with a DirectStreamUrl carrying the api_key. A client that disables both
direct modes is demanding a transcode and is refused with ErrorCode
"NoCompatibleStream" — rather than handed a TranscodingUrl that would never
resolve, which is how a client hangs forever.
Wave 10 - Subsonic
- /rest/{method} dispatch with ping, getLicense, getMusicFolders, getIndexes,
getArtists/getArtist/getAlbum, getMusicDirectory, getSong, search3, stream,
getCoverArt, getAlbumList2, getRandomSongs, scrobble, getUser, getPlaylists,
plus GET /api/v1/subsonic/credentials to hand a user their key.
- Auth via t=md5(key+salt), p=key, constant-time comparison. A bad credential
returns HTTP 200 with the envelope status "failed" and error code 40 — NOT an
HTTP 401. Subsonic clients read the envelope, and getting this wrong is the
single most common integration failure in that protocol.
- stream supports format=raw and never invokes ffmpeg.
Coordinator wired both into main.go (the line agents were told to leave alone)
and verified against real requests.
Verified end to end, 18 packages pass, gofmt/vet clean, svelte-check 0/0:
- Jellyfin: discovery, AuthenticateByName minting a token, /Items reporting 18,
PlaybackInfo showing SupportsTranscoding=false, and a Range request to
/Videos/{id}/stream using ONLY ?api_key= returning 206 with Content-Range.
- Subsonic: ping as JSON and XML, md5(key+salt) auth succeeding, and a bad token
returning the failed envelope with code 40.
- Scoping, both protocols, with a genuinely non-admin user: 0 items, 404 on an
admin-only item, 404 on its stream, 0 music folders; admin still sees 18.
- The SPA is not shadowed by the root-mounted compat paths: /, /healthz and
/api/v1/version all still answer correctly.
Both agents failed with an HTTP 400 at the REPORT stage, not during the work;
their files were complete and compile, so the work was salvaged and verified
here rather than redone.
216 lines
7.4 KiB
Go
216 lines
7.4 KiB
Go
// Command vellum is the unified media server: one process, one database,
|
|
// every media type.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/mithral/vellum/internal/auth"
|
|
"github.com/mithral/vellum/internal/cli"
|
|
"github.com/mithral/vellum/internal/core"
|
|
"github.com/mithral/vellum/internal/httpapi"
|
|
"github.com/mithral/vellum/internal/jellyfin"
|
|
"github.com/mithral/vellum/internal/jobs"
|
|
"github.com/mithral/vellum/internal/library"
|
|
"github.com/mithral/vellum/internal/media"
|
|
"github.com/mithral/vellum/internal/metadata"
|
|
"github.com/mithral/vellum/internal/modules/comics"
|
|
"github.com/mithral/vellum/internal/modules/ebooks"
|
|
"github.com/mithral/vellum/internal/modules/games"
|
|
_ "github.com/mithral/vellum/internal/modules/photos" // registers the thumbnail extractor
|
|
"github.com/mithral/vellum/internal/playback"
|
|
"github.com/mithral/vellum/internal/search"
|
|
"github.com/mithral/vellum/internal/subsonic"
|
|
"github.com/mithral/vellum/migrations"
|
|
)
|
|
|
|
// version is overridden at build time with -ldflags "-X main.version=...".
|
|
var version = "dev"
|
|
|
|
func main() {
|
|
cfg, err := core.LoadConfig()
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "vellum: fatal:", err)
|
|
os.Exit(1)
|
|
}
|
|
log := core.NewLogger(cfg.LogLevel, cfg.LogFormat)
|
|
|
|
// Administrative subcommands run instead of the server. This is the way
|
|
// back in when someone is locked out, and the way to take a backup while
|
|
// the server is stopped.
|
|
if handled, err := cli.Run(os.Args[1:], cfg, log); handled {
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "vellum:", err)
|
|
os.Exit(1)
|
|
}
|
|
return
|
|
}
|
|
|
|
if err := run(cfg, log); err != nil {
|
|
fmt.Fprintln(os.Stderr, "vellum: fatal:", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func run(cfg core.Config, log *slog.Logger) error {
|
|
log.Info("starting vellum", "version", version, "listen", cfg.Listen)
|
|
|
|
if err := cfg.Validate(); err != nil {
|
|
return err
|
|
}
|
|
|
|
db, err := core.OpenDB(cfg.DBPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer db.Close()
|
|
|
|
migCtx, cancelMig := context.WithTimeout(context.Background(), 60*time.Second)
|
|
err = core.Migrate(migCtx, db, migrations.FS, log)
|
|
cancelMig()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
api := httpapi.New(db, log, version)
|
|
|
|
authenticator := auth.NewAuthenticator(db, cfg.CookieName, cfg.SessionTTL, cfg.CookieSecure, log)
|
|
if !cfg.CookieSecure {
|
|
log.Warn("VELLUM_COOKIE_SECURE is false; session cookies will be sent over plain HTTP. " +
|
|
"Set it to true when serving over TLS.")
|
|
}
|
|
|
|
if err := auth.EnsureAdminFromEnv(context.Background(), db, log, cfg.AdminUser, cfg.AdminPassword); err != nil {
|
|
return fmt.Errorf("bootstrap admin: %w", err)
|
|
}
|
|
|
|
authHandlers := auth.NewHandlers(authenticator, log)
|
|
authHandlers.Register(api.Mux())
|
|
api.SetAuth(authenticator)
|
|
|
|
// Background work: a table-backed queue in the same database. Created
|
|
// before the modules so a module's handlers can enqueue work.
|
|
queue := jobs.New(db, log)
|
|
|
|
library.NewHandlers(db, log, authenticator, queue).Register(api.Mux())
|
|
media.NewHandlers(db, log, authenticator, cfg.ConfigDir).Register(api.Mux())
|
|
ebooks.NewHandlers(db, log, authenticator).Register(api.Mux())
|
|
comics.NewHandlers(db, log, authenticator).Register(api.Mux())
|
|
games.NewHandlers(db, log, authenticator).Register(api.Mux())
|
|
search.NewHandlers(db, log, authenticator).Register(api.Mux())
|
|
// Playback engine registry + selection. Vellum never transcodes, so the
|
|
// server reports what is inside a file and matches it against the engines the
|
|
// CLIENT declares — a browser offers "html5", a native client can offer mpv.
|
|
playback.NewHandlers(db, log, authenticator).Register(api.Mux())
|
|
|
|
// Client compatibility layers. Neither speaks the session cookie; both
|
|
// authenticate with an api_tokens credential and then go through the same
|
|
// grant chokepoint as the web app. They mount at the root because that is
|
|
// where the real clients look, and both report SupportsTranscoding=false:
|
|
// this server has no CPU for it, so a client that demands a transcode is
|
|
// refused rather than left waiting for one that will never start.
|
|
jellyfin.NewHandlers(db, log, authenticator).Register(api.Mux())
|
|
subsonic.NewHandlers(db, log, authenticator).Register(api.Mux())
|
|
|
|
// Scans run on the queue rather than inside the request: a real library
|
|
// takes longer than any sensible timeout, and a hanging request is
|
|
// indistinguishable from a broken server. The result is written back so
|
|
// the UI can read progress.
|
|
queue.Register(library.ScanJobKind, func(ctx context.Context, payload json.RawMessage) error {
|
|
var p library.ScanPayload
|
|
if err := json.Unmarshal(payload, &p); err != nil {
|
|
return fmt.Errorf("scan: bad payload: %w", err)
|
|
}
|
|
lib, err := library.Get(ctx, db, p.LibraryID)
|
|
if err != nil {
|
|
return nil // deleted since the job was queued
|
|
}
|
|
res, err := library.Scan(ctx, db, lib, log)
|
|
if jobID, ok := jobs.JobID(ctx); ok {
|
|
if serr := jobs.SetResult(ctx, db, jobID, res); serr != nil {
|
|
log.Warn("record scan result", "err", serr)
|
|
}
|
|
}
|
|
return err
|
|
})
|
|
|
|
// Providers are registered primary-first; a fallback is only consulted
|
|
// when the primary returns nothing.
|
|
metadata.Register(metadata.NewOpenLibrary())
|
|
metadata.Register(metadata.NewGoogleBooks())
|
|
metadata.Register(metadata.NewMusicBrainz())
|
|
if cfg.ComicVineAPIKey != "" {
|
|
metadata.Register(metadata.NewComicVine(cfg.ComicVineAPIKey))
|
|
} else {
|
|
log.Info("comic metadata disabled: set VELLUM_COMICVINE_API_KEY to enable ComicVine lookups")
|
|
}
|
|
if cfg.IGDBClientID != "" && cfg.IGDBClientSecret != "" {
|
|
metadata.Register(metadata.NewIGDB(cfg.IGDBClientID, cfg.IGDBClientSecret))
|
|
} else {
|
|
log.Info("game metadata disabled: set VELLUM_IGDB_CLIENT_ID and VELLUM_IGDB_CLIENT_SECRET to enable IGDB lookups")
|
|
}
|
|
enricher := metadata.NewEnricher(db, log, cfg.ConfigDir)
|
|
queue.Register(metadata.JobKind, enricher.Handle)
|
|
metadata.NewHandlers(db, log, authenticator, queue).Register(api.Mux())
|
|
|
|
// Anything left 'running' belongs to a previous process that died mid-job.
|
|
// Requeue before the workers start, or it stays 'running' forever and the
|
|
// UI polls a job that will never move.
|
|
if n, err := queue.RecoverStale(context.Background()); err != nil {
|
|
log.Warn("recover stale jobs", "err", err)
|
|
} else if n > 0 {
|
|
log.Info("requeued jobs left running by a previous process", "count", n)
|
|
}
|
|
|
|
jobCtx, cancelJobs := context.WithCancel(context.Background())
|
|
defer cancelJobs()
|
|
go queue.Run(jobCtx)
|
|
|
|
srv := &http.Server{
|
|
Addr: cfg.Listen,
|
|
Handler: api.Handler(),
|
|
// ReadHeaderTimeout protects against slowloris. WriteTimeout is
|
|
// deliberately zero: streaming large media files must not be cut off.
|
|
ReadHeaderTimeout: 10 * time.Second,
|
|
ReadTimeout: cfg.ReadTimeout,
|
|
WriteTimeout: cfg.WriteTimeout,
|
|
IdleTimeout: cfg.IdleTimeout,
|
|
ErrorLog: nil,
|
|
}
|
|
|
|
errCh := make(chan error, 1)
|
|
go func() {
|
|
log.Info("listening", "addr", cfg.Listen, "data_dir", cfg.DataDir)
|
|
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
errCh <- err
|
|
}
|
|
}()
|
|
|
|
stop := make(chan os.Signal, 1)
|
|
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
|
|
|
|
select {
|
|
case err := <-errCh:
|
|
return fmt.Errorf("http server: %w", err)
|
|
case sig := <-stop:
|
|
log.Info("shutting down", "signal", sig.String())
|
|
}
|
|
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), cfg.ShutdownGrace)
|
|
defer cancel()
|
|
if err := srv.Shutdown(shutdownCtx); err != nil {
|
|
return fmt.Errorf("graceful shutdown: %w", err)
|
|
}
|
|
cancelJobs()
|
|
log.Info("stopped")
|
|
return nil
|
|
}
|