Files
minstrel/internal/config/config.go
T
bvandeusen c4cccea775 refactor(server): remove bootstrap admin path
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>
2026-05-08 22:14:33 -04:00

207 lines
6.4 KiB
Go

package config
import (
"fmt"
"os"
"strconv"
"strings"
"gopkg.in/yaml.v3"
)
type Config struct {
Server ServerConfig `yaml:"server"`
Storage StorageConfig `yaml:"storage"`
Database DatabaseConfig `yaml:"database"`
Log LogConfig `yaml:"log"`
Library LibraryConfig `yaml:"library"`
Subsonic SubsonicConfig `yaml:"subsonic"`
Events EventsConfig `yaml:"events"`
Recommendation RecommendationConfig `yaml:"recommendation"`
Branding BrandingConfig `yaml:"branding"`
}
type ServerConfig struct {
Address string `yaml:"address"`
}
// StorageConfig points at on-disk locations the server uses for cached
// runtime artifacts. Currently:
// - DataDir hosts <DataDir>/playlist_covers/{playlist_id}.jpg (M7 #352).
//
// More entries may grow here (e.g., transcode cache) without breaking
// existing layouts.
type StorageConfig struct {
// DataDir is the root for cached artifacts. Default: "./data" (relative
// to the working directory). Operators in production should set an
// absolute path persistent across restarts.
DataDir string `yaml:"data_dir"`
}
// BrandingConfig drives per-instance UI labelling: the app name shown
// in the header / tab title and the description used in OG share-preview
// metadata. Both default to sensible values when unset.
type BrandingConfig struct {
AppName string `yaml:"app_name"`
Description string `yaml:"description"`
}
type DatabaseConfig struct {
URL string `yaml:"url"`
}
type LogConfig struct {
Level string `yaml:"level"`
Format string `yaml:"format"`
}
type LibraryConfig struct {
ScanPaths []string `yaml:"scan_paths"`
ScanOnStartup bool `yaml:"scan_on_startup"`
CoverArtFromMBCAA bool `yaml:"coverart_from_mbcaa"` // default true
CoverArtBackfillCap int `yaml:"coverart_backfill_cap"` // default 500
ContactEmail string `yaml:"contact_email"` // optional override for MBCAA UA
}
// SubsonicConfig controls wire-format concessions on the /rest/* surface.
// AllowPlaintextPassword gates the `p=` parameter; the recommended posture is
// to leave it disabled and require apiKey (OpenSubsonic) or t+s.
type SubsonicConfig struct {
AllowPlaintextPassword bool `yaml:"allow_plaintext_password"`
}
// EventsConfig governs play-event ingestion: session window, skip rule.
type EventsConfig struct {
SessionTimeoutMinutes int `yaml:"session_timeout_minutes"`
SkipMaxCompletionRatio float64 `yaml:"skip_max_completion_ratio"`
SkipMaxDurationPlayedMs int `yaml:"skip_max_duration_played_ms"`
}
// RecommendationConfig governs the M3 weighted-shuffle scoring (spec §6).
// All weights are operator-tunable; defaults match the spec recommendations.
type RecommendationConfig struct {
BaseWeight float64 `yaml:"base_weight"`
LikeBoost float64 `yaml:"like_boost"`
RecencyWeight float64 `yaml:"recency_weight"`
SkipPenalty float64 `yaml:"skip_penalty"`
JitterMagnitude float64 `yaml:"jitter_magnitude"`
ContextWeight float64 `yaml:"context_weight"`
SimilarityWeight float64 `yaml:"similarity_weight"`
RecentlyPlayedHours int `yaml:"recently_played_hours"`
RadioSize int `yaml:"radio_size"`
RadioSizeMax int `yaml:"radio_size_max"`
}
func Default() Config {
return Config{
Server: ServerConfig{Address: ":4533"},
Storage: StorageConfig{DataDir: "./data"},
Database: DatabaseConfig{URL: ""},
Log: LogConfig{Level: "info", Format: "text"},
Branding: BrandingConfig{
AppName: "Minstrel",
Description: "Self-hosted music server with Subsonic compatibility, smart shuffle, and Lidarr integration.",
},
Events: EventsConfig{
SessionTimeoutMinutes: 30,
SkipMaxCompletionRatio: 0.5,
SkipMaxDurationPlayedMs: 30000,
},
Recommendation: RecommendationConfig{
BaseWeight: 1.0,
LikeBoost: 2.0,
RecencyWeight: 1.0,
SkipPenalty: 1.0,
JitterMagnitude: 0.1,
ContextWeight: 2.0,
SimilarityWeight: 2.0,
RecentlyPlayedHours: 1,
RadioSize: 50,
RadioSizeMax: 200,
},
Library: LibraryConfig{
CoverArtFromMBCAA: true,
CoverArtBackfillCap: 500,
},
}
}
func Load(path string) (Config, error) {
cfg := Default()
if path != "" {
data, err := os.ReadFile(path)
if err != nil {
if !os.IsNotExist(err) {
return cfg, fmt.Errorf("read config %q: %w", path, err)
}
} else if err := yaml.Unmarshal(data, &cfg); err != nil {
return cfg, fmt.Errorf("parse config %q: %w", path, err)
}
}
applyEnv(&cfg)
return cfg, nil
}
func applyEnv(cfg *Config) {
if v, ok := os.LookupEnv("MINSTREL_SERVER_ADDRESS"); ok {
cfg.Server.Address = v
}
if v, ok := os.LookupEnv("MINSTREL_STORAGE_DATA_DIR"); ok {
cfg.Storage.DataDir = v
}
if v, ok := os.LookupEnv("MINSTREL_DATABASE_URL"); ok {
cfg.Database.URL = v
}
if v, ok := os.LookupEnv("MINSTREL_LOG_LEVEL"); ok {
cfg.Log.Level = strings.ToLower(v)
}
if v, ok := os.LookupEnv("MINSTREL_LOG_FORMAT"); ok {
cfg.Log.Format = strings.ToLower(v)
}
if v, ok := os.LookupEnv("MINSTREL_BRANDING_APP_NAME"); ok && v != "" {
cfg.Branding.AppName = v
}
if v, ok := os.LookupEnv("MINSTREL_BRANDING_DESCRIPTION"); ok && v != "" {
cfg.Branding.Description = v
}
if v, ok := os.LookupEnv("MINSTREL_LIBRARY_SCAN_PATHS"); ok {
cfg.Library.ScanPaths = splitPaths(v)
}
if v, ok := os.LookupEnv("MINSTREL_LIBRARY_SCAN_ON_STARTUP"); ok {
if b, err := strconv.ParseBool(v); err == nil {
cfg.Library.ScanOnStartup = b
}
}
if v, ok := os.LookupEnv("MINSTREL_LIBRARY_COVERART_FROM_MBCAA"); ok {
if b, err := strconv.ParseBool(v); err == nil {
cfg.Library.CoverArtFromMBCAA = b
}
}
if v, ok := os.LookupEnv("MINSTREL_LIBRARY_COVERART_BACKFILL_CAP"); ok {
if n, err := strconv.Atoi(v); err == nil {
cfg.Library.CoverArtBackfillCap = n
}
}
if v, ok := os.LookupEnv("MINSTREL_CONTACT_EMAIL"); ok {
cfg.Library.ContactEmail = v
}
if v, ok := os.LookupEnv("MINSTREL_SUBSONIC_ALLOW_PLAINTEXT_PASSWORD"); ok {
if b, err := strconv.ParseBool(v); err == nil {
cfg.Subsonic.AllowPlaintextPassword = b
}
}
}
// splitPaths splits a colon-separated path list, dropping empty entries.
// Colon matches PATH-style Unix conventions; Minstrel ships Linux-only.
func splitPaths(s string) []string {
parts := strings.Split(s, ":")
out := parts[:0]
for _, p := range parts {
if p != "" {
out = append(out, p)
}
}
return out
}