refactor(providers): cover art and tags share one registry and settings service (#8002)
coverart and tags each carried a line-for-line copy of the provider registry and the settings service: reconcile at boot, seal and reveal API keys, bump the sources version when the enabled set or the registered set changes. A fix to either had to be made twice. internal/providers now holds Registry, Service (over a small Store interface) and EnabledAs[T]. Each family keeps its own tables, provider set and capabilities: a 40-line Store adapter over its sqlc queries and its capability views (EnabledAlbumProviders, EnabledTrackTagProviders). Provider, ProviderSettings, TestableProvider, ProviderInfo and ProviderUpdatePatch stay as the families' names for the shared types. The service has unit tests over an in-memory store. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+35
-112
@@ -3,53 +3,20 @@ package coverart
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/providers"
|
||||
)
|
||||
|
||||
// Provider is the base shape every cover-art source implements.
|
||||
// Providers register themselves at init() time via Register(). The
|
||||
// enricher reads the current per-provider settings and dispatches to
|
||||
// the capability methods below.
|
||||
type Provider interface {
|
||||
// ID is a stable lowercase token persisted in DB columns
|
||||
// (cover_art_source / artist_art_source) and the
|
||||
// cover_art_provider_settings.provider_id key, and shown in the admin UI.
|
||||
ID() string
|
||||
// Provider is the base shape every cover-art source implements; the
|
||||
// capability interfaces below say what it can fetch. Providers register at
|
||||
// init() via Register(). Its ID is persisted in cover_art_source /
|
||||
// artist_art_source and keyed in cover_art_provider_settings.
|
||||
type Provider = providers.Provider
|
||||
|
||||
// DisplayName is the human-readable label shown in the admin
|
||||
// Settings card.
|
||||
DisplayName() string
|
||||
|
||||
// RequiresAPIKey reports whether the provider needs a key to
|
||||
// function. True for theaudiodb / fanart.tv; false for mbcaa.
|
||||
// The admin UI uses this to show or hide the api_key text field;
|
||||
// it does NOT gate the enabled toggle (per the no-coercive-
|
||||
// settings principle, providers with usable defaults — such as
|
||||
// TheAudioDB's public test key — ship working).
|
||||
RequiresAPIKey() bool
|
||||
|
||||
// DefaultEnabled is whether a freshly-installed Minstrel ships
|
||||
// with this provider on. Providers that need an API key ship off.
|
||||
DefaultEnabled() bool
|
||||
|
||||
// Configure receives the admin's settings (api_key, enabled
|
||||
// flag) and applies them. Called at boot during reconciliation
|
||||
// and on every admin PATCH. Implementations must be idempotent
|
||||
// and goroutine-safe (the enricher may be reading concurrently).
|
||||
Configure(ProviderSettings) error
|
||||
}
|
||||
|
||||
// ProviderSettings is the runtime config a provider receives when
|
||||
// Configure() is called. Providers may interpret an empty APIKey
|
||||
// however they wish — TheAudioDB falls back to its public test key,
|
||||
// MBCAA ignores the field entirely.
|
||||
type ProviderSettings struct {
|
||||
Enabled bool
|
||||
APIKey string
|
||||
}
|
||||
// ProviderSettings is what Configure receives. TheAudioDB falls back to
|
||||
// its public test key on an empty APIKey; MBCAA ignores the key.
|
||||
type ProviderSettings = providers.Settings
|
||||
|
||||
// ArtistRef is the lookup key passed to ArtistArtProvider.FetchArtistArt.
|
||||
// MBID is preferred when present; Name is the always-populated fallback
|
||||
@@ -92,22 +59,16 @@ type ArtistArtProvider interface {
|
||||
FetchArtistArt(ctx context.Context, ref ArtistRef) (thumb, fanart []byte, err error)
|
||||
}
|
||||
|
||||
// TestableProvider is an opt-in capability: providers that can answer
|
||||
// "is my config working?" without performing a full enrichment cycle.
|
||||
// Used by the admin Test-Connection button. MBCAA and TheAudioDB
|
||||
// implement this.
|
||||
type TestableProvider interface {
|
||||
Provider
|
||||
TestConnection(ctx context.Context) error
|
||||
}
|
||||
// TestableProvider can answer the admin Test-Connection button. MBCAA and
|
||||
// TheAudioDB implement it.
|
||||
type TestableProvider = providers.Testable
|
||||
|
||||
// ErrNotTestable is returned by the SettingsService.TestProvider
|
||||
// method when the named provider doesn't implement TestableProvider.
|
||||
var ErrNotTestable = errors.New("coverart: provider does not support test connection")
|
||||
|
||||
// ErrProviderNotFound is returned when the named provider is not
|
||||
// registered.
|
||||
var ErrProviderNotFound = errors.New("coverart: provider not registered")
|
||||
// ErrNotTestable and ErrProviderNotFound are the shared providers errors,
|
||||
// so a handler can test for either family's with one errors.Is.
|
||||
var (
|
||||
ErrNotTestable = providers.ErrNotTestable
|
||||
ErrProviderNotFound = providers.ErrProviderNotFound
|
||||
)
|
||||
|
||||
// ErrNotFound indicates the upstream confirmed there is no art for
|
||||
// this MBID — a terminal-this-pass result. The enricher treats this
|
||||
@@ -124,63 +85,25 @@ var ErrNotFound = apierror.ErrNotFound
|
||||
// scan pass can retry.
|
||||
var ErrTransient = errors.New("coverart: transient error")
|
||||
|
||||
// registry is the package-private list of all compiled-in providers.
|
||||
// Providers register at init() via Register(). The enricher iterates
|
||||
// over this list filtered by the current per-provider Settings.
|
||||
var registry = struct {
|
||||
mu sync.Mutex
|
||||
providers []Provider
|
||||
}{}
|
||||
// registry is every compiled-in cover-art provider. The enricher iterates
|
||||
// it filtered by the current settings.
|
||||
var registry = providers.NewRegistry("coverart")
|
||||
|
||||
// Register adds a provider to the registry. Must be called from
|
||||
// init() of a provider file (provider_mbcaa.go, provider_theaudiodb.go,
|
||||
// etc.). Calling twice with the same ID panics — providers are
|
||||
// compiled-in, duplicate registration is a build-time bug.
|
||||
func Register(p Provider) {
|
||||
registry.mu.Lock()
|
||||
defer registry.mu.Unlock()
|
||||
for _, existing := range registry.providers {
|
||||
if existing.ID() == p.ID() {
|
||||
panic(fmt.Sprintf("coverart: duplicate provider ID %q", p.ID()))
|
||||
}
|
||||
}
|
||||
registry.providers = append(registry.providers, p)
|
||||
}
|
||||
// Register adds a provider. Call it from a provider file's init(); a
|
||||
// duplicate ID panics.
|
||||
func Register(p Provider) { registry.Register(p) }
|
||||
|
||||
// AllProviders returns a snapshot of all registered providers in
|
||||
// registration order. Caller must not mutate the returned slice.
|
||||
func AllProviders() []Provider {
|
||||
registry.mu.Lock()
|
||||
defer registry.mu.Unlock()
|
||||
out := make([]Provider, len(registry.providers))
|
||||
copy(out, registry.providers)
|
||||
return out
|
||||
}
|
||||
// AllProviders returns a snapshot of every registered provider in
|
||||
// registration order.
|
||||
func AllProviders() []Provider { return registry.All() }
|
||||
|
||||
// ProviderByID looks up a registered provider by ID. Returns
|
||||
// ErrProviderNotFound if no such provider has registered.
|
||||
func ProviderByID(id string) (Provider, error) {
|
||||
registry.mu.Lock()
|
||||
defer registry.mu.Unlock()
|
||||
for _, p := range registry.providers {
|
||||
if p.ID() == id {
|
||||
return p, nil
|
||||
}
|
||||
}
|
||||
return nil, ErrProviderNotFound
|
||||
}
|
||||
// ProviderByID returns the registered provider with id, or
|
||||
// ErrProviderNotFound.
|
||||
func ProviderByID(id string) (Provider, error) { return registry.ByID(id) }
|
||||
|
||||
// resetRegistryForTests clears the registry. Test-only helper
|
||||
// (provider_test.go); never called from production code.
|
||||
func resetRegistryForTests() {
|
||||
registry.mu.Lock()
|
||||
defer registry.mu.Unlock()
|
||||
registry.providers = nil
|
||||
}
|
||||
// resetRegistryForTests empties the registry. Tests only.
|
||||
func resetRegistryForTests() { registry.ResetForTests() }
|
||||
|
||||
// ResetRegistryForTests is an exported wrapper around resetRegistryForTests
|
||||
// for use by tests in other packages (e.g. internal/api). Never call from
|
||||
// production code.
|
||||
func ResetRegistryForTests() {
|
||||
resetRegistryForTests()
|
||||
}
|
||||
// ResetRegistryForTests empties the registry, for tests in other packages
|
||||
// (internal/api). Tests only.
|
||||
func ResetRegistryForTests() { registry.ResetForTests() }
|
||||
|
||||
Reference in New Issue
Block a user