c4cccea775
The bootstrap-admin-from-env-vars flow was a holdover from before self-registration could create the first admin. Now that handleRegister + CreateUserFirstAdminRace promotes the first user to register on an empty users table (verified shipped in v2026.05.08.2 / #376), the bootstrap path is just a second source of truth that confuses operators (and leaves an "admin" account in the DB that nobody asked for). Removes: - internal/auth/bootstrap.go + its test - The auth.Bootstrap call from cmd/minstrel/main.go - AuthConfig + AdminBootstrapConfig structs from config - MINSTREL_AUTH_ADMIN_USERNAME / _PASSWORD env reads + their test - The auth: block from config.example.yaml - Bootstrap-related comments in docker-compose.yml + README.md The README quickstart now points operators at /register on first start instead of "watch the logs for a one-time password." Existing instances keep their bootstrap-admin row in the DB; operators who want it gone can register a new admin via /register, promote them in /admin/users, then delete the old bootstrap user (last-admin guard will require the new admin to be promoted first). No migration needed. Recovery story for forgotten admin passwords now hinges on Fable #321 (admin password reset CLI) — currently the only path back in if no other admin exists. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
209 lines
7.2 KiB
Go
209 lines
7.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/config"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/coverart"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/library"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/lidarrconfig"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/lidarrrequests"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/logging"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/scrobble"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/scrobble/listenbrainz"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/server"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/similarity"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/subsonic"
|
|
)
|
|
|
|
func main() {
|
|
if err := run(); err != nil {
|
|
fmt.Fprintf(os.Stderr, "minstrel: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func run() error {
|
|
configPath := flag.String("config", os.Getenv("MINSTREL_CONFIG"), "path to YAML config file")
|
|
flag.Parse()
|
|
|
|
cfg, err := config.Load(*configPath)
|
|
if err != nil {
|
|
return fmt.Errorf("load config: %w", err)
|
|
}
|
|
|
|
logger, err := logging.New(os.Stdout, cfg.Log.Level, cfg.Log.Format)
|
|
if err != nil {
|
|
return fmt.Errorf("init logger: %w", err)
|
|
}
|
|
|
|
logger.Info("minstrel starting",
|
|
"address", cfg.Server.Address,
|
|
"log_level", cfg.Log.Level,
|
|
"log_format", cfg.Log.Format,
|
|
)
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
defer stop()
|
|
|
|
if err := db.Migrate(cfg.Database.URL, logger); err != nil {
|
|
return fmt.Errorf("migrate: %w", err)
|
|
}
|
|
|
|
pool, err := db.Open(ctx, cfg.Database.URL)
|
|
if err != nil {
|
|
return fmt.Errorf("open db: %w", err)
|
|
}
|
|
defer pool.Close()
|
|
|
|
scanner := library.New(pool, logger, cfg.Library.ScanPaths)
|
|
|
|
contact := cfg.Library.ContactEmail
|
|
if contact == "" {
|
|
contact = "https://git.fabledsword.com/bvandeusen/minstrel"
|
|
}
|
|
// Swap in the production User-Agent + MinPeriod on the registered
|
|
// MBCAA provider. init() registered it with default config; this
|
|
// updates the running instance.
|
|
coverart.NewMBCAAProviderFromConfig(coverart.FetcherConfig{
|
|
UserAgent: fmt.Sprintf("Minstrel/dev (%s)", contact),
|
|
MinPeriod: time.Second,
|
|
})
|
|
|
|
coverSettings, err := coverart.NewSettingsService(ctx, pool, logger.With("component", "coverart"))
|
|
if err != nil {
|
|
logger.Error("coverart settings service init failed", "err", err)
|
|
os.Exit(1)
|
|
}
|
|
if newVer, bumped, berr := coverSettings.BumpVersionIfProvidersChanged(ctx); berr != nil {
|
|
logger.Warn("coverart: provider-hash boot check failed", "err", berr)
|
|
} else if bumped {
|
|
logger.Info("coverart: registered provider set changed; version bumped",
|
|
"new_version", newVer)
|
|
}
|
|
coverEnricher := coverart.NewEnricher(pool, logger.With("component", "coverart"), coverSettings)
|
|
coverEnricher.DataDir = cfg.Storage.DataDir
|
|
|
|
// One unified scan chain: library walk → MBID backfill → cover enrich.
|
|
// Boot-time scan and manual-trigger scans share this path; results land
|
|
// in scan_runs for the admin overview.
|
|
if cfg.Library.ScanOnStartup && len(cfg.Library.ScanPaths) > 0 {
|
|
go func() {
|
|
if _, err := library.RunScan(ctx, pool, scanner, coverEnricher,
|
|
logger.With("component", "scan_run"),
|
|
library.RunScanConfig{
|
|
BackfillCap: 5000,
|
|
EnrichCap: cfg.Library.CoverArtBackfillCap,
|
|
ArtistEnrichCap: cfg.Library.CoverArtBackfillCap,
|
|
DataDir: cfg.Storage.DataDir,
|
|
},
|
|
); err != nil {
|
|
logger.Warn("startup scan run failed", "err", err)
|
|
}
|
|
}()
|
|
} else {
|
|
// No startup file walk, but still run backfill + enrich so cover
|
|
// progress doesn't stall just because the operator disabled
|
|
// scan-on-startup.
|
|
go func() {
|
|
if _, err := library.RunScan(ctx, pool, nil, coverEnricher,
|
|
logger.With("component", "scan_run"),
|
|
library.RunScanConfig{
|
|
BackfillCap: 5000,
|
|
EnrichCap: cfg.Library.CoverArtBackfillCap,
|
|
ArtistEnrichCap: cfg.Library.CoverArtBackfillCap,
|
|
DataDir: cfg.Storage.DataDir,
|
|
},
|
|
); err != nil {
|
|
logger.Warn("boot-only scan run failed", "err", err)
|
|
}
|
|
}()
|
|
}
|
|
|
|
// Start the ListenBrainz scrobble worker. Per spec §M4a, runs every 30s
|
|
// and drains up to 50 pending rows per tick. Per-user gating happens
|
|
// inside the worker (rows from disabled users are skipped).
|
|
scrobbleWorker := scrobble.NewWorker(pool, listenbrainz.NewClient(), logger.With("component", "scrobble"))
|
|
go scrobbleWorker.Run(ctx)
|
|
|
|
// Start the similarity ingest worker. Per spec §M4b, runs every 1h, drains
|
|
// up to 5 played tracks + 5 played artists per tick, weekly re-fetch cap
|
|
// per row. Public LB endpoints — no token required.
|
|
similarityWorker := similarity.NewWorker(pool, listenbrainz.NewClient(), logger.With("component", "similarity"))
|
|
go similarityWorker.Run(ctx)
|
|
|
|
// Start the Lidarr reconciler worker. Per spec §M5a, polls pending Lidarr
|
|
// import requests and reconciles them against the library. Short-circuits
|
|
// to no-op when lidarr_config.enabled = false.
|
|
lidarrCfg := lidarrconfig.New(pool)
|
|
lidarrReconciler := lidarrrequests.NewReconciler(pool, lidarrCfg, logger.With("component", "lidarr"))
|
|
go lidarrReconciler.Run(ctx)
|
|
|
|
// Ensure DataDir exists before any service tries to write into it.
|
|
// Fatal on failure: a non-writable data_dir silently breaks every
|
|
// downstream cache (playlist covers, artist art, album-cover fallback)
|
|
// and produces "0 successes" symptoms that are hard to diagnose.
|
|
if cfg.Storage.DataDir != "" {
|
|
if err := os.MkdirAll(cfg.Storage.DataDir, 0o755); err != nil {
|
|
logger.Error("data_dir create failed; refusing to start",
|
|
"path", cfg.Storage.DataDir, "err", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
scanCfg := library.RunScanConfig{
|
|
BackfillCap: 5000,
|
|
EnrichCap: cfg.Library.CoverArtBackfillCap,
|
|
ArtistEnrichCap: cfg.Library.CoverArtBackfillCap,
|
|
DataDir: cfg.Storage.DataDir,
|
|
}
|
|
scheduler := library.NewScheduler(pool, logger.With("component", "scheduler"),
|
|
scanner, coverEnricher, scanCfg)
|
|
scheduler.Start(ctx)
|
|
srv := server.New(logger, pool, scanner, subsonic.Config{
|
|
AllowPlaintextPassword: cfg.Subsonic.AllowPlaintextPassword,
|
|
}, cfg.Events, cfg.Recommendation, cfg.Storage.DataDir, cfg.Branding, coverEnricher, cfg.Library.CoverArtBackfillCap, coverSettings, scanner, scanCfg, scheduler)
|
|
playlists.StartSystemPlaylistCron(ctx, pool, logger.With("component", "system_playlist_cron"), cfg.Storage.DataDir)
|
|
httpServer := &http.Server{
|
|
Addr: cfg.Server.Address,
|
|
Handler: srv.Router(),
|
|
ReadHeaderTimeout: 10 * time.Second,
|
|
}
|
|
|
|
errCh := make(chan error, 1)
|
|
go func() {
|
|
logger.Info("listening", "address", cfg.Server.Address)
|
|
if err := httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
errCh <- err
|
|
}
|
|
close(errCh)
|
|
}()
|
|
|
|
select {
|
|
case <-ctx.Done():
|
|
logger.Info("shutdown signal received")
|
|
case err := <-errCh:
|
|
if err != nil {
|
|
return fmt.Errorf("server: %w", err)
|
|
}
|
|
}
|
|
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
if err := httpServer.Shutdown(shutdownCtx); err != nil {
|
|
return fmt.Errorf("shutdown: %w", err)
|
|
}
|
|
logger.Info("minstrel stopped")
|
|
return nil
|
|
}
|